dart-lang / args

A command-line argument parsing library for Dart.
https://pub.dev/packages/args
BSD 3-Clause "New" or "Revised" License
208 stars 61 forks source link

Statically typed argument parser (possibly with code generator)? #217

Open fzyzcjy opened 2 years ago

fzyzcjy commented 2 years ago

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`
  }
...
}
devoncarew commented 2 years ago

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.

kevmoo commented 2 years ago

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

fzyzcjy commented 2 years ago

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

alexeyinkin commented 1 week ago

I made it with a macro: https://pub.dev/packages/args_macro