OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library args.src.arg_parser; | 5 library args.src.arg_parser; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'arg_results.dart'; | 9 import 'arg_results.dart'; |
10 import 'option.dart'; | 10 import 'option.dart'; |
11 import 'parser.dart'; | 11 import 'parser.dart'; |
12 import 'usage.dart'; | 12 import 'usage.dart'; |
13 | 13 |
14 /// A class for taking a list of raw command line arguments and parsing out | 14 /// A class for taking a list of raw command line arguments and parsing out |
15 /// options and flags from them. | 15 /// options and flags from them. |
16 class ArgParser { | 16 class ArgParser { |
17 final Map<String, Option> _options; | 17 final Map<String, Option> _options; |
18 final Map<String, ArgParser> _commands; | 18 final Map<String, ArgParser> _commands; |
19 final List _optionsAndSeparators; | |
19 | 20 |
20 /// The options that have been defined for this parser. | 21 /// The options that have been defined for this parser. |
21 final Map<String, Option> options; | 22 final Map<String, Option> options; |
22 | 23 |
24 /// A list of the [Option]s in [options] intermingled with [String] | |
25 /// separators. | |
26 final List optionsAndSeparators; | |
Bob Nystrom
2015/05/21 16:43:16
This is kind of gross.
It might be time to consid
nweiz
2015/05/21 19:22:27
I thought about that, but Separators are really no
Bob Nystrom
2015/05/21 19:42:27
True. It might be possible to come up with a name
nweiz
2015/05/21 19:52:37
Done.
| |
27 | |
23 /// The commands that have been defined for this parser. | 28 /// The commands that have been defined for this parser. |
24 final Map<String, ArgParser> commands; | 29 final Map<String, ArgParser> commands; |
25 | 30 |
26 /// Whether or not this parser parses options that appear after non-option | 31 /// Whether or not this parser parses options that appear after non-option |
27 /// arguments. | 32 /// arguments. |
28 final bool allowTrailingOptions; | 33 final bool allowTrailingOptions; |
29 | 34 |
30 /// Creates a new ArgParser. | 35 /// Creates a new ArgParser. |
31 /// | 36 /// |
32 /// If [allowTrailingOptions] is set, the parser will continue parsing even | 37 /// If [allowTrailingOptions] is set, the parser will continue parsing even |
33 /// after it finds an argument that is neither an option nor a command. | 38 /// after it finds an argument that is neither an option nor a command. |
34 /// This allows options to be specified after regular arguments. Defaults to | 39 /// This allows options to be specified after regular arguments. Defaults to |
35 /// `false`. | 40 /// `false`. |
36 factory ArgParser({bool allowTrailingOptions}) => new ArgParser._( | 41 factory ArgParser({bool allowTrailingOptions}) => new ArgParser._( |
37 <String, Option>{}, <String, ArgParser>{}, | 42 <String, Option>{}, <String, ArgParser>{}, [], |
38 allowTrailingOptions: allowTrailingOptions); | 43 allowTrailingOptions: allowTrailingOptions); |
39 | 44 |
40 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, | 45 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, |
41 {bool allowTrailingOptions}) | 46 List optionsAndSeparators, {bool allowTrailingOptions}) |
42 : this._options = options, | 47 : this._options = options, |
43 this.options = new UnmodifiableMapView(options), | 48 this.options = new UnmodifiableMapView(options), |
44 this._commands = commands, | 49 this._commands = commands, |
45 this.commands = new UnmodifiableMapView(commands), | 50 this.commands = new UnmodifiableMapView(commands), |
51 this._optionsAndSeparators = optionsAndSeparators, | |
52 this.optionsAndSeparators = new UnmodifiableListView(optionsAndSeparator s), | |
Bob Nystrom
2015/05/21 16:43:16
Long line.
nweiz
2015/05/21 19:22:27
Done.
| |
46 this.allowTrailingOptions = allowTrailingOptions != null | 53 this.allowTrailingOptions = allowTrailingOptions != null |
47 ? allowTrailingOptions | 54 ? allowTrailingOptions |
48 : false; | 55 : false; |
49 | 56 |
50 /// Defines a command. | 57 /// Defines a command. |
51 /// | 58 /// |
52 /// A command is a named argument which may in turn define its own options and | 59 /// A command is a named argument which may in turn define its own options and |
53 /// subcommands using the given parser. If [parser] is omitted, implicitly | 60 /// subcommands using the given parser. If [parser] is omitted, implicitly |
54 /// creates a new one. Returns the parser for the command. | 61 /// creates a new one. Returns the parser for the command. |
55 ArgParser addCommand(String name, [ArgParser parser]) { | 62 ArgParser addCommand(String name, [ArgParser parser]) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 | 110 |
104 // Make sure the abbreviation isn't too long or in use. | 111 // Make sure the abbreviation isn't too long or in use. |
105 if (abbr != null) { | 112 if (abbr != null) { |
106 var existing = findByAbbreviation(abbr); | 113 var existing = findByAbbreviation(abbr); |
107 if (existing != null) { | 114 if (existing != null) { |
108 throw new ArgumentError( | 115 throw new ArgumentError( |
109 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 116 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
110 } | 117 } |
111 } | 118 } |
112 | 119 |
113 _options[name] = newOption(name, abbr, help, valueHelp, allowed, | 120 var option = newOption(name, abbr, help, valueHelp, allowed, |
114 allowedHelp, defaultsTo, callback, type, | 121 allowedHelp, defaultsTo, callback, type, |
115 negatable: negatable, splitCommas: splitCommas, hide: hide); | 122 negatable: negatable, splitCommas: splitCommas, hide: hide); |
123 _options[name] = option; | |
124 _optionsAndSeparators.add(option); | |
125 } | |
126 | |
127 /// Adds a separator line to the usage. | |
128 /// | |
129 /// The separator goes between the options that have added before this is | |
130 /// called and the options that are added after. This only the arg parser's | |
131 /// usage text, not its parsing. Separators can be used to group options | |
132 /// together visually. | |
Bob Nystrom
2015/05/21 16:43:16
This paragraph is kind of choppy. How about just:
nweiz
2015/05/21 19:22:27
Done.
| |
133 void addSeparator(String text) { | |
134 _optionsAndSeparators.add(text); | |
116 } | 135 } |
117 | 136 |
118 /// Parses [args], a list of command-line arguments, matches them against the | 137 /// Parses [args], a list of command-line arguments, matches them against the |
119 /// flags and options defined by this parser, and returns the result. | 138 /// flags and options defined by this parser, and returns the result. |
120 ArgResults parse(List<String> args) => | 139 ArgResults parse(List<String> args) => |
121 new Parser(null, this, args.toList(), null, null).parse(); | 140 new Parser(null, this, args.toList(), null, null).parse(); |
122 | 141 |
123 /// Generates a string displaying usage information for the defined options. | 142 /// Generates a string displaying usage information for the defined options. |
124 /// | 143 /// |
125 /// This is basically the help text shown on the command line. | 144 /// This is basically the help text shown on the command line. |
(...skipping 14 matching lines...) Expand all Loading... | |
140 return options[option].defaultValue; | 159 return options[option].defaultValue; |
141 } | 160 } |
142 | 161 |
143 /// Finds the option whose abbreviation is [abbr], or `null` if no option has | 162 /// Finds the option whose abbreviation is [abbr], or `null` if no option has |
144 /// that abbreviation. | 163 /// that abbreviation. |
145 Option findByAbbreviation(String abbr) { | 164 Option findByAbbreviation(String abbr) { |
146 return options.values.firstWhere((option) => option.abbreviation == abbr, | 165 return options.values.firstWhere((option) => option.abbreviation == abbr, |
147 orElse: () => null); | 166 orElse: () => null); |
148 } | 167 } |
149 } | 168 } |
OLD | NEW |