为什么我的方法级联不能在这个dart代码片段中工作?

时间:2017-02-25 19:21:57

标签: dart

背景:所以我刚开始玩dart,作为一个小测试,我决定尝试实现unix'tr'程序。我使用stagehand来创建一个控制台简单的框架。我做的第一件事是使用args包获得cli选项:

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);
}

现在,这是有效的(至少,运行“dart main.dart -h”将成功打印出帮助项目)。当我尝试使用方法级联时,我的问题就开始了。下面的代码给出了错误:

错误:第13行pos 7:'parser'的初始化程序可能不会引用自身

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);
}

问题是我做错了什么?它是一个简单的语法错误,我只是看不到?为了它的价值,我使用bash / vi作为我的想法,我简单地考虑使用webstorm,但因为它不是免费的我不能这样做。我很感激所给予的任何帮助。

1 个答案:

答案 0 :(得分:5)

因为您在parser变量完全初始化之前添加了方法级联。所以你不能在这里引用它;

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

将其更改为;

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);

......应该解决它。