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

Side by Side Diff: lib/src/arg_parser.dart

Issue 975463004: Parse comma-separated multiple values. (Closed) Base URL: git@github.com:dart-lang/args@master
Patch Set: Code review changes Created 5 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
« no previous file with comments | « README.md ('k') | lib/src/option.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, 70 void addFlag(String name, {String abbr, String help, bool defaultsTo: false,
71 bool negatable: true, void callback(bool value), bool hide: false}) { 71 bool negatable: true, void callback(bool value), bool hide: false}) {
72 _addOption(name, abbr, help, null, null, null, defaultsTo, callback, 72 _addOption(name, abbr, help, null, null, null, defaultsTo, callback,
73 OptionType.FLAG, negatable: negatable, hide: hide); 73 OptionType.FLAG, negatable: negatable, hide: hide);
74 } 74 }
75 75
76 /// Defines a value-taking option. Throws an [ArgumentError] if: 76 /// Defines a value-taking option. Throws an [ArgumentError] if:
77 /// 77 ///
78 /// * There is already an option with name [name]. 78 /// * There is already an option with name [name].
79 /// * There is already an option using abbreviation [abbr]. 79 /// * There is already an option using abbreviation [abbr].
80 /// * [splitCommas] is passed but [allowMultiple] is `false`.
80 void addOption(String name, {String abbr, String help, String valueHelp, 81 void addOption(String name, {String abbr, String help, String valueHelp,
81 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo, 82 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo,
82 void callback(value), bool allowMultiple: false, bool hide: false}) { 83 void callback(value), bool allowMultiple: false, bool splitCommas,
84 bool hide: false}) {
85 if (!allowMultiple && splitCommas != null) {
86 throw new ArgumentError(
87 'splitCommas may not be set if allowMultiple is false.');
Sean Eagan 2015/03/04 22:28:03 Why not? Seems like it would be reasonable that s
nweiz 2015/03/04 22:39:55 Why would you want to forbid the latter? That prov
88 }
89
83 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, 90 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo,
84 callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE, 91 callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE,
85 hide: hide); 92 splitCommas: splitCommas, hide: hide);
86 } 93 }
87 94
88 void _addOption(String name, String abbr, String help, String valueHelp, 95 void _addOption(String name, String abbr, String help, String valueHelp,
89 List<String> allowed, Map<String, String> allowedHelp, defaultsTo, 96 List<String> allowed, Map<String, String> allowedHelp, defaultsTo,
90 void callback(value), OptionType type, 97 void callback(value), OptionType type,
91 {bool negatable: false, bool hide: false}) { 98 {bool negatable: false, bool splitCommas, bool hide: false}) {
92 // Make sure the name isn't in use. 99 // Make sure the name isn't in use.
93 if (_options.containsKey(name)) { 100 if (_options.containsKey(name)) {
94 throw new ArgumentError('Duplicate option "$name".'); 101 throw new ArgumentError('Duplicate option "$name".');
95 } 102 }
96 103
97 // Make sure the abbreviation isn't too long or in use. 104 // Make sure the abbreviation isn't too long or in use.
98 if (abbr != null) { 105 if (abbr != null) {
99 var existing = findByAbbreviation(abbr); 106 var existing = findByAbbreviation(abbr);
100 if (existing != null) { 107 if (existing != null) {
101 throw new ArgumentError( 108 throw new ArgumentError(
102 'Abbreviation "$abbr" is already used by "${existing.name}".'); 109 'Abbreviation "$abbr" is already used by "${existing.name}".');
103 } 110 }
104 } 111 }
105 112
106 _options[name] = newOption(name, abbr, help, valueHelp, allowed, 113 _options[name] = newOption(name, abbr, help, valueHelp, allowed,
107 allowedHelp, defaultsTo, callback, type, 114 allowedHelp, defaultsTo, callback, type,
108 negatable: negatable, hide: hide); 115 negatable: negatable, splitCommas: splitCommas, hide: hide);
109 } 116 }
110 117
111 /// Parses [args], a list of command-line arguments, matches them against the 118 /// Parses [args], a list of command-line arguments, matches them against the
112 /// flags and options defined by this parser, and returns the result. 119 /// flags and options defined by this parser, and returns the result.
113 ArgResults parse(List<String> args) => 120 ArgResults parse(List<String> args) =>
114 new Parser(null, this, args.toList(), null, null).parse(); 121 new Parser(null, this, args.toList(), null, null).parse();
115 122
116 /// Generates a string displaying usage information for the defined options. 123 /// Generates a string displaying usage information for the defined options.
117 /// 124 ///
118 /// This is basically the help text shown on the command line. 125 /// This is basically the help text shown on the command line.
(...skipping 14 matching lines...) Expand all
133 return options[option].defaultValue; 140 return options[option].defaultValue;
134 } 141 }
135 142
136 /// Finds the option whose abbreviation is [abbr], or `null` if no option has 143 /// Finds the option whose abbreviation is [abbr], or `null` if no option has
137 /// that abbreviation. 144 /// that abbreviation.
138 Option findByAbbreviation(String abbr) { 145 Option findByAbbreviation(String abbr) {
139 return options.values.firstWhere((option) => option.abbreviation == abbr, 146 return options.values.firstWhere((option) => option.abbreviation == abbr,
140 orElse: () => null); 147 orElse: () => null);
141 } 148 }
142 } 149 }
OLDNEW
« no previous file with comments | « README.md ('k') | lib/src/option.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698