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; | |
6 | |
7 import 'dart:collection'; | 5 import 'dart:collection'; |
8 | 6 |
9 import 'arg_results.dart'; | 7 import 'arg_results.dart'; |
10 import 'option.dart'; | 8 import 'option.dart'; |
11 import 'parser.dart'; | 9 import 'parser.dart'; |
12 import 'usage.dart'; | 10 import 'usage.dart'; |
13 | 11 |
14 /// A class for taking a list of raw command line arguments and parsing out | 12 /// A class for taking a list of raw command line arguments and parsing out |
15 /// options and flags from them. | 13 /// options and flags from them. |
16 class ArgParser { | 14 class ArgParser { |
(...skipping 13 matching lines...) Expand all Loading... |
30 /// Whether or not this parser parses options that appear after non-option | 28 /// Whether or not this parser parses options that appear after non-option |
31 /// arguments. | 29 /// arguments. |
32 final bool allowTrailingOptions; | 30 final bool allowTrailingOptions; |
33 | 31 |
34 /// Creates a new ArgParser. | 32 /// Creates a new ArgParser. |
35 /// | 33 /// |
36 /// If [allowTrailingOptions] is set, the parser will continue parsing even | 34 /// If [allowTrailingOptions] is set, the parser will continue parsing even |
37 /// after it finds an argument that is neither an option nor a command. | 35 /// after it finds an argument that is neither an option nor a command. |
38 /// This allows options to be specified after regular arguments. Defaults to | 36 /// This allows options to be specified after regular arguments. Defaults to |
39 /// `false`. | 37 /// `false`. |
40 factory ArgParser({bool allowTrailingOptions}) => new ArgParser._( | 38 factory ArgParser({bool allowTrailingOptions: false}) => new ArgParser._( |
41 <String, Option>{}, <String, ArgParser>{}, | 39 <String, Option>{}, <String, ArgParser>{}, |
42 allowTrailingOptions: allowTrailingOptions); | 40 allowTrailingOptions: allowTrailingOptions); |
43 | 41 |
44 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, | 42 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands, |
45 {bool allowTrailingOptions}) | 43 {bool allowTrailingOptions: false}) |
46 : this._options = options, | 44 : this._options = options, |
47 this.options = new UnmodifiableMapView(options), | 45 this.options = new UnmodifiableMapView(options), |
48 this._commands = commands, | 46 this._commands = commands, |
49 this.commands = new UnmodifiableMapView(commands), | 47 this.commands = new UnmodifiableMapView(commands), |
50 this.allowTrailingOptions = allowTrailingOptions != null | 48 this.allowTrailingOptions = allowTrailingOptions != null |
51 ? allowTrailingOptions | 49 ? allowTrailingOptions |
52 : false; | 50 : false; |
53 | 51 |
54 /// Defines a command. | 52 /// Defines a command. |
55 /// | 53 /// |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 /// | 123 /// |
126 /// In the usage text for the parser, this will appear between any options | 124 /// In the usage text for the parser, this will appear between any options |
127 /// added before this call and ones added after it. | 125 /// added before this call and ones added after it. |
128 void addSeparator(String text) { | 126 void addSeparator(String text) { |
129 _optionsAndSeparators.add(text); | 127 _optionsAndSeparators.add(text); |
130 } | 128 } |
131 | 129 |
132 /// Parses [args], a list of command-line arguments, matches them against the | 130 /// Parses [args], a list of command-line arguments, matches them against the |
133 /// flags and options defined by this parser, and returns the result. | 131 /// flags and options defined by this parser, and returns the result. |
134 ArgResults parse(List<String> args) => | 132 ArgResults parse(List<String> args) => |
135 new Parser(null, this, args.toList(), null, null).parse(); | 133 new Parser(null, this, args.toList()).parse(); |
136 | 134 |
137 /// Generates a string displaying usage information for the defined options. | 135 /// Generates a string displaying usage information for the defined options. |
138 /// | 136 /// |
139 /// This is basically the help text shown on the command line. | 137 /// This is basically the help text shown on the command line. |
140 @Deprecated("Replaced with get usage. getUsage() will be removed in args 1.0") | 138 @Deprecated("Replaced with get usage. getUsage() will be removed in args 1.0") |
141 String getUsage() => usage; | 139 String getUsage() => usage; |
142 | 140 |
143 /// Generates a string displaying usage information for the defined options. | 141 /// Generates a string displaying usage information for the defined options. |
144 /// | 142 /// |
145 /// This is basically the help text shown on the command line. | 143 /// This is basically the help text shown on the command line. |
146 String get usage => new Usage(_optionsAndSeparators).generate(); | 144 String get usage => new Usage(_optionsAndSeparators).generate(); |
147 | 145 |
148 /// Get the default value for an option. Useful after parsing to test if the | 146 /// Get the default value for an option. Useful after parsing to test if the |
149 /// user specified something other than the default. | 147 /// user specified something other than the default. |
150 getDefault(String option) { | 148 getDefault(String option) { |
151 if (!options.containsKey(option)) { | 149 if (!options.containsKey(option)) { |
152 throw new ArgumentError('No option named $option'); | 150 throw new ArgumentError('No option named $option'); |
153 } | 151 } |
154 return options[option].defaultValue; | 152 return options[option].defaultValue; |
155 } | 153 } |
156 | 154 |
157 /// Finds the option whose abbreviation is [abbr], or `null` if no option has | 155 /// Finds the option whose abbreviation is [abbr], or `null` if no option has |
158 /// that abbreviation. | 156 /// that abbreviation. |
159 Option findByAbbreviation(String abbr) { | 157 Option findByAbbreviation(String abbr) { |
160 return options.values.firstWhere((option) => option.abbreviation == abbr, | 158 return options.values.firstWhere((option) => option.abbreviation == abbr, |
161 orElse: () => null); | 159 orElse: () => null); |
162 } | 160 } |
163 } | 161 } |
OLD | NEW |