cli_annotations 0.1.0-dev.1
cli_annotations: ^0.1.0-dev.1 copied to clipboard
Annotations for generating CLI applications using `package:cli_gen`.
cli-gen #
A package for building cli applications using code generation and macros.
Before | After |
---|---|
|
|
Getting Started #
- Create a
CommandRunner
by annotating a class with@cliRunner
and extending the generated superclass (uses the typical_$
prefix).
@cliRunner
class GitRunner extends _$GitRunner {
// ...
}
- Create a
Command
by simply creating a method on the class. Any parameter type will be automatically parsed from string arguments.
@cliRunner
class GitRunner extends _$GitRunner {
@cliCommand
Future<void> merge({
required String branch,
MergeStrategy strategy = MergeStrategy.ort,
bool? commit,
}) async {
// ... application logic ...
}
}
- Alternatively, you can create a
Subcommand
by annotating a class with@cliSubcommand
and extending the generated superclass.
// Create a Subcommand
@cliSubcommand
class StashSubcommand extends _$StashSubcommand {
@cliCommand
Future<void> push() async {
// ... push application logic ...
}
@cliCommand
Future<void> pop() async {
// ... pop application logic ...
}
}
// Then mount it to the main `CommandRunner` or a parent `Subcommand`.
@cliRunner
class GitRunner extends _$GitRunner {
@mount
Command get stash => StashSubcommand();
}
That's all there is to it!
How it Works #
cli-gen
uses package:args
under the hood to manage argument parsing, command hierarchies, and help text generation. The annotations included with this package are roughly a 1:1 mapping to their package:args
equivalents, for example:
@cliRunner
generates aCommandRunner
class@cliCommand
generates aCommand
class and overrides therun
method with a call to your method or function@cliSubcommand
generates aCommand
class and adds all nested commands as subcommands
Examples of the generated code can be found in the example
project, within their respective .g.dart
files.
Features #
ArgParser generation from Parameters #
-
Generate an ArgParser from a Constructor or Method/Function
-
Auto Argument Parsing (convert a String/bool argument into the expected Dart type, without using annotations to tell the builder what parser to use):
- ✅ Primatives:
- ✅ String
- ✅ int
- ✅ double
- ✅ bool
- ✅ Uri
- ✅ DateTime
- ✅ Collections:
- ✅ List
- ✅ Set
- ✅ Iterable
- ❌ Map
- ❌ User-Defined types:
- ✅ Enums
- ❌ Classes
- ❌ Extension Types
- ✅ Primatives:
-
Multi-Select arguments
- ❌ List of primative values
- ❌ enums for a finite list of values
-
help
comments from doc comments
-
-
annotations to help guide the generator
Command generation #
- ✅ Generate a
Command
class using a@cliCommand
annotation on a method or function - ✅ Generate a
Subcommand
class using a@cliSubcommand
annotation - ✅ Generate a
CommandRunner
using a@cliRunner
annotation- ✅ Allow mounting nested subcommands using a
@mount
annotation
- ✅ Allow mounting nested subcommands using a