Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: pkg/args/lib/src/parser.dart

Issue 12545013: Added the continueParsing option to ArgParser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.src.parser; 5 library args.src.parser;
6 6
7 import '../args.dart'; 7 import '../args.dart';
8 8
9 final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); 9 final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$');
10 final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); 10 final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$');
(...skipping 24 matching lines...) Expand all
35 final List<String> args; 35 final List<String> args;
36 36
37 /** The accumulated parsed options. */ 37 /** The accumulated parsed options. */
38 final Map results = {}; 38 final Map results = {};
39 39
40 Parser(this.commandName, this.grammar, this.args, [this.parent]); 40 Parser(this.commandName, this.grammar, this.args, [this.parent]);
41 41
42 /** The current argument being parsed. */ 42 /** The current argument being parsed. */
43 String get current => args[0]; 43 String get current => args[0];
44 44
45 /** Parses the arguments. This can only be called once. */ 45 /**
46 ArgResults parse() { 46 * Parses the arguments. This can only be called once.
47 *
48 * If [continueParsing] is set, the parser will continue parsing even after it
49 * finds an argument that is not an option. This allows you to specify options
50 * after your command parameters.
51 */
52 ArgResults parse({continueParsing: false}) {
47 var commandResults = null; 53 var commandResults = null;
54 var rest = [];
48 55
49 // Initialize flags to their defaults. 56 // Initialize flags to their defaults.
50 grammar.options.forEach((name, option) { 57 grammar.options.forEach((name, option) {
51 if (option.allowMultiple) { 58 if (option.allowMultiple) {
52 results[name] = []; 59 results[name] = [];
53 } else { 60 } else {
54 results[name] = option.defaultValue; 61 results[name] = option.defaultValue;
55 } 62 }
56 }); 63 });
57 64
(...skipping 14 matching lines...) Expand all
72 commandResults = commandParser.parse(); 79 commandResults = commandParser.parse();
73 continue; 80 continue;
74 } 81 }
75 82
76 // Try to parse the current argument as an option. Note that the order 83 // Try to parse the current argument as an option. Note that the order
77 // here matters. 84 // here matters.
78 if (parseSoloOption()) continue; 85 if (parseSoloOption()) continue;
79 if (parseAbbreviation(this)) continue; 86 if (parseAbbreviation(this)) continue;
80 if (parseLongOption()) continue; 87 if (parseLongOption()) continue;
81 88
82 // If we got here, the argument doesn't look like an option, so stop. 89 if (!continueParsing) {
83 break; 90 // If we got here, the argument doesn't look like an option, so stop.
91 break;
92 } else {
93 rest.add(args.removeAt(0));
94 }
84 } 95 }
85 96
86 // Set unspecified multivalued arguments to their default value, 97 // Set unspecified multivalued arguments to their default value,
87 // if any, and invoke the callbacks. 98 // if any, and invoke the callbacks.
88 grammar.options.forEach((name, option) { 99 grammar.options.forEach((name, option) {
89 if (option.allowMultiple && 100 if (option.allowMultiple &&
90 results[name].length == 0 && 101 results[name].length == 0 &&
91 option.defaultValue != null) { 102 option.defaultValue != null) {
92 results[name].add(option.defaultValue); 103 results[name].add(option.defaultValue);
93 } 104 }
94 if (option.callback != null) option.callback(results[name]); 105 if (option.callback != null) option.callback(results[name]);
95 }); 106 });
96 107
97 // Add in the leftover arguments we didn't parse to the innermost command. 108 // Add in the leftover arguments we didn't parse to the innermost command.
98 var rest = args.toList(); 109 rest.addAll(args.toList());
99 args.clear(); 110 args.clear();
100 return new ArgResults(results, commandName, commandResults, rest); 111 return new ArgResults(results, commandName, commandResults, rest);
101 } 112 }
102 113
103 /** 114 /**
104 * Pulls the value for [option] from the second argument in [args]. Validates 115 * Pulls the value for [option] from the second argument in [args]. Validates
105 * that there is a valid value there. 116 * that there is a valid value there.
106 */ 117 */
107 void readNextArgAsValue(Option option) { 118 void readNextArgAsValue(Option option) {
108 // Take the option argument from the next command line arg. 119 // Take the option argument from the next command line arg.
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 '"$value" is not an allowed value for option "${option.name}".'); 283 '"$value" is not an allowed value for option "${option.name}".');
273 } 284 }
274 285
275 if (option.allowMultiple) { 286 if (option.allowMultiple) {
276 results[option.name].add(value); 287 results[option.name].add(value);
277 } else { 288 } else {
278 results[option.name] = value; 289 results[option.name] = value;
279 } 290 }
280 } 291 }
281 } 292 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698