blob: b13f71c590c48227b0e13eb2aec44841d166d3ac [file] [log] [blame]
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'package:completion/completion.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../globals_null_migrated.dart' as globals;
import '../runner/flutter_command.dart';
class ShellCompletionCommand extends FlutterCommand {
ShellCompletionCommand() {
defaultsTo: false,
negatable: true,
help: 'Causes the given shell completion setup script to be overwritten if it already exists.',
final String name = 'bash-completion';
final String description = 'Output command line shell completion setup scripts.\n\n'
'This command prints the flutter command line completion setup script for Bash and Zsh. To '
'use it, specify an output file and follow the instructions in the generated output file to '
'install it in your shell environment. Once it is sourced, your shell will be able to '
'complete flutter commands and options.';
final List<String> aliases = <String>['zsh-completion'];
bool get shouldUpdateCache => false;
/// Return null to disable analytics recording of the `bash-completion` command.
Future<String> get usagePath async => null;
Future<FlutterCommandResult> runCommand() async {
if ( > 1) {
throwToolExit('Too many arguments given to bash-completion command.', exitCode: 1);
if ( || == '-') {
final String script = generateCompletionScript(<String>['flutter']);
return FlutterCommandResult.warning();
final File outputFile = globals.fs.file(;
if (outputFile.existsSync() && !boolArg('overwrite')) {
'Output file ${outputFile.path} already exists, will not overwrite. '
'Use --overwrite to force overwriting existing output file.',
exitCode: 1,
try {
} on FileSystemException catch (error) {
throwToolExit('Unable to write shell completion setup script.\n$error', exitCode: 1);
return FlutterCommandResult.success();