OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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; | 5 library args; |
6 | 6 |
7 import 'package:collection/wrappers.dart'; | 7 export 'src/arg_parser.dart'; |
8 | 8 export 'src/arg_results.dart' hide newArgResults; |
9 import 'src/parser.dart'; | 9 export 'src/option.dart' hide newOption; |
10 import 'src/usage.dart'; | |
11 import 'src/options.dart'; | |
12 export 'src/options.dart'; | |
13 | |
14 /// A class for taking a list of raw command line arguments and parsing out | |
15 /// options and flags from them. | |
16 class ArgParser { | |
17 final Map<String, Option> _options; | |
18 final Map<String, ArgParser> _commands; | |
19 | |
20 /// The options that have been defined for this parser. | |
21 final Map<String, Option> options; | |
22 | |
23 /// The commands that have been defined for this parser. | |
24 final Map<String, ArgParser> commands; | |
25 | |
26 /// Whether or not this parser parses options that appear after non-option | |
27 /// arguments. | |
28 final bool allowTrailingOptions; | |
29 | |
30 /// Creates a new ArgParser. | |
31 /// | |
32 /// If [allowTrailingOptions] is set, the parser will continue parsing even | |
33 /// 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 | |
35 /// `false`. | |
36 factory ArgParser({bool allowTrailingOptions}) => | |
37 new ArgParser._(<String, Option>{}, <String, ArgParser>{}, | |
38 allowTrailingOptions: allowTrailingOptions); | |
39 | |
40 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, | |
41 {bool allowTrailingOptions}) : | |
42 this._options = options, | |
43 this.options = new UnmodifiableMapView(options), | |
44 this._commands = commands, | |
45 this.commands = new UnmodifiableMapView(commands), | |
46 this.allowTrailingOptions = allowTrailingOptions != null ? | |
47 allowTrailingOptions : false; | |
48 | |
49 /// Defines a command. | |
50 /// | |
51 /// A command is a named argument which may in turn define its own options and | |
52 /// subcommands using the given parser. If [parser] is omitted, implicitly | |
53 /// creates a new one. Returns the parser for the command. | |
54 ArgParser addCommand(String name, [ArgParser parser]) { | |
55 // Make sure the name isn't in use. | |
56 if (_commands.containsKey(name)) { | |
57 throw new ArgumentError('Duplicate command "$name".'); | |
58 } | |
59 | |
60 if (parser == null) parser = new ArgParser(); | |
61 _commands[name] = parser; | |
62 return parser; | |
63 } | |
64 | |
65 /// Defines a flag. Throws an [ArgumentError] if: | |
66 /// | |
67 /// * There is already an option named [name]. | |
68 /// * There is already an option using abbreviation [abbr]. | |
69 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, | |
70 bool negatable: true, void callback(bool value), bool hide: false}) { | |
71 _addOption(name, abbr, help, null, null, null, defaultsTo, callback, | |
72 isFlag: true, negatable: negatable, hide: hide); | |
73 } | |
74 | |
75 /// Defines a value-taking option. Throws an [ArgumentError] if: | |
76 /// | |
77 /// * There is already an option with name [name]. | |
78 /// * There is already an option using abbreviation [abbr]. | |
79 void addOption(String name, {String abbr, String help, String valueHelp, | |
80 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo, | |
81 void callback(value), bool allowMultiple: false, bool hide: false}) { | |
82 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, | |
83 callback, isFlag: false, allowMultiple: allowMultiple, | |
84 hide: hide); | |
85 } | |
86 | |
87 void _addOption(String name, String abbr, String help, String valueHelp, | |
88 List<String> allowed, Map<String, String> allowedHelp, defaultsTo, | |
89 void callback(value), {bool isFlag, bool negatable: false, | |
90 bool allowMultiple: false, bool hide: false}) { | |
91 // Make sure the name isn't in use. | |
92 if (_options.containsKey(name)) { | |
93 throw new ArgumentError('Duplicate option "$name".'); | |
94 } | |
95 | |
96 // Make sure the abbreviation isn't too long or in use. | |
97 if (abbr != null) { | |
98 var existing = findByAbbreviation(abbr); | |
99 if (existing != null) { | |
100 throw new ArgumentError( | |
101 'Abbreviation "$abbr" is already used by "${existing.name}".'); | |
102 } | |
103 } | |
104 | |
105 _options[name] = new Option(name, abbr, help, valueHelp, allowed, | |
106 allowedHelp, defaultsTo, callback, isFlag: isFlag, negatable: negatable, | |
107 allowMultiple: allowMultiple, hide: hide); | |
108 } | |
109 | |
110 /// Parses [args], a list of command-line arguments, matches them against the | |
111 /// flags and options defined by this parser, and returns the result. | |
112 ArgResults parse(List<String> args) => | |
113 new Parser(null, this, args.toList(), null, null).parse(); | |
114 | |
115 /// Generates a string displaying usage information for the defined options. | |
116 /// | |
117 /// This is basically the help text shown on the command line. | |
118 String getUsage() => new Usage(this).generate(); | |
119 | |
120 /// Get the default value for an option. Useful after parsing to test if the | |
121 /// user specified something other than the default. | |
122 getDefault(String option) { | |
123 if (!options.containsKey(option)) { | |
124 throw new ArgumentError('No option named $option'); | |
125 } | |
126 return options[option].defaultValue; | |
127 } | |
128 | |
129 /// Finds the option whose abbreviation is [abbr], or `null` if no option has | |
130 /// that abbreviation. | |
131 Option findByAbbreviation(String abbr) { | |
132 return options.values.firstWhere((option) => option.abbreviation == abbr, | |
133 orElse: () => null); | |
134 } | |
135 } | |
136 | |
137 /// The results of parsing a series of command line arguments using | |
138 /// [ArgParser.parse()]. | |
139 /// | |
140 /// Includes the parsed options and any remaining unparsed command line | |
141 /// arguments. | |
142 class ArgResults { | |
143 final Map<String, dynamic> _options; | |
144 | |
145 /// If these are the results for parsing a command's options, this will be the | |
146 /// name of the command. For top-level results, this returns `null`. | |
147 final String name; | |
148 | |
149 /// The command that was selected, or `null` if none was. | |
150 /// | |
151 /// This will contain the options that were selected for that command. | |
152 final ArgResults command; | |
153 | |
154 /// The remaining command-line arguments that were not parsed as options or | |
155 /// flags. | |
156 /// | |
157 /// If `--` was used to separate the options from the remaining arguments, | |
158 /// it will not be included in this list unless parsing stopped before the | |
159 /// `--` was reached. | |
160 final List<String> rest; | |
161 | |
162 /// Creates a new [ArgResults]. | |
163 ArgResults(this._options, this.name, this.command, List<String> rest) | |
164 : this.rest = new UnmodifiableListView(rest); | |
165 | |
166 /// Gets the parsed command-line option named [name]. | |
167 operator [](String name) { | |
168 if (!_options.containsKey(name)) { | |
169 throw new ArgumentError( | |
170 'Could not find an option named "$name".'); | |
171 } | |
172 | |
173 return _options[name]; | |
174 } | |
175 | |
176 /// Get the names of the options as an [Iterable]. | |
177 Iterable<String> get options => _options.keys; | |
178 } | |
179 | |
OLD | NEW |