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

Side by Side Diff: pkg/analyzer/lib/src/command_line/command_line_parser.dart

Issue 2578733002: move CommandLineParser into analyzer for reuse by DDC (Closed)
Patch Set: Created 4 years 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 | « no previous file | pkg/analyzer/test/src/command_line/arguments_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 import 'package:args/args.dart';
Brian Wilkerson 2016/12/14 21:05:09 Copyright
danrubel 2016/12/14 21:28:07 Good eyes. Done.
2
3 /**
4 * Commandline argument parser.
5 *
6 * TODO(pq): when the args package supports ignoring unrecognized options/flags,
7 * this class can be replaced with a simple [ArgParser] instance.
8 */
9 class CommandLineParser {
10 static const String IGNORE_UNRECOGNIZED_FLAG = 'ignore-unrecognized-flags';
11
12 final List<String> _knownFlags;
13 final bool _alwaysIgnoreUnrecognized;
14 final ArgParser _parser;
15
16 /// Creates a new command line parser.
17 CommandLineParser({bool alwaysIgnoreUnrecognized: false})
18 : _knownFlags = <String>[],
19 _alwaysIgnoreUnrecognized = alwaysIgnoreUnrecognized,
20 _parser = new ArgParser(allowTrailingOptions: true) {
21 addFlag(IGNORE_UNRECOGNIZED_FLAG,
22 help: 'Ignore unrecognized command line flags.',
23 defaultsTo: false,
24 negatable: false);
25 }
26
27 ArgParser get parser => _parser;
28
29 /// Defines a flag.
30 /// See [ArgParser.addFlag()].
31 void addFlag(String name,
32 {String abbr,
33 String help,
34 bool defaultsTo: false,
35 bool negatable: true,
36 void callback(bool value),
37 bool hide: false}) {
38 _knownFlags.add(name);
39 _parser.addFlag(name,
40 abbr: abbr,
41 help: help,
42 defaultsTo: defaultsTo,
43 negatable: negatable,
44 callback: callback,
45 hide: hide);
46 }
47
48 /// Defines a value-taking option.
49 /// See [ArgParser.addOption()].
50 void addOption(String name,
51 {String abbr,
52 String help,
53 List<String> allowed,
54 Map<String, String> allowedHelp,
55 String defaultsTo,
56 void callback(value),
57 bool allowMultiple: false,
58 bool splitCommas,
59 bool hide: false}) {
60 _knownFlags.add(name);
61 _parser.addOption(name,
62 abbr: abbr,
63 help: help,
64 allowed: allowed,
65 allowedHelp: allowedHelp,
66 defaultsTo: defaultsTo,
67 callback: callback,
68 allowMultiple: allowMultiple,
69 splitCommas: splitCommas,
70 hide: hide);
71 }
72
73 /// Parses [args], a list of command-line arguments, matches them against the
74 /// flags and options defined by this parser, and returns the result.
75 /// See [ArgParser].
76 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args));
77
78 List<String> _filterUnknowns(List<String> args) {
79 if (!_alwaysIgnoreUnrecognized &&
80 !args.contains('--$IGNORE_UNRECOGNIZED_FLAG')) {
81 return args;
82 }
83
84 //TODO(pquitslund): replace w/ the following once library skew issues are
85 // sorted out
86 //return args.where((arg) => !arg.startsWith('--') ||
87 // _knownFlags.contains(arg.substring(2)));
88
89 // Filter all unrecognized flags and options.
90 List<String> filtered = <String>[];
91 for (int i = 0; i < args.length; ++i) {
92 String arg = args[i];
93 if (arg.startsWith('--') && arg.length > 2) {
94 String option = arg.substring(2);
95 // strip the last '=value'
96 int equalsOffset = option.lastIndexOf('=');
97 if (equalsOffset != -1) {
98 option = option.substring(0, equalsOffset);
99 }
100 // Check the option
101 if (!_knownFlags.contains(option)) {
102 //"eat" params by advancing to the next flag/option
103 i = _getNextFlagIndex(args, i);
104 } else {
105 filtered.add(arg);
106 }
107 } else {
108 filtered.add(arg);
109 }
110 }
111
112 return filtered;
113 }
114
115 int _getNextFlagIndex(args, i) {
116 for (; i < args.length; ++i) {
117 if (args[i].startsWith('--')) {
118 return i;
119 }
120 }
121 return i;
122 }
123 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/command_line/arguments_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698