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
CommandRunnerby annotating a class with@cliRunnerand extending the generated superclass (uses the typical_$prefix).
@cliRunner
class GitRunner extends _$GitRunner {
// ...
}
- Create a
Commandby 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
Subcommandby annotating a class with@cliSubcommandand 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:
@cliRunnergenerates aCommandRunnerclass@cliCommandgenerates aCommandclass and overrides therunmethod with a call to your method or function@cliSubcommandgenerates aCommandclass 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
-
helpcomments from doc comments
-
-
annotations to help guide the generator
Command generation #
- ✅ Generate a
Commandclass using a@cliCommandannotation on a method or function - ✅ Generate a
Subcommandclass using a@cliSubcommandannotation - ✅ Generate a
CommandRunnerusing a@cliRunnerannotation- ✅ Allow mounting nested subcommands using a
@mountannotation
- ✅ Allow mounting nested subcommands using a