@@ -219,22 +219,49 @@ private boolean checkExists(final File file) {
219219 @ Command (name = "generate-completion" , version = "generate-completion " + CommandLine .VERSION ,
220220 mixinStandardHelpOptions = true ,
221221 description = {
222- "Generate bash/zsh completion script for ${ROOT-COMMAND-NAME:-the root command of this command}." ,
222+ "Generate bash/zsh or fish completion script for ${ROOT-COMMAND-NAME:-the root command of this command}." ,
223223 "Run the following command to give `${ROOT-COMMAND-NAME:-$PARENTCOMMAND}` TAB completion in the current shell:" ,
224224 "" ,
225- " source <(${PARENT-COMMAND-FULL-NAME:-$PARENTCOMMAND} ${COMMAND-NAME})" ,
225+ " bash/zsh: source <(${PARENT-COMMAND-FULL-NAME:-$PARENTCOMMAND} ${COMMAND-NAME})" ,
226+ "" ,
227+ " fish: eval (<(${PARENT-COMMAND-FULL-NAME:-$PARENTCOMMAND} ${COMMAND-NAME} --shell fish)\" " ,
226228 "" },
227229 optionListHeading = "Options:%n" ,
228230 helpCommand = true
229231 )
230232 public static class GenerateCompletion implements Runnable {
231233
234+ enum Shell {
235+ BASH {
236+ @ Override
237+ String generate (CommandSpec spec ) {
238+ return AutoComplete .bash (
239+ spec .root ().name (),
240+ spec .root ().commandLine ());
241+ }
242+ },
243+ FISH {
244+ @ Override
245+ String generate (CommandSpec spec ) {
246+ return AutoComplete .fish (
247+ spec .root ().name (),
248+ spec .root ().commandLine ());
249+ }
250+ };
251+
252+ abstract String generate (CommandSpec spec );
253+ }
254+
232255 @ Spec CommandLine .Model .CommandSpec spec ;
233256
257+ @ Option (names = {"-s" , "--shell" }, description = "Specify the shell to generate the completion script for. " +
258+ "When omitted, the default shell is bash. " +
259+ "Valid values are: bash, fish" , defaultValue = "bash" )
260+ Shell shell ;
261+
234262 public void run () {
235- String script = AutoComplete .bash (
236- spec .root ().name (),
237- spec .root ().commandLine ());
263+ String script = shell .generate (spec );
264+
238265 // not PrintWriter.println: scripts with Windows line separators fail in strange ways!
239266 spec .commandLine ().getOut ().print (script );
240267 spec .commandLine ().getOut ().print ('\n' );
0 commit comments