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

Unified Diff: pkg/args/lib/src/parser.dart

Issue 383913003: Add .wasParsed() to ArgResults. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: pkg/args/lib/src/parser.dart
diff --git a/pkg/args/lib/src/parser.dart b/pkg/args/lib/src/parser.dart
index 3f2e79dcec6937064b98a104ece9084c6e790df5..99ff465a3c3afcb0724a5115b7780446ece315b7 100644
--- a/pkg/args/lib/src/parser.dart
+++ b/pkg/args/lib/src/parser.dart
@@ -46,15 +46,6 @@ class Parser {
ArgResults parse() {
var commandResults = null;
- // Initialize flags to their defaults.
- grammar.options.forEach((name, option) {
- if (option.allowMultiple) {
- results[name] = [];
- } else {
- results[name] = option.defaultValue;
- }
- });
-
// Parse the args.
while (args.length > 0) {
if (current == '--') {
@@ -89,21 +80,16 @@ class Parser {
rest.add(args.removeAt(0));
}
- // Set unspecified multivalued arguments to their default value,
- // if any, and invoke the callbacks.
+ // Invoke the callbacks.
grammar.options.forEach((name, option) {
- if (option.allowMultiple &&
- results[name].length == 0 &&
- option.defaultValue != null) {
- results[name].add(option.defaultValue);
- }
- if (option.callback != null) option.callback(results[name]);
+ if (option.callback == null) return;
+ option.callback(option.getOrDefault(results[name]));
});
// Add in the leftover arguments we didn't parse to the innermost command.
rest.addAll(args);
args.clear();
- return new ArgResults(results, commandName, commandResults, rest);
+ return new ArgResults(grammar, results, commandName, commandResults, rest);
}
/// Pulls the value for [option] from the second argument in [args].
@@ -271,8 +257,9 @@ class Parser {
'"$value" is not an allowed value for option "${option.name}".');
}
- if (option.allowMultiple) {
- results[option.name].add(value);
+ if (option.isMultiple) {
+ var list = results.putIfAbsent(option.name, () => []);
+ list.add(value);
} else {
results[option.name] = value;
}

Powered by Google App Engine
This is Rietveld 408576698