args icon indicating copy to clipboard operation
args copied to clipboard

Statically typed argument parser (possibly with code generator)?

Open fzyzcjy opened this issue 3 years ago • 4 comments

Hi thanks for this helpful library! However, it is quite boilerplate to repeat code like:

class CommandOne extends Command<void> {
...
  CommandOne() {
    argParser.addOption('firstArgument');
    argParser.addOption('secondArgument');
  }

  @override
  Future<void> run() async {
    final firstArgument = argResults!['firstArgument'] as String;
    final secondArgument = argResults!['secondArgument']!.map((e) => int.parse(e)).toList();
...
  }
...
}

Since args package is like the Python argparse package, I am hoping it can be enhanced just like how https://typer.tiangolo.com/ Typer does.

In short, the ideal API may be:

class CommandOneArg {
  String firstArgument;
  List<int> secondArgument;
}

class CommandOne extends Command<CommandOneArg> {
  @override
  Future<void> run() async {
    // just directly use `argResult` which is of type `CommandOneArg`
  }
...
}

fzyzcjy avatar Apr 27 '22 06:04 fzyzcjy

Interesting idea; I believe that @kevmoo may at one time have authored something like this.

Something like this could also be layered on top of the existing package:args - args wouldn't have to support it directly.

devoncarew avatar Apr 27 '22 20:04 devoncarew

See here! https://pub.dev/packages/build_cli

kevmoo avatar Apr 27 '22 20:04 kevmoo

@devoncarew @kevmoo Thank you! But seems that it does not have commands support (like what I show in my post) yet...

fzyzcjy avatar Apr 28 '22 00:04 fzyzcjy