| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * This library lets you define parsers for parsing raw command-line arguments | 6 * This library lets you define parsers for parsing raw command-line arguments |
| 7 * into a set of options and values using [GNU][] and [POSIX][] style options. | 7 * into a set of options and values using [GNU][] and [POSIX][] style options. |
| 8 * | 8 * |
| 9 * ## Defining options ## | 9 * ## Defining options ## |
| 10 * | 10 * |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 * by calling [ArgParser.parse()] with a set of arguments: | 70 * by calling [ArgParser.parse()] with a set of arguments: |
| 71 * | 71 * |
| 72 * var results = parser.parse(['some', 'command', 'line', 'args']); | 72 * var results = parser.parse(['some', 'command', 'line', 'args']); |
| 73 * | 73 * |
| 74 * These will usually come from `new Options().arguments`, but you can pass in | 74 * These will usually come from `new Options().arguments`, but you can pass in |
| 75 * any list of strings. It returns an instance of [ArgResults]. This is a | 75 * any list of strings. It returns an instance of [ArgResults]. This is a |
| 76 * map-like object that will return the value of any parsed option. | 76 * map-like object that will return the value of any parsed option. |
| 77 * | 77 * |
| 78 * var parser = new ArgParser(); | 78 * var parser = new ArgParser(); |
| 79 * parser.addOption('mode'); | 79 * parser.addOption('mode'); |
| 80 * parser.addFlag('verbose', defaultsTo: true); | 80 * parser.addFlag('verbose', abbr: 'v'); |
| 81 * var results = parser.parse('['--mode', 'debug', 'something', 'else']); | 81 * var results = parser.parse('['--mode', 'debug', 'skip', '-v', 'me']); |
| 82 * | 82 * |
| 83 * print(results['mode']); // debug | 83 * print(results['mode']); // debug |
| 84 * print(results['verbose']); // true | 84 * print(results['verbose']); // true |
| 85 * | 85 * |
| 86 * The [parse()] method will stop as soon as it reaches `--` or anything that | 86 * The [parse()] method will ignore any arguments it doesn't recognize and stop |
| 87 * it doesn't recognize as an option, flag, or option value. If there are still | 87 * if it reaches `--`. Any unhandled arguments will be provided to you in |
| 88 * arguments left, they will be provided to you in | |
| 89 * [ArgResults.rest]. | 88 * [ArgResults.rest]. |
| 90 * | 89 * |
| 91 * print(results.rest); // ['something', 'else'] | 90 * print(results.rest); // ['skip', 'me'] |
| 92 * | 91 * |
| 93 * ## Specifying options ## | 92 * ## Specifying options ## |
| 94 * | 93 * |
| 95 * To actually pass in options and flags on the command line, use GNU or POSIX | 94 * To actually pass in options and flags on the command line, use GNU or POSIX |
| 96 * style. If you define an option like: | 95 * style. If you define an option like: |
| 97 * | 96 * |
| 98 * parser.addOption('name', abbr: 'n'); | 97 * parser.addOption('name', abbr: 'n'); |
| 99 * | 98 * |
| 100 * Then a value for it can be specified on the command line using any of: | 99 * Then a value for it can be specified on the command line using any of: |
| 101 * | 100 * |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 * Will display something like: | 171 * Will display something like: |
| 173 * | 172 * |
| 174 * --mode The compiler configuration | 173 * --mode The compiler configuration |
| 175 * [debug, release] | 174 * [debug, release] |
| 176 * | 175 * |
| 177 * --[no-]verbose Show additional diagnostic info | 176 * --[no-]verbose Show additional diagnostic info |
| 178 * --arch The architecture to compile for | 177 * --arch The architecture to compile for |
| 179 * | 178 * |
| 180 * [arm] ARM Holding 32-bit chip | 179 * [arm] ARM Holding 32-bit chip |
| 181 * [ia32] Intel x86 | 180 * [ia32] Intel x86 |
| 182 * | 181 * |
| 183 * To assist the formatting of the usage help, single line help text will | 182 * To assist the formatting of the usage help, single line help text will |
| 184 * be followed by a single new line. Options with multi-line help text | 183 * be followed by a single new line. Options with multi-line help text |
| 185 * will be followed by two new lines. This provides spatial diversity between | 184 * will be followed by two new lines. This provides spatial diversity between |
| 186 * options. | 185 * options. |
| 187 * | 186 * |
| 188 * [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.h
tml#tag_12_02 | 187 * [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.h
tml#tag_12_02 |
| 189 * [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Inte
rfaces | 188 * [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Inte
rfaces |
| 190 */ | 189 */ |
| 191 library args; | 190 library args; |
| 192 | 191 |
| 193 import 'dart:math'; | 192 import 'dart:math'; |
| 194 | 193 |
| 195 // TODO(rnystrom): Use "package:" URL here when test.dart can handle pub. | |
| 196 import 'src/utils.dart'; | 194 import 'src/utils.dart'; |
| 197 | 195 |
| 198 /** | 196 /** |
| 199 * A class for taking a list of raw command line arguments and parsing out | 197 * A class for taking a list of raw command line arguments and parsing out |
| 200 * options and flags from them. | 198 * options and flags from them. |
| 201 */ | 199 */ |
| 202 class ArgParser { | 200 class ArgParser { |
| 203 static final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); | 201 static final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); |
| 204 static final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); | 202 static final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); |
| 205 static final _LONG_OPT = new RegExp(r'^--([a-zA-Z\-_0-9]+)(=(.*))?$'); | 203 static final _LONG_OPT = new RegExp(r'^--([a-zA-Z\-_0-9]+)(=(.*))?$'); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 } | 277 } |
| 280 | 278 |
| 281 /** | 279 /** |
| 282 * Parses [args], a list of command-line arguments, matches them against the | 280 * Parses [args], a list of command-line arguments, matches them against the |
| 283 * flags and options defined by this parser, and returns the result. | 281 * flags and options defined by this parser, and returns the result. |
| 284 */ | 282 */ |
| 285 ArgResults parse(List<String> args) { | 283 ArgResults parse(List<String> args) { |
| 286 _args = args; | 284 _args = args; |
| 287 _current = 0; | 285 _current = 0; |
| 288 var results = {}; | 286 var results = {}; |
| 287 var rest = []; |
| 289 | 288 |
| 290 // Initialize flags to their defaults. | 289 // Initialize flags to their defaults. |
| 291 _options.forEach((name, option) { | 290 _options.forEach((name, option) { |
| 292 if (option.allowMultiple) { | 291 if (option.allowMultiple) { |
| 293 results[name] = []; | 292 results[name] = []; |
| 294 } else { | 293 } else { |
| 295 results[name] = option.defaultValue; | 294 results[name] = option.defaultValue; |
| 296 } | 295 } |
| 297 }); | 296 }); |
| 298 | 297 |
| 299 // Parse the args. | 298 // Parse the args. |
| 300 for (_current = 0; _current < args.length; _current++) { | 299 for (_current = 0; _current < args.length; _current++) { |
| 301 var arg = args[_current]; | 300 var arg = args[_current]; |
| 302 | 301 |
| 303 if (arg == '--') { | 302 if (arg == '--') { |
| 304 // Reached the argument terminator, so stop here. | 303 // Reached the argument terminator, so stop here. |
| 305 _current++; | 304 _current++; |
| 306 break; | 305 break; |
| 307 } | 306 } |
| 308 | 307 |
| 309 // Try to parse the current argument as an option. Note that the order | 308 // Try to parse the current argument as an option. Note that the order |
| 310 // here matters. | 309 // here matters. |
| 311 if (_parseSoloOption(results)) continue; | 310 if (_parseSoloOption(results)) continue; |
| 312 if (_parseAbbreviation(results)) continue; | 311 if (_parseAbbreviation(results)) continue; |
| 313 if (_parseLongOption(results)) continue; | 312 if (_parseLongOption(results)) continue; |
| 314 | 313 |
| 315 // If we got here, the argument doesn't look like an option, so stop. | 314 // If we got here, the argument doesn't look like an option, so ignore it. |
| 316 break; | 315 rest.add(arg); |
| 317 } | 316 } |
| 318 | 317 |
| 319 // Set unspecified multivalued arguments to their default value, | 318 // Set unspecified multivalued arguments to their default value, |
| 320 // if any, and invoke the callbacks. | 319 // if any, and invoke the callbacks. |
| 321 for (var name in _optionNames) { | 320 for (var name in _optionNames) { |
| 322 var option = _options[name]; | 321 var option = _options[name]; |
| 323 if (option.allowMultiple && | 322 if (option.allowMultiple && |
| 324 results[name].length == 0 && | 323 results[name].length == 0 && |
| 325 option.defaultValue != null) { | 324 option.defaultValue != null) { |
| 326 results[name].add(option.defaultValue); | 325 results[name].add(option.defaultValue); |
| 327 } | 326 } |
| 328 if (option.callback != null) option.callback(results[name]); | 327 if (option.callback != null) option.callback(results[name]); |
| 329 } | 328 } |
| 330 | 329 |
| 331 // Add in the leftover arguments we didn't parse. | 330 // Add in the leftover arguments we didn't parse. |
| 332 return new ArgResults(results, | 331 rest.addAll(_args.getRange(_current, _args.length - _current)); |
| 333 _args.getRange(_current, _args.length - _current)); | 332 |
| 333 return new ArgResults(results, rest); |
| 334 } | 334 } |
| 335 | 335 |
| 336 /** | 336 /** |
| 337 * Generates a string displaying usage information for the defined options. | 337 * Generates a string displaying usage information for the defined options. |
| 338 * This is basically the help text shown on the command line. | 338 * This is basically the help text shown on the command line. |
| 339 */ | 339 */ |
| 340 String getUsage() { | 340 String getUsage() { |
| 341 return new _Usage(this).generate(); | 341 return new _Usage(this).generate(); |
| 342 } | 342 } |
| 343 | 343 |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 allowedBuffer.add(allowed); | 776 allowedBuffer.add(allowed); |
| 777 if (allowed == option.defaultValue) { | 777 if (allowed == option.defaultValue) { |
| 778 allowedBuffer.add(' (default)'); | 778 allowedBuffer.add(' (default)'); |
| 779 } | 779 } |
| 780 first = false; | 780 first = false; |
| 781 } | 781 } |
| 782 allowedBuffer.add(']'); | 782 allowedBuffer.add(']'); |
| 783 return allowedBuffer.toString(); | 783 return allowedBuffer.toString(); |
| 784 } | 784 } |
| 785 } | 785 } |
| OLD | NEW |