-
Notifications
You must be signed in to change notification settings - Fork 4
refactor: Simplified default usage of BetterCommand/Runner #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cbec496
chore: meta dep changed to 1.11, same as serverpod
christerswahn 9581cec
refactor: made MessageOutput constructor const
christerswahn 878579d
refactor: Renamed StandardGlobalOptions
christerswahn 36c2e3e
refactor: Cleaned up globalConfig initialization
christerswahn a608991
feat: Commands inherit some defaults from runner
christerswahn bd24174
fix: Global config resolve doesn't block subcommands
christerswahn 66a70b4
docs: Clarified Configuration value types
christerswahn cc87c9b
docs: Crafted a command and options code example
christerswahn eb04bca
test: Updated test case
christerswahn 0803a59
test: Made successful test output quiet
christerswahn a36df1b
fix: Fixes for CodeRabbit comments
christerswahn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,120 @@ | ||
| import 'dart:async' show FutureOr; | ||
| import 'dart:io' show exit; | ||
|
|
||
| import 'package:args/command_runner.dart'; | ||
| import 'package:cli_tools/cli_tools.dart'; | ||
| import 'package:cli_tools/config.dart'; | ||
|
|
||
| void main(List<String> args) async { | ||
| var commandRunner = BetterCommandRunner( | ||
| 'example', | ||
| 'Example CLI command', | ||
| globalOptions: [ | ||
| StandardGlobalOption.quiet, | ||
| StandardGlobalOption.verbose, | ||
| ], | ||
| ); | ||
| commandRunner.addCommand(TimeSeriesCommand()); | ||
|
|
||
| try { | ||
| await commandRunner.run(args); | ||
| } on UsageException catch (e) { | ||
| print(e); | ||
| exit(1); | ||
| } | ||
|
|
||
| void main() async { | ||
| /// Simple example of using the [StdOutLogger] class. | ||
| var logger = StdOutLogger(LogLevel.info); | ||
| final LogLevel logLevel; | ||
| if (commandRunner.globalConfiguration.value(StandardGlobalOption.verbose)) { | ||
| logLevel = LogLevel.debug; | ||
| } else if (commandRunner.globalConfiguration | ||
| .value(StandardGlobalOption.quiet)) { | ||
| logLevel = LogLevel.error; | ||
| } else { | ||
| logLevel = LogLevel.info; | ||
| } | ||
| var logger = StdOutLogger(logLevel); | ||
|
|
||
| logger.info('An info message'); | ||
| logger.error('An error message'); | ||
| logger.debug( | ||
| 'A debug message that will not be shown because log level is info', | ||
| 'A debug message that will not be shown unless --verbose is set', | ||
| ); | ||
| await logger.progress( | ||
| 'A progress message', | ||
| () async => Future.delayed(const Duration(seconds: 3), () => true), | ||
| ); | ||
| } | ||
|
|
||
| /// Options are defineable as enums as well as regular lists. | ||
| /// | ||
| /// The enum approach is more distinct and type safe. | ||
| /// The list approach is more dynamic and permits non-const initialization. | ||
| enum TimeSeriesOption<V> implements OptionDefinition<V> { | ||
| until(DateTimeOption( | ||
| argName: 'until', | ||
| envName: 'SERIES_UNTIL', // can also be specified as environment variable | ||
| fromDefault: _defaultUntil, | ||
| helpText: 'The end timestamp of the series', | ||
| )), | ||
| length(IntOption( | ||
| argName: 'length', | ||
| argAbbrev: 'l', | ||
| argPos: 0, // can also be specified as positional argument | ||
| helpText: 'The number of elements in the series', | ||
| min: 1, | ||
| max: 100, | ||
| group: _granularityGroup, | ||
| )), | ||
| interval(DurationOption( | ||
| argName: 'interval', | ||
| argAbbrev: 'i', | ||
| helpText: 'The interval between the series elements', | ||
| min: Duration(seconds: 1), | ||
| max: Duration(days: 1), | ||
| group: _granularityGroup, | ||
| )); | ||
|
|
||
| const TimeSeriesOption(this.option); | ||
|
|
||
| @override | ||
| final ConfigOptionBase<V> option; | ||
| } | ||
|
|
||
| /// Exactly one of the options in this group must be set. | ||
| const _granularityGroup = MutuallyExclusive( | ||
| 'Granularity', | ||
| mode: MutuallyExclusiveMode.mandatory, | ||
| ); | ||
|
|
||
| /// A function can be used as a const initializer. | ||
| DateTime _defaultUntil() => DateTime.now().add(const Duration(days: 1)); | ||
|
|
||
| class TimeSeriesCommand extends BetterCommand<TimeSeriesOption, void> { | ||
| TimeSeriesCommand() : super(options: TimeSeriesOption.values); | ||
|
|
||
| @override | ||
| String get name => 'series'; | ||
|
|
||
| @override | ||
| String get description => 'Generate a series of time stamps'; | ||
|
|
||
| @override | ||
| FutureOr<void>? runWithConfig(Configuration<TimeSeriesOption> commandConfig) { | ||
| var start = DateTime.now(); | ||
| var until = commandConfig.value(TimeSeriesOption.until); | ||
|
|
||
| // exactly one of these options is set | ||
| var length = commandConfig.optionalValue(TimeSeriesOption.length); | ||
| var interval = commandConfig.optionalValue(TimeSeriesOption.interval); | ||
| interval ??= (until.difference(start) ~/ length!); | ||
| if (interval < const Duration(milliseconds: 1)) { | ||
| interval = const Duration(milliseconds: 1); | ||
| } | ||
|
|
||
| while (start.isBefore(until)) { | ||
| print(start); | ||
| start = start.add(interval); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.