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

Side by Side Diff: utils/pub/pub.dart

Issue 11363249: Fix error reporting on invalid command line args. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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 | « utils/pub/command_version.dart ('k') | utils/pub/source.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 /** 5 /**
6 * The main entrypoint for the pub command line application. 6 * The main entrypoint for the pub command line application.
7 */ 7 */
8 library pub; 8 library pub;
9 9
10 import '../../pkg/args/lib/args.dart'; 10 import '../../pkg/args/lib/args.dart';
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 help: 'Prints the version of Pub'); 52 help: 'Prints the version of Pub');
53 parser.addFlag('trace', help: 'Prints a stack trace when an error occurs'); 53 parser.addFlag('trace', help: 'Prints a stack trace when an error occurs');
54 return parser; 54 return parser;
55 } 55 }
56 56
57 main() { 57 main() {
58 var globalOptions; 58 var globalOptions;
59 try { 59 try {
60 globalOptions = pubArgParser.parse(new Options().arguments); 60 globalOptions = pubArgParser.parse(new Options().arguments);
61 } on FormatException catch (e) { 61 } on FormatException catch (e) {
62 printUsage(description: e.message); 62 printError(e.message);
63 return; 63 printError('Run "pub help" to see available options.');
64 exit(exit_codes.USAGE);
64 } 65 }
65 66
66 if (globalOptions['version']) { 67 if (globalOptions['version']) {
67 printVersion(); 68 printVersion();
68 return; 69 return;
69 } 70 }
70 71
71 if (globalOptions['help'] || globalOptions.rest.isEmpty) { 72 if (globalOptions['help'] || globalOptions.rest.isEmpty) {
72 printUsage(); 73 printUsage();
73 return; 74 return;
(...skipping 14 matching lines...) Expand all
88 89
89 var cache = new SystemCache(cacheDir); 90 var cache = new SystemCache(cacheDir);
90 cache.register(new SdkSource(sdkDir)); 91 cache.register(new SdkSource(sdkDir));
91 cache.register(new GitSource()); 92 cache.register(new GitSource());
92 cache.register(new HostedSource()); 93 cache.register(new HostedSource());
93 cache.sources.setDefault('hosted'); 94 cache.sources.setDefault('hosted');
94 95
95 // Select the command. 96 // Select the command.
96 var command = pubCommands[globalOptions.rest[0]]; 97 var command = pubCommands[globalOptions.rest[0]];
97 if (command == null) { 98 if (command == null) {
98 printError('Unknown command "${globalOptions.rest[0]}".'); 99 printError('Could not find a command named "${globalOptions.rest[0]}".');
99 printError('Run "pub help" to see available commands.'); 100 printError('Run "pub help" to see available commands.');
100 exit(exit_codes.USAGE); 101 exit(exit_codes.USAGE);
101 return; 102 return;
102 } 103 }
103 104
104 var commandArgs = 105 var commandArgs =
105 globalOptions.rest.getRange(1, globalOptions.rest.length - 1); 106 globalOptions.rest.getRange(1, globalOptions.rest.length - 1);
106 command.run(cache, globalOptions, commandArgs); 107 command.run(cache, globalOptions, commandArgs);
107 } 108 }
108 109
109 /** Displays usage information for the app. */ 110 /** Displays usage information for the app. */
110 void printUsage([String description = 'Pub is a package manager for Dart.']) { 111 void printUsage([String description = 'Pub is a package manager for Dart.']) {
111 print(description); 112 print(description);
112 print(''); 113 print('');
113 print('Usage: pub command [arguments]'); 114 print('Usage: pub command [arguments]');
114 print(''); 115 print('');
115 print('Global options:'); 116 print('Global options:');
116 print(pubArgParser.getUsage()); 117 print(pubArgParser.getUsage());
117 print(''); 118 print('');
118 print('The commands are:');
119 119
120 // Show the commands sorted. 120 // Show the commands sorted.
121 print('Available commands:');
122
121 // TODO(rnystrom): A sorted map would be nice. 123 // TODO(rnystrom): A sorted map would be nice.
122 int length = 0; 124 int length = 0;
123 var names = <String>[]; 125 var names = <String>[];
124 for (var command in pubCommands.keys) { 126 for (var command in pubCommands.keys) {
125 length = max(length, command.length); 127 length = max(length, command.length);
126 names.add(command); 128 names.add(command);
127 } 129 }
128 130
129 names.sort((a, b) => a.compareTo(b)); 131 names.sort((a, b) => a.compareTo(b));
130 132
(...skipping 12 matching lines...) Expand all
143 abstract class PubCommand { 145 abstract class PubCommand {
144 SystemCache cache; 146 SystemCache cache;
145 ArgResults globalOptions; 147 ArgResults globalOptions;
146 ArgResults commandOptions; 148 ArgResults commandOptions;
147 149
148 Entrypoint entrypoint; 150 Entrypoint entrypoint;
149 151
150 /** 152 /**
151 * A one-line description of this command. 153 * A one-line description of this command.
152 */ 154 */
153 abstract String get description; 155 String get description;
154 156
155 /** 157 /**
156 * How to invoke this command (e.g. `"pub install [package]"`). 158 * How to invoke this command (e.g. `"pub install [package]"`).
157 */ 159 */
158 abstract String get usage; 160 String get usage;
159 161
160 /// Whether or not this command requires [entrypoint] to be defined. If false, 162 /// Whether or not this command requires [entrypoint] to be defined. If false,
161 /// Pub won't look for a pubspec and [entrypoint] will be null when the 163 /// Pub won't look for a pubspec and [entrypoint] will be null when the
162 /// command runs. 164 /// command runs.
163 bool get requiresEntrypoint => true; 165 bool get requiresEntrypoint => true;
164 166
165 /** 167 /**
166 * Override this to define command-specific options. The results will be made 168 * Override this to define command-specific options. The results will be made
167 * available in [commandOptions]. 169 * available in [commandOptions].
168 */ 170 */
169 ArgParser get commandParser => new ArgParser(); 171 ArgParser get commandParser => new ArgParser();
170 172
171 void run(SystemCache cache_, ArgResults globalOptions_, 173 void run(SystemCache cache_, ArgResults globalOptions_,
172 List<String> commandArgs) { 174 List<String> commandArgs) {
173 cache = cache_; 175 cache = cache_;
174 globalOptions = globalOptions_; 176 globalOptions = globalOptions_;
175 177
176 try { 178 try {
177 commandOptions = commandParser.parse(commandArgs); 179 commandOptions = commandParser.parse(commandArgs);
178 } on FormatException catch (e) { 180 } on FormatException catch (e) {
179 this.printUsage(description: e.message); 181 printError(e.message);
182 printError('Use "pub help" for more information.');
180 exit(exit_codes.USAGE); 183 exit(exit_codes.USAGE);
181 } 184 }
182 185
183 handleError(error, trace) { 186 handleError(error, trace) {
184 // This is basically the top-level exception handler so that we don't 187 // This is basically the top-level exception handler so that we don't
185 // spew a stack trace on our users. 188 // spew a stack trace on our users.
186 var message = error.toString(); 189 var message = error.toString();
187 190
188 // TODO(rnystrom): The default exception implementation class puts 191 // TODO(rnystrom): The default exception implementation class puts
189 // "Exception:" in the output, so strip that off. 192 // "Exception:" in the output, so strip that off.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 // Explicitly exit on success to ensure that any dangling dart:io handles 241 // Explicitly exit on success to ensure that any dangling dart:io handles
239 // don't cause the process to never terminate. 242 // don't cause the process to never terminate.
240 future.then((_) => exit(0)); 243 future.then((_) => exit(0));
241 } 244 }
242 245
243 /** 246 /**
244 * Override this to perform the specific command. Return a future that 247 * Override this to perform the specific command. Return a future that
245 * completes when the command is done or fails if the command fails. If the 248 * completes when the command is done or fails if the command fails. If the
246 * command is synchronous, it may return `null`. 249 * command is synchronous, it may return `null`.
247 */ 250 */
248 abstract Future onRun(); 251 Future onRun();
249 252
250 /** Displays usage information for this command. */ 253 /** Displays usage information for this command. */
251 void printUsage([String description]) { 254 void printUsage([String description]) {
252 if (description == null) description = this.description; 255 if (description == null) description = this.description;
253 print(description); 256 print(description);
254 print(''); 257 print('');
255 print('Usage: $usage'); 258 print('Usage: $usage');
256 259
257 var commandUsage = commandParser.getUsage(); 260 var commandUsage = commandParser.getUsage();
258 if (!commandUsage.isEmpty) { 261 if (!commandUsage.isEmpty) {
259 print(''); 262 print('');
260 print(commandUsage); 263 print(commandUsage);
261 } 264 }
262 } 265 }
263 266
264 /// Returns the appropriate exit code for [exception], falling back on 1 if no 267 /// Returns the appropriate exit code for [exception], falling back on 1 if no
265 /// appropriate exit code could be found. 268 /// appropriate exit code could be found.
266 int _chooseExitCode(exception) { 269 int _chooseExitCode(exception) {
267 if (exception is HttpException || exception is HttpParserException || 270 if (exception is HttpException || exception is HttpParserException ||
268 exception is SocketIOException || exception is PubHttpException) { 271 exception is SocketIOException || exception is PubHttpException) {
269 return exit_codes.UNAVAILABLE; 272 return exit_codes.UNAVAILABLE;
270 } else if (exception is FormatException) { 273 } else if (exception is FormatException) {
271 return exit_codes.DATA; 274 return exit_codes.DATA;
272 } else { 275 } else {
273 return 1; 276 return 1;
274 } 277 }
275 } 278 }
276 } 279 }
OLDNEW
« no previous file with comments | « utils/pub/command_version.dart ('k') | utils/pub/source.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698