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

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

Issue 10756011: Have Pub make use of the arg parser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add tests for non-negatable flags. Created 8 years, 5 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 | « tests/lib/args/args_test.dart ('k') | utils/tests/pub/pub_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
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('../../lib/args/args.dart');
10 #import('io.dart'); 11 #import('io.dart');
11 #import('command_install.dart'); 12 #import('command_install.dart');
12 #import('command_list.dart'); 13 #import('command_list.dart');
13 #import('command_update.dart'); 14 #import('command_update.dart');
14 #import('command_version.dart'); 15 #import('command_version.dart');
15 #import('entrypoint.dart'); 16 #import('entrypoint.dart');
16 #import('git_source.dart'); 17 #import('git_source.dart');
17 #import('package.dart'); 18 #import('package.dart');
18 #import('pubspec.dart'); 19 #import('pubspec.dart');
19 #import('repo_source.dart'); 20 #import('repo_source.dart');
20 #import('sdk_source.dart'); 21 #import('sdk_source.dart');
21 #import('source.dart'); 22 #import('source.dart');
22 #import('source_registry.dart'); 23 #import('source_registry.dart');
23 #import('system_cache.dart'); 24 #import('system_cache.dart');
24 #import('utils.dart'); 25 #import('utils.dart');
25 #import('version.dart'); 26 #import('version.dart');
26 27
27 Version get pubVersion() => new Version(0, 0, 0); 28 Version get pubVersion() => new Version(0, 0, 0);
28 29
29 main() { 30 main() {
30 var args = new Options().arguments;
31
32 // TODO(rnystrom): In addition to explicit "help" and "version" commands, 31 // TODO(rnystrom): In addition to explicit "help" and "version" commands,
33 // should also add special-case support for --help and --version arguments to 32 // should also add special-case support for --help and --version arguments to
34 // be consistent with other Unix apps. 33 // be consistent with other Unix apps.
35 var commands = { 34 var commands = {
36 'list': new ListCommand(), 35 'list': new ListCommand(),
37 'install': new InstallCommand(), 36 'install': new InstallCommand(),
38 'update': new UpdateCommand(), 37 'update': new UpdateCommand(),
39 'version': new VersionCommand() 38 'version': new VersionCommand()
40 }; 39 };
41 40
42 // TODO(rnystrom): Hack. This is temporary code to allow the pub tests to 41 var parser = new ArgParser();
43 // pass in relevant paths. Eventually these should be either environment 42 parser.addFlag('help', abbr: 'h', negatable: false,
44 // variables or at least a cleaner arg parser. 43 help: 'Prints this usage information');
45 var cacheDir, sdkDir; 44 parser.addFlag('version', negatable: false,
46 for (var i = 0; i < args.length; i++) { 45 help: 'Prints the version of Pub');
47 if (args[i].startsWith('--cachedir=')) { 46 // TODO(rnystrom): Hack. These are temporary options to allow the pub tests to
48 cacheDir = args[i].substring('--cachedir='.length); 47 // pass in relevant paths. Eventually these should be environment variables.
49 args.removeRange(i, 1); 48 parser.addOption('cachedir', help: 'The directory containing the system-wide '
50 i--; 49 'Pub cache');
51 } else if (args[i].startsWith('--sdkdir=')) { 50 parser.addOption('sdkdir', help: 'The directory containing the Dart SDK');
52 sdkDir = args[i].substring('--sdkdir='.length);
53 args.removeRange(i, 1);
54 i--;
55 }
56 }
57 51
58 if (args.length == 0) { 52 var results;
59 printUsage(commands); 53 try {
54 results = parser.parse(new Options().arguments);
55 } catch (ArgFormatException e) {
56 printUsage(parser, commands, description: e.message);
60 return; 57 return;
61 } 58 }
62 59
63 // For consistency with expected unix idioms, support --help, -h, and 60 if (results['version']) {
64 // --version in addition to the regular commands. 61 printVersion();
65 if (args.length == 1) { 62 return;
66 if (args[0] == '--help' || args[0] == '-h') {
67 printUsage(commands);
68 return;
69 }
70
71 if (args[0] == '--version') {
72 printVersion();
73 return;
74 }
75 } 63 }
76 64
77 var cache = new SystemCache(cacheDir); 65 if (results['help'] || results.rest.isEmpty()) {
78 cache.sources.register(new SdkSource(sdkDir)); 66 printUsage(parser, commands);
67 return;
68 }
69
70 var cache = new SystemCache(results['cachedir']);
71 cache.sources.register(new SdkSource(results['sdkdir']));
79 cache.sources.register(new GitSource()); 72 cache.sources.register(new GitSource());
80 cache.sources.register(new RepoSource()); 73 cache.sources.register(new RepoSource());
81 // TODO(nweiz): Make 'repo' the default once pub.dartlang.org exists 74 // TODO(nweiz): Make 'repo' the default once pub.dartlang.org exists
82 cache.sources.setDefault('sdk'); 75 cache.sources.setDefault('sdk');
83 76
84 // Select the command. 77 // Select the command.
85 var command = commands[args[0]]; 78 var command = commands[results.rest[0]];
86 if (command == null) { 79 if (command == null) {
87 printError('Unknown command "${args[0]}".'); 80 printError('Unknown command "${results.rest[0]}".');
88 printError('Run "pub help" to see available commands.'); 81 printError('Run "pub help" to see available commands.');
89 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits. 82 exit(64); // See http://www.freebsd.org/cgi/man.cgi?query=sysexits.
90 return; 83 return;
91 } 84 }
92 85
93 args.removeRange(0, 1); 86 command.run(cache, results.rest.getRange(1, results.rest.length - 1));
94 command.run(cache, args);
95 } 87 }
96 88
97 /** Displays usage information for the app. */ 89 /** Displays usage information for the app. */
98 void printUsage(Map<String, PubCommand> commands) { 90 void printUsage(ArgParser parser, Map<String, PubCommand> commands,
99 print('Pub is a package manager for Dart.'); 91 [String description = 'Pub is a package manager for Dart.']) {
92 print(description);
100 print(''); 93 print('');
101 print('Usage:'); 94 print('Usage: pub command [arguments]');
102 print(''); 95 print('');
103 print(' pub command [arguments]'); 96 print('Global options:');
97 print(parser.getUsage());
104 print(''); 98 print('');
105 print('The commands are:'); 99 print('The commands are:');
106 print('');
107 100
108 // Show the commands sorted. 101 // Show the commands sorted.
109 // TODO(rnystrom): A sorted map would be nice. 102 // TODO(rnystrom): A sorted map would be nice.
110 int length = 0; 103 int length = 0;
111 var names = <String>[]; 104 var names = <String>[];
112 for (var command in commands.getKeys()) { 105 for (var command in commands.getKeys()) {
113 length = Math.max(length, command.length); 106 length = Math.max(length, command.length);
114 names.add(command); 107 names.add(command);
115 } 108 }
116 109
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 }).handleException(handleError); 169 }).handleException(handleError);
177 } 170 }
178 171
179 /** 172 /**
180 * Override this to perform the specific command. Return a future that 173 * Override this to perform the specific command. Return a future that
181 * completes when the command is done or fails if the command fails. If the 174 * completes when the command is done or fails if the command fails. If the
182 * command is synchronous, it may return `null`. 175 * command is synchronous, it may return `null`.
183 */ 176 */
184 abstract Future onRun(); 177 abstract Future onRun();
185 } 178 }
OLDNEW
« no previous file with comments | « tests/lib/args/args_test.dart ('k') | utils/tests/pub/pub_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698