Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * The main entrypoint for the pub command line application. | |
| 7 */ | |
| 8 #library('pub'); | |
| 9 | |
| 10 #import('utils.dart'); | |
| 11 | |
| 12 List<PubCommand> commands; | |
|
nweiz
2012/03/30 00:38:37
This should probably be a hash.
Bob Nystrom
2012/03/30 01:06:17
Done.
| |
| 13 | |
| 14 main() { | |
| 15 final args = new Options().arguments; | |
| 16 | |
| 17 commands = [ | |
| 18 new PubCommand('version', 'print Pub version', showVersion) | |
|
nweiz
2012/03/30 00:38:37
Not something worth changing in this CL, but at so
Bob Nystrom
2012/03/30 01:06:17
Good call. Added a TODO.
| |
| 19 ]; | |
| 20 | |
| 21 if (args.length == 0) { | |
| 22 showUsage(); | |
| 23 return; | |
| 24 } | |
| 25 | |
| 26 // Select the command. | |
| 27 final commandName = args[0]; | |
| 28 args.removeRange(0, 1); | |
| 29 | |
| 30 for (final command in commands) { | |
| 31 if (command.name == commandName) { | |
| 32 command.function(args); | |
| 33 return; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 // If we got here, the command wasn't recognized. | |
| 38 print('Unknown command "$commandName".'); | |
| 39 print('Run "pub help" to see available commands.'); | |
| 40 exit(64); // EX_USAGE, see http://www.freebsd.org/cgi/man.cgi?query=sysexits. | |
| 41 } | |
| 42 | |
| 43 /** Displays usage information for the app. */ | |
| 44 void showUsage() { | |
| 45 print('Pub is a package manager for Dart.'); | |
| 46 print(''); | |
| 47 print('Usage:'); | |
| 48 print(''); | |
| 49 print(' pub command [arguments]'); | |
| 50 print(''); | |
| 51 print('The commands are:'); | |
| 52 print(''); | |
| 53 | |
| 54 int length = 0; | |
| 55 commands.forEach((command) => length = Math.max(length, command.name.length)); | |
| 56 | |
| 57 for (final command in commands) { | |
| 58 print(' ${padRight(command.name, length)} ${command.description}'); | |
|
nweiz
2012/03/30 00:38:37
I'm not sure two spaces between the command name a
Bob Nystrom
2012/03/30 01:06:17
Done.
| |
| 59 } | |
| 60 | |
| 61 print(''); | |
| 62 print('Use "pub help [command]" for more information about a command.'); | |
| 63 } | |
| 64 | |
| 65 /** Displays pub version information. */ | |
| 66 void showVersion(List<String> args) { | |
| 67 // TODO(rnystrom): Store some place central. | |
| 68 print('Pub 0.0.0'); | |
| 69 } | |
| 70 | |
| 71 typedef void CommandFunction(List<String> args); | |
| 72 | |
| 73 class PubCommand { | |
| 74 final String name; | |
| 75 final String description; | |
| 76 final CommandFunction function; | |
| 77 | |
| 78 PubCommand(this.name, this.description, this.function); | |
| 79 } | |
| OLD | NEW |