Skip to content

Commit

Permalink
[Tool] Output help on 'flutter pub' (#126211)
Browse files Browse the repository at this point in the history
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 #110025
  • Loading branch information
loic-sharma committed May 8, 2023
1 parent 4439fd4 commit 472a0ab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion packages/flutter_tools/lib/src/commands/packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -40,6 +46,8 @@ class PackagesCommand extends FlutterCommand {
addSubcommand(PackagesPassthroughCommand());
}

final PrintFn _usagePrintFn;

@override
final String name = 'pub';

Expand All @@ -54,6 +62,9 @@ class PackagesCommand extends FlutterCommand {

@override
Future<FlutterCommandResult> runCommand() async => FlutterCommandResult.fail();

@override
void printUsage() => _usagePrintFn(usage);
}

class PackagesTestCommand extends FlutterCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 472a0ab

Please sign in to comment.