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

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

Issue 11938028: Experimental analyzer front-end baby-steps. (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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library options;
6
7 import 'package:args/args.dart';
8
9 import 'dart:io';
10
11 /**
12 * Analyzer commandline configuration options.
13 */
14 class CommandLineOptions {
15
16 static const _BINARY_NAME = 'analyzer';
17
18 static const _SDK_ENV = 'com.google.dart.sdk';
19
20 static final String _DEFAULT_SDK_LOCATION = Platform.environment[_SDK_ENV];
21
22 /** Batch mode (for unit testing) */
23 final bool shouldBatch;
24
25 /** Whether to use machine format for error display */
26 final bool machineFormat;
27
28 /** Whether to ignore unrecognized flags */
29 final bool ignoreUnrecognizedFlags;
30
31 /** Whether to print metrics */
32 final bool showMetrics;
33
34 /** Whether to treat warnings as fatal */
35 final bool warningsAreFatal;
36
37 /** The path to the dart SDK */
38 final String dartSdkPath;
39
40 /** The source files to analyze */
41 final List<String> sourceFiles;
42
43 /**
44 * Initialize options from the given parsed [args].
45 */
46 CommandLineOptions.fromArgs(ArgResults args)
47 : shouldBatch = args['batch'],
48 machineFormat = args['machine_format'],
49 ignoreUnrecognizedFlags = args['ignore_unrecognized_flags'],
50 showMetrics = args['metrics'],
51 warningsAreFatal = args['fatal_warnings'],
52 dartSdkPath = args['dart_sdk'],
53 sourceFiles = args.rest;
54
55 /**
56 * Parse [args] into [CommandLineOptions] describing the specified
57 * analyzer options. In case of a format error, [null] is returned.
58 */
59 static CommandLineOptions parse(List<String> args) {
60
61 var parser = new _CommandLineParser()
62 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode',
63 defaultsTo: false, negatable: false)
64 ..addOption('dart_sdk', help: 'Specify path to the Dart sdk',
65 defaultsTo: _DEFAULT_SDK_LOCATION)
66 ..addFlag('machine_format', help: 'Specify whether errors '
67 'should be in machine format',
68 defaultsTo: false, negatable: false)
69 ..addFlag('ignore_unrecognized_flags',
70 help: 'Ignore unrecognized command line flags',
71 defaultsTo: false, negatable: false)
72 ..addFlag('fatal_warnings', help: 'Treat non-type warnings as fatal',
73 defaultsTo: false, negatable: false)
74 ..addFlag('metrics', help: 'Print metrics',
75 defaultsTo: false, negatable: false)
76 ..addFlag('help', abbr: 'h', help: 'Display this help message',
77 defaultsTo: false, negatable: false);
78
79 try {
80 var results = parser.parse(args);
81 if (results['help'] || results.rest.length == 0) {
82 _showUsage(parser);
83 return null;
84 }
85 return new CommandLineOptions.fromArgs(results);
86 } on FormatException catch (e) {
87 print(e.message);
88 _showUsage(parser);
89 return null;
90 }
91
92 }
93
94 static _showUsage(parser) {
95 print('Usage: ${_BINARY_NAME} [options...] '
96 '<libraries to analyze...]');
Brian Wilkerson 2013/01/18 21:14:33 Was the '<' suppose to be '['?
pquitslund 2013/01/18 21:22:20 Aha. Fixed!
97 print(parser.getUsage());
98 }
99
100 }
101
102 /**
103 * Commandline argument parser.
104 *
105 * TODO(pquitslund): when the args package supports ignoring unrecognized
106 * options/flags, this class can be replaced with a simple [ArgParser] instance.
107 */
108 class _CommandLineParser {
109
110 final List<String> _knownFlags;
111 final ArgParser _parser;
112
113 /** Creates a new command line parser */
114 _CommandLineParser()
115 : _knownFlags = <String>[],
116 _parser = new ArgParser();
117
118
119 /**
120 * Defines a flag.
121 *
122 * See [ArgParser.addFlag()].
123 */
124 void addFlag(String name, {String abbr, String help, bool defaultsTo: false,
125 bool negatable: true, void callback(bool value)}) {
126 _knownFlags.add(name);
127 _parser.addFlag(name, abbr:abbr, help:help, defaultsTo:defaultsTo,
128 negatable:negatable, callback:callback);
129 }
130
131 /**
132 * Defines a value-taking option.
133 *
134 * See [ArgParser.addOption()].
135 */
136 void addOption(String name, {String abbr, String help, List<String> allowed,
137 Map<String, String> allowedHelp, String defaultsTo,
138 void callback(value), bool allowMultiple: false}) {
139 _parser.addOption(name, abbr:abbr, help:help, allowed:allowed,
140 allowedHelp:allowedHelp, defaultsTo:defaultsTo, callback:callback,
141 allowMultiple:allowMultiple);
142 }
143
144
145 /**
146 * Generates a string displaying usage information for the defined options.
147 *
148 * See [ArgParser.getUsage()].
149 */
150 String getUsage() => _parser.getUsage();
151
152 /**
153 * Parses [args], a list of command-line arguments, matches them against the
154 * flags and options defined by this parser, and returns the result.
155 *
156 * See [ArgParser].
157 */
158 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args));
159
160 List<String> _filterUnknowns(args) {
161
162 //Only filter if the ignore flag is specified
163 if (!args.contains('--ignore_unrecognized_flags')) {
164 return args;
165 }
166
167 //Filter all unrecognized flags and options
168 var filtered = <String>[];
169 for (var i=0; i < args.length; ++i) {
170 var arg = args[i];
171 if (arg.startsWith('--') && arg.length > 2) {
172 if (!_knownFlags.contains(arg.substring(2))) {
173 //"eat" params by advancing to the next flag/option
174 i = _getNextFlagIndex(args, i);
175 } else {
176 filtered.add(arg);
177 }
178 } else {
179 filtered.add(arg);
180 }
181 }
182
183 return filtered;
184 }
185
186 _getNextFlagIndex(args, i) {
187 for ( ; i < args.length; ++i) {
188 if (args[i].startsWith('--')) {
189 return i;
190 }
191 }
192 return i;
193 }
194
195 }
196
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698