args: define args using dart method signature #117
Labels
closed-not-planned
Closed as we don't intend to take action on the reported issue
package:args
type-enhancement
A request for a change that isn't a bug
<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="96" height="96"hspace="10"> Issue by seaneagan
Originally opened as dart-lang/sdk#12272
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: