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

Side by Side Diff: pkg/analyzer-experimental/lib/options.dart

Issue 12049028: Options parsing cleanup. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 11 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
« no previous file with comments | « pkg/analyzer-experimental/bin/analyzer.dart ('k') | no next file » | 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) 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 options; 5 library options;
6 6
7 import 'package:args/args.dart'; 7 import 'package:args/args.dart';
8 8
9 import 'dart:io'; 9 import 'dart:io';
10 10
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 class _CommandLineParser { 107 class _CommandLineParser {
108 108
109 final List<String> _knownFlags; 109 final List<String> _knownFlags;
110 final ArgParser _parser; 110 final ArgParser _parser;
111 111
112 /** Creates a new command line parser */ 112 /** Creates a new command line parser */
113 _CommandLineParser() 113 _CommandLineParser()
114 : _knownFlags = <String>[], 114 : _knownFlags = <String>[],
115 _parser = new ArgParser(); 115 _parser = new ArgParser();
116 116
117
118 /** 117 /**
119 * Defines a flag. 118 * Defines a flag.
120 * 119 *
121 * See [ArgParser.addFlag()]. 120 * See [ArgParser.addFlag()].
122 */ 121 */
123 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, 122 void addFlag(String name, {String abbr, String help, bool defaultsTo: false,
124 bool negatable: true, void callback(bool value)}) { 123 bool negatable: true, void callback(bool value)}) {
125 _knownFlags.add(name); 124 _knownFlags.add(name);
126 _parser.addFlag(name, abbr: abbr, help: help, defaultsTo: defaultsTo, 125 _parser.addFlag(name, abbr: abbr, help: help, defaultsTo: defaultsTo,
127 negatable: negatable, callback: callback); 126 negatable: negatable, callback: callback);
128 } 127 }
129 128
130 /** 129 /**
131 * Defines a value-taking option. 130 * Defines a value-taking option.
132 * 131 *
133 * See [ArgParser.addOption()]. 132 * See [ArgParser.addOption()].
134 */ 133 */
135 void addOption(String name, {String abbr, String help, List<String> allowed, 134 void addOption(String name, {String abbr, String help, List<String> allowed,
136 Map<String, String> allowedHelp, String defaultsTo, 135 Map<String, String> allowedHelp, String defaultsTo,
137 void callback(value), bool allowMultiple: false}) { 136 void callback(value), bool allowMultiple: false}) {
138 _parser.addOption(name, abbr: abbr, help: help, allowed: allowed, 137 _parser.addOption(name, abbr: abbr, help: help, allowed: allowed,
139 allowedHelp: allowedHelp, defaultsTo: defaultsTo, callback: callback, 138 allowedHelp: allowedHelp, defaultsTo: defaultsTo, callback: callback,
140 allowMultiple: allowMultiple); 139 allowMultiple: allowMultiple);
141 } 140 }
142 141
143
144 /** 142 /**
145 * Generates a string displaying usage information for the defined options. 143 * Generates a string displaying usage information for the defined options.
146 * 144 *
147 * See [ArgParser.getUsage()]. 145 * See [ArgParser.getUsage()].
148 */ 146 */
149 String getUsage() => _parser.getUsage(); 147 String getUsage() => _parser.getUsage();
150 148
151 /** 149 /**
152 * Parses [args], a list of command-line arguments, matches them against the 150 * Parses [args], a list of command-line arguments, matches them against the
153 * flags and options defined by this parser, and returns the result. 151 * flags and options defined by this parser, and returns the result.
154 * 152 *
155 * See [ArgParser]. 153 * See [ArgParser].
156 */ 154 */
157 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args)); 155 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args));
158 156
159 List<String> _filterUnknowns(args) { 157 List<String> _filterUnknowns(args) {
160 158
161 // Only filter args if the ignore flag is specified. 159 // Only filter args if the ignore flag is specified.
162 if (!args.contains('--ignore_unrecognized_flags')) { 160 if (!args.contains('--ignore_unrecognized_flags')) {
163 return args; 161 return args;
164 } 162 }
165 163
166 // Filter all unrecognized flags and options. 164 // Filter all unrecognized flags and options.
167 var filtered = <String>[]; 165 return args.where((arg) => !arg.startsWith('--') ||
168 for (var i=0; i < args.length; ++i) { 166 _knownFlags.contains(arg.substring(2)));
169 var arg = args[i];
170 if (arg.startsWith('--') && arg.length > 2) {
171 if (!_knownFlags.contains(arg.substring(2))) {
172 //"eat" params by advancing to the next flag/option
173 i = _getNextFlagIndex(args, i);
174 } else {
175 filtered.add(arg);
176 }
177 } else {
178 filtered.add(arg);
179 }
180 }
181 167
182 return filtered;
183 }
184
185 _getNextFlagIndex(args, i) {
186 for ( ; i < args.length; ++i) {
187 if (args[i].startsWith('--')) {
188 return i;
189 }
190 }
191 return i;
192 } 168 }
193 169
194 } 170 }
195 171
OLDNEW
« no previous file with comments | « pkg/analyzer-experimental/bin/analyzer.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698