args: define args using dart method signature #12272
Labels
area-pkg
Used for miscellaneous pkg/ packages not associated with specific area- teams.
closed-not-planned
Closed as we don't intend to take action on the reported issue
type-enhancement
A request for a change that isn't a bug
This issue was originally filed by @seaneagan
The args of a command line script seem to map nicely to the args of a dart method. It would be nice to be able to define script args using the same techniques as dart methods, and be able to automatically forward script args to said method. Presumably both of these could be done using a mirror on the method.
Say we have a script called 'greet.dart':
import 'package:args/args.dart' as args;
main() => args.forward(_main);
greet(String salutation, String name, [String punctuation = '']) =>
print('$salutation $name$punctuation);
where _main could be defined using String positional arguments:
// example:
// some_script.dart John Hi
_main(String name, [String salutation = 'Hello']) =>
greet(salutation, name);
or String or bool named arguments:
// example:
// some_script.dart --salutation Hi --exclaim John
_main(String name, {String salutation: 'Hello ', bool exclaim: false}) =>
greet(salutation, name, exclaim ? '!' : '');
or with the trailing positional parameter being a List<String>:
// example:
// some_script.dart --salutation Hi Bob Alice
_main(List<String> rest, {String salutation: 'Hello '}) =>
greet(salutation, rest.join(', '));
If dart ever gets true rest arguments those could be used instead.
To add "help", comments on each arg would make sense, but that is against the dart style guide. For this and other arg metadata, dart's metadata could be used:
_main(
String name, {
@Arg(abbr: 's', 'How to greet them')
String salutation: 'Hello ',
@Arg(abbr: 'p', help: 'Whether to includ a "!"', allowedHelp: const {'': 'none', '!': 'exclamation'})
String punctuation = ''}) =>
greet(salutation, name, punctuation);
The text was updated successfully, but these errors were encountered: