OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library pub.command; | 5 library pub.command; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'package:args/args.dart'; | 10 import 'package:args/args.dart'; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 /// If false, pub won't look for a pubspec and [entrypoint] will be null when | 122 /// If false, pub won't look for a pubspec and [entrypoint] will be null when |
123 /// the command runs. This only needs to be set in leaf commands. | 123 /// the command runs. This only needs to be set in leaf commands. |
124 bool get requiresEntrypoint => true; | 124 bool get requiresEntrypoint => true; |
125 | 125 |
126 /// Whether or not this command takes arguments in addition to options. | 126 /// Whether or not this command takes arguments in addition to options. |
127 /// | 127 /// |
128 /// If false, pub will exit with an error if arguments are provided. This | 128 /// If false, pub will exit with an error if arguments are provided. This |
129 /// only needs to be set in leaf commands. | 129 /// only needs to be set in leaf commands. |
130 bool get takesArguments => false; | 130 bool get takesArguments => false; |
131 | 131 |
| 132 /// Override this and return `false` to disallow trailing options from being |
| 133 /// parsed after a non-option argument is parsed. |
| 134 bool get allowTrailingOptions => true; |
| 135 |
132 /// Alternate names for this command. These names won't be used in the | 136 /// Alternate names for this command. These names won't be used in the |
133 /// documentation, but they will work when invoked on the command line. | 137 /// documentation, but they will work when invoked on the command line. |
134 final aliases = const <String>[]; | 138 final aliases = const <String>[]; |
135 | 139 |
136 /// The [ArgParser] for this command. | 140 /// The [ArgParser] for this command. |
137 final commandParser = new ArgParser(); | 141 ArgParser get commandParser => _commandParser; |
| 142 ArgParser _commandParser; |
138 | 143 |
139 /// Subcommands exposed by this command. | 144 /// Subcommands exposed by this command. |
140 /// | 145 /// |
141 /// If empty, then this command has no subcommands. Otherwise, a subcommand | 146 /// If empty, then this command has no subcommands. Otherwise, a subcommand |
142 /// must be specified by the user. In that case, this command's [onRun] will | 147 /// must be specified by the user. In that case, this command's [onRun] will |
143 /// not be called and the subcommand's will. | 148 /// not be called and the subcommand's will. |
144 final subcommands = <String, PubCommand>{}; | 149 final subcommands = <String, PubCommand>{}; |
145 | 150 |
146 /// Override this to use offline-only sources instead of hitting the network. | 151 /// Override this to use offline-only sources instead of hitting the network. |
147 /// | 152 /// |
148 /// This will only be called before the [SystemCache] is created. After that, | 153 /// This will only be called before the [SystemCache] is created. After that, |
149 /// it has no effect. This only needs to be set in leaf commands. | 154 /// it has no effect. This only needs to be set in leaf commands. |
150 bool get isOffline => false; | 155 bool get isOffline => false; |
151 | 156 |
152 PubCommand() { | 157 PubCommand() { |
| 158 _commandParser = new ArgParser(allowTrailingOptions: allowTrailingOptions); |
| 159 |
153 // Allow "--help" after a command to get command help. | 160 // Allow "--help" after a command to get command help. |
154 commandParser.addFlag('help', abbr: 'h', negatable: false, | 161 commandParser.addFlag('help', abbr: 'h', negatable: false, |
155 help: 'Print usage information for this command.'); | 162 help: 'Print usage information for this command.'); |
156 } | 163 } |
157 | 164 |
158 /// Runs this command using a system cache at [cacheDir] with [options]. | 165 /// Runs this command using a system cache at [cacheDir] with [options]. |
159 Future run(String cacheDir, ArgResults options) { | 166 Future run(String cacheDir, ArgResults options) { |
160 _commandOptions = options; | 167 _commandOptions = options; |
161 | 168 |
162 cache = new SystemCache.withSources(cacheDir, isOffline: isOffline); | 169 cache = new SystemCache.withSources(cacheDir, isOffline: isOffline); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 for (var alias in command.aliases) { | 263 for (var alias in command.aliases) { |
257 commands[alias] = command; | 264 commands[alias] = command; |
258 } | 265 } |
259 } | 266 } |
260 | 267 |
261 return commands; | 268 return commands; |
262 } | 269 } |
263 | 270 |
264 /// Creates the top-level [ArgParser] used to parse the pub command line. | 271 /// Creates the top-level [ArgParser] used to parse the pub command line. |
265 ArgParser _initArgParser() { | 272 ArgParser _initArgParser() { |
266 var argParser = new ArgParser(); | 273 var argParser = new ArgParser(allowTrailingOptions: true); |
267 | 274 |
268 // Add the global options. | 275 // Add the global options. |
269 argParser.addFlag('help', abbr: 'h', negatable: false, | 276 argParser.addFlag('help', abbr: 'h', negatable: false, |
270 help: 'Print this usage information.'); | 277 help: 'Print this usage information.'); |
271 argParser.addFlag('version', negatable: false, | 278 argParser.addFlag('version', negatable: false, |
272 help: 'Print pub version.'); | 279 help: 'Print pub version.'); |
273 argParser.addFlag('trace', | 280 argParser.addFlag('trace', |
274 help: 'Print debugging information when an error occurs.'); | 281 help: 'Print debugging information when an error occurs.'); |
275 argParser.addOption('verbosity', | 282 argParser.addOption('verbosity', |
276 help: 'Control output verbosity.', | 283 help: 'Control output verbosity.', |
(...skipping 17 matching lines...) Expand all Loading... |
294 | 301 |
295 /// Registers a [command] with [name] on [parser]. | 302 /// Registers a [command] with [name] on [parser]. |
296 void _registerCommand(String name, PubCommand command, ArgParser parser) { | 303 void _registerCommand(String name, PubCommand command, ArgParser parser) { |
297 parser.addCommand(name, command.commandParser); | 304 parser.addCommand(name, command.commandParser); |
298 | 305 |
299 // Recursively wire up any subcommands. | 306 // Recursively wire up any subcommands. |
300 command.subcommands.forEach((name, subcommand) { | 307 command.subcommands.forEach((name, subcommand) { |
301 _registerCommand(name, subcommand, command.commandParser); | 308 _registerCommand(name, subcommand, command.commandParser); |
302 }); | 309 }); |
303 } | 310 } |
OLD | NEW |