doRun method
Implementation
@override
Future<int> doRun() async {
final config = CustomCommandsConfig.instance;
printInfo('Add new custom command');
printInfo('');
// Get command name
final name = _prompt('Command name');
if (name.isEmpty) {
printError('Command name is required');
return 1;
}
if (config.hasCommand(name)) {
printError('Command already exists: $name');
return 1;
}
// Get description
final description = _prompt('Description (optional)');
// Get aliases
final aliasesStr = _prompt('Aliases (comma-separated, optional)');
final aliases = aliasesStr.isNotEmpty
? aliasesStr.split(',').map((e) => e.trim()).toList()
: <String>[];
// Get arguments
final arguments = <CustomCommandArgument>[];
while (true) {
final addArg = _prompt('Add argument? (y/n)', defaultValue: 'n');
if (addArg.toLowerCase() != 'y') break;
final arg = _promptArgument();
if (arg != null) {
arguments.add(arg);
}
}
// Get actions
final actions = <CustomCommandAction>[];
while (true) {
final addAction = _prompt('Add action? (y/n)', defaultValue: actions.isEmpty ? 'y' : 'n');
if (addAction.toLowerCase() != 'y') {
if (actions.isEmpty) {
printError('At least one action is required');
continue;
}
break;
}
final action = _promptAction();
if (action != null) {
actions.add(action);
}
}
if (actions.isEmpty) {
printError('At least one action is required');
return 1;
}
// Create command definition
final command = CustomCommandDefinition(
name: name,
description: description,
aliases: aliases,
arguments: arguments,
actions: actions,
);
try {
config.addCommand(command);
config.save();
printInfo('');
printInfo('Custom command added: $name');
printInfo('Config saved to: ${config.getOrCreateConfigPath()}');
printInfo('');
printInfo('You can now use it with:');
printInfo(' alex $name');
return 0;
} catch (e) {
printError('Failed to add command: $e');
return 1;
}
}