mbk
mbk

Reputation: 13

Why are my method cascades not working in this dart code snippet?

background: so I just started playing around with dart, as a small test I decided to try implementing the unix 'tr' program. I used stagehand to create a console-simple framework. the first thing I did was to get cli options with the args package:

import 'dart:io';
import 'package:args/args.dart';
import 'package:translate/translate.dart' as translate;

ArgResults input;

main(List<String> arguments) {

  var parser = new ArgParser();
    parser.addFlag("complement",
                   help: "complement the strings",
                   negatable: false,
                   abbr: "c");
    parser.addFlag("delete",
                   help: "delete the selected strings",
                   negatable: false,
                   abbr: "d");
    parser.addFlag("help",
                   help: "display this help screen",
                   negatable: false,
                   abbr: "h",
                   callback: (help) => print(parser.usage));
    parser.addFlag("squeeze-repeats",
                   help: "squeeze repeats",
                   negatable: false,
                   abbr: "s");
    parser.addFlag("truncate",
                   help: "truncate strings",
                   negatable: false,
                   abbr: "t");
    parser.addFlag("version",
                   help: "display version info",
                   negatable: false,
                   abbr: "v");

  input = parser.parse(arguments);
}

now, this works (at least, running "dart main.dart -h" will successfully print out the help items). my problem begins when I try to utilize method cascading. the code below gives the error:

error: line 13 pos 7: initializer of 'parser' may not refer to itself

import 'dart:io';
import 'package:args/args.dart';
import 'package:translate/translate.dart' as translate;

ArgResults input;

main(List<String> arguments) {

  var parser = new ArgParser()
    ..addFlag("complement",
              help: "complement the strings",
              negatable: false,
              abbr: "c")
    ..addFlag("delete",
              help: "delete the selected strings",
              negatable: false,
              abbr: "d")
    ..addFlag("help",
              help: "display this help screen",
              negatable: false,
              abbr: "h",
              callback: (help) => print(parser.usage))
    ..addFlag("squeeze-repeats",
              help: "squeeze repeats",
              negatable: false,
              abbr: "s")
    ..addFlag("truncate",
              help: "truncate strings",
              negatable: false,
              abbr: "t")
    ..addFlag("version",
              help: "display version info",
              negatable: false,
              abbr: "v");

  input = parser.parse(arguments);
}

the question is what am I doing wrong? is it as simple as a syntax error that I just can't see? for what it's worth, I am using bash/vi as my ide, I briefly considered using webstorm but since it would not be free I can't do it. I appreciate whatever help is given.

Upvotes: 0

Views: 433

Answers (1)

stwupton
stwupton

Reputation: 2235

Because you are adding the method cascades before the parser variable has been fully initialised. So you cannot reference it here;

callback: (help) => print(parser.usage))
                          ^

Changing it to;

var parser = new ArgParser();
parser
  ..addFlag("complement",
          help: "complement the strings",
          negatable: false,
          abbr: "c")
  ..addFlag("delete",
          help: "delete the selected strings",
          negatable: false,
          abbr: "d")
  ..addFlag("help",
          help: "display this help screen",
          negatable: false,
          abbr: "h",
          callback: (help) => print(parser.usage))
  ..addFlag("squeeze-repeats",
          help: "squeeze repeats",
          negatable: false,
          abbr: "s")
  ..addFlag("truncate",
          help: "truncate strings",
          negatable: false,
          abbr: "t")
  ..addFlag("version",
          help: "display version info",
          negatable: false,
          abbr: "v");

input = parser.parse(arguments);

...should fix it.

Upvotes: 6

Related Questions