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 | 19 |
20 /// The options that have been defined for this parser. | 20 /// The options that have been defined for this parser. |
21 final Map<String, Option> options; | 21 final Map<String, Option> options; |
22 | 22 |
23 /// The commands that have been defined for this parser. | 23 /// The commands that have been defined for this parser. |
24 final Map<String, ArgParser> commands; | 24 final Map<String, ArgParser> commands; |
25 | 25 |
| 26 /// A list of the [Option]s in [options] intermingled with [String] |
| 27 /// separators. |
| 28 final _optionsAndSeparators = []; |
| 29 |
26 /// Whether or not this parser parses options that appear after non-option | 30 /// Whether or not this parser parses options that appear after non-option |
27 /// arguments. | 31 /// arguments. |
28 final bool allowTrailingOptions; | 32 final bool allowTrailingOptions; |
29 | 33 |
30 /// Creates a new ArgParser. | 34 /// Creates a new ArgParser. |
31 /// | 35 /// |
32 /// If [allowTrailingOptions] is set, the parser will continue parsing even | 36 /// If [allowTrailingOptions] is set, the parser will continue parsing even |
33 /// after it finds an argument that is neither an option nor a command. | 37 /// 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 | 38 /// This allows options to be specified after regular arguments. Defaults to |
35 /// `false`. | 39 /// `false`. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 107 |
104 // Make sure the abbreviation isn't too long or in use. | 108 // Make sure the abbreviation isn't too long or in use. |
105 if (abbr != null) { | 109 if (abbr != null) { |
106 var existing = findByAbbreviation(abbr); | 110 var existing = findByAbbreviation(abbr); |
107 if (existing != null) { | 111 if (existing != null) { |
108 throw new ArgumentError( | 112 throw new ArgumentError( |
109 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 113 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
110 } | 114 } |
111 } | 115 } |
112 | 116 |
113 _options[name] = newOption(name, abbr, help, valueHelp, allowed, | 117 var option = newOption(name, abbr, help, valueHelp, allowed, |
114 allowedHelp, defaultsTo, callback, type, | 118 allowedHelp, defaultsTo, callback, type, |
115 negatable: negatable, splitCommas: splitCommas, hide: hide); | 119 negatable: negatable, splitCommas: splitCommas, hide: hide); |
| 120 _options[name] = option; |
| 121 _optionsAndSeparators.add(option); |
| 122 } |
| 123 |
| 124 /// Adds a separator line to the usage. |
| 125 /// |
| 126 /// In the usage text for the parser, this will appear between any options |
| 127 /// added before this call and ones added after it. |
| 128 void addSeparator(String text) { |
| 129 _optionsAndSeparators.add(text); |
116 } | 130 } |
117 | 131 |
118 /// Parses [args], a list of command-line arguments, matches them against the | 132 /// Parses [args], a list of command-line arguments, matches them against the |
119 /// flags and options defined by this parser, and returns the result. | 133 /// flags and options defined by this parser, and returns the result. |
120 ArgResults parse(List<String> args) => | 134 ArgResults parse(List<String> args) => |
121 new Parser(null, this, args.toList(), null, null).parse(); | 135 new Parser(null, this, args.toList(), null, null).parse(); |
122 | 136 |
123 /// Generates a string displaying usage information for the defined options. | 137 /// Generates a string displaying usage information for the defined options. |
124 /// | 138 /// |
125 /// This is basically the help text shown on the command line. | 139 /// This is basically the help text shown on the command line. |
126 @Deprecated("Replaced with get usage. getUsage() will be removed in args 1.0") | 140 @Deprecated("Replaced with get usage. getUsage() will be removed in args 1.0") |
127 String getUsage() => new Usage(this).generate(); | 141 String getUsage() => usage; |
128 | 142 |
129 /// Generates a string displaying usage information for the defined options. | 143 /// Generates a string displaying usage information for the defined options. |
130 /// | 144 /// |
131 /// This is basically the help text shown on the command line. | 145 /// This is basically the help text shown on the command line. |
132 String get usage => new Usage(this).generate(); | 146 String get usage => new Usage(_optionsAndSeparators).generate(); |
133 | 147 |
134 /// Get the default value for an option. Useful after parsing to test if the | 148 /// Get the default value for an option. Useful after parsing to test if the |
135 /// user specified something other than the default. | 149 /// user specified something other than the default. |
136 getDefault(String option) { | 150 getDefault(String option) { |
137 if (!options.containsKey(option)) { | 151 if (!options.containsKey(option)) { |
138 throw new ArgumentError('No option named $option'); | 152 throw new ArgumentError('No option named $option'); |
139 } | 153 } |
140 return options[option].defaultValue; | 154 return options[option].defaultValue; |
141 } | 155 } |
142 | 156 |
143 /// Finds the option whose abbreviation is [abbr], or `null` if no option has | 157 /// Finds the option whose abbreviation is [abbr], or `null` if no option has |
144 /// that abbreviation. | 158 /// that abbreviation. |
145 Option findByAbbreviation(String abbr) { | 159 Option findByAbbreviation(String abbr) { |
146 return options.values.firstWhere((option) => option.abbreviation == abbr, | 160 return options.values.firstWhere((option) => option.abbreviation == abbr, |
147 orElse: () => null); | 161 orElse: () => null); |
148 } | 162 } |
149 } | 163 } |
OLD | NEW |