1
votes

Using the example here: http://www.dartdocs.org/documentation/args/0.13.0/index.html

I'm trying to write a command line app, but I get NoSuchMethodError: method not found: 'options' when I try to use this example code. Why isn't options defined for the Command subclass instance? If I'm not supposed to use options then what am I supposed to use to access the options passed to this command?

Here's my code which also fails:

class ShuffleCommand extends Command {

  final name = "shuffle";
  final description = "Shuffle and choose cards from a deck";

  ShuffleCommand() {
    argParser
      ..addFlag('count', abbr: 'c', defaultsTo: "1")
      ..addOption('deck');
  }

  void run() {
    print(options);
  }
} 
1

1 Answers

1
votes

I guess the example should look like

import 'package:args/args.dart';
import 'package:args/command_runner.dart';

void main(List<String> args) {
  var parser = new ArgParser();

  var command = new ArgParser();
  parser.addCommand('shuffle', command);

  var runner = new CommandRunner('shuffle', '')
    ..addCommand(new ShuffleCommand())
    ..run(args);

}

class ShuffleCommand extends Command {

  final name = "shuffle";
  final description = "Shuffle and choose cards from a deck";

  ShuffleCommand() {
    argParser
      ..addFlag('count', abbr: 'c', defaultsTo: true)
      ..addOption('deck');
  }

  void run() {
    print(argParser.options);
  }
}

when you run it with dart main.dart shuffle, run() is executed and the options printed out.