A lightweight and powerful Dart library for creating command-line applications with a declarative API.
Add commander_cli to your pubspec.yaml file:
dependencies:
commander_cli: ^1.0.0void main(List<String> arguments) {
CommandManager(identifier: 'mineral')
..load(Directory.current.uri)
// Used to track commands included in dependencies
..followDependencies()
..run(arguments);
}@Command(name: 'foo', description: 'Foo command')
@Option(name: 'name', help: 'Name of the user', abbr: 'n')
@Option(name: 'age', help: 'Current age', abbr: 'a')
Future<void> main(_, input) async {
final parser = CommandParser(input);
print(parser.args.toMap());
print(parser.flags.toMap());
exit(0);
}You can validate your input data with Vine or another package like this:
final validator = vine.compile(
vine.object({
vine.string('name'),
vine.number('age').min(18),
})
);
@Command(name: 'foo', description: 'Foo command')
@Option(name: 'name', help: 'Name of the user', abbr: 'n')
@Option(name: 'age', help: 'Current age', abbr: 'a')
Future<void> main(_, input) async {
final parser = CommandParser(input);
final data = await validator.validate(parser.args.toMap());
print(data);
exit(0);
}You should run the following command to generate the declaration:
dart run bin/main.dart generatename: your_project
mineral:
includes: lib/commands
commands:
- name: foo
description: Foo command
entrypoint: lib/commands/foo.dart
options:
- name: name
abbr: n
help: Name of the user
- name: age
abbr: a
help: Current ageNow you can run your command:
dart run bin/main.dart foo --name "John Doe" --age 30