Unverified Commit 472a0ab3 authored by Loïc Sharma's avatar Loïc Sharma Committed by GitHub

[Tool] Output help on 'flutter pub' (#126211)

This change makes `flutter pub` show the help usage:

```
PS > flutter pub
Commands for managing Flutter packages.

Global options:
-h, --help                  Print this usage information.
-v, --verbose               Noisy logging, including all shell commands executed.
                            If used with "--help", shows hidden options. If used with "flutter
                            doctor", shows additional diagnostic information. (Use "-vv" to force
                            verbose logging in those cases.)
-d, --device-id             Target device id or name (prefixes allowed).
    --version               Reports the version of this tool.
    --suppress-analytics    Suppress analytics reporting for the current CLI invocation.
    --disable-telemetry     Disable telemetry reporting when this command runs.

Usage: flutter pub <subcommand> [arguments]
-h, --help    Print this usage information.

Available subcommands:
  add         Add a dependency to pubspec.yaml.
  cache       Work with the Pub system cache.
  deps        Print package dependencies.
  downgrade   Downgrade packages in a Flutter project.
  get         Get the current package's dependencies.
  global      Work with Pub global packages.
  login       Log into pub.dev.
  logout      Log out of pub.dev.
  outdated    Analyze dependencies to find which ones can be upgraded.
  pub         Pass the remaining arguments to Dart's "pub" tool.
  publish     Publish the current package to pub.dartlang.org.
  remove      Removes a dependency from the current package.
  run         Run an executable from a package.
  test        Run the "test" package.
  token       Manage authentication tokens for hosted pub repositories.
  upgrade     Upgrade the current package's dependencies to latest versions.
  uploader    Manage uploaders for a package on pub.dev.
  version     Print Pub version.

Run "flutter help" to see global options.
```

Previously it showed an error message:

```
PS > flutter pub
Missing subcommand for "flutter pub".

Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.
```

Addresses https://github.com/flutter/flutter/issues/110025
parent 4439fd41
......@@ -123,7 +123,7 @@ class CustomDevicesCommand extends FlutterCommand {
final CustomDevicesConfig _customDevicesConfig;
final FeatureFlags _featureFlags;
final void Function(Object) _usagePrintFn;
final PrintFn _usagePrintFn;
@override
String get description {
......
......@@ -18,8 +18,14 @@ import '../project.dart';
import '../reporting/reporting.dart';
import '../runner/flutter_command.dart';
/// The function signature of the [print] function.
typedef PrintFn = void Function(Object?);
class PackagesCommand extends FlutterCommand {
PackagesCommand() {
PackagesCommand({
PrintFn usagePrintFn = print,
}) : _usagePrintFn = usagePrintFn
{
addSubcommand(PackagesGetCommand('get', "Get the current package's dependencies.", PubContext.pubGet));
addSubcommand(PackagesGetCommand('upgrade', "Upgrade the current package's dependencies to latest versions.", PubContext.pubUpgrade));
addSubcommand(PackagesGetCommand('add', 'Add a dependency to pubspec.yaml.', PubContext.pubAdd));
......@@ -40,6 +46,8 @@ class PackagesCommand extends FlutterCommand {
addSubcommand(PackagesPassthroughCommand());
}
final PrintFn _usagePrintFn;
@override
final String name = 'pub';
......@@ -54,6 +62,9 @@ class PackagesCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> runCommand() async => FlutterCommandResult.fail();
@override
void printUsage() => _usagePrintFn(usage);
}
class PackagesTestCommand extends FlutterCommand {
......
......@@ -176,13 +176,16 @@ class FlutterCommandRunner extends CommandRunner<void> {
@override
Future<void> run(Iterable<String> args) {
// Have an invocation of 'build' print out it's sub-commands.
// Have invocations of 'build', 'custom-devices', and 'pub' print out
// their sub-commands.
// TODO(ianh): Move this to the Build command itself somehow.
if (args.length == 1) {
if (args.first == 'build') {
args = <String>['build', '-h'];
} else if (args.first == 'custom-devices') {
args = <String>['custom-devices', '-h'];
} else if (args.first == 'pub') {
args = <String>['pub', '-h'];
}
}
......
......@@ -30,6 +30,20 @@ void main() {
Cache.enableLocking();
});
testUsingContext('pub shows help', () async {
Object? usage;
final PackagesCommand command = PackagesCommand(
usagePrintFn: (Object? object) => usage = object,
);
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['pub']);
expect(usage, allOf(
contains('Commands for managing Flutter packages.'),
contains('Usage: flutter pub <subcommand> [arguments]'),
));
});
testUsingContext('pub get usage values are resilient to missing package config files before running "pub get"', () async {
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
fileSystem.currentDirectory.childFile('.flutter-plugins').createSync();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment