Skip to content

Commit

Permalink
#121 improve usage help; handle invalid input better
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Aug 8, 2017
1 parent f8d7ffe commit f45922a
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,24 @@ private AutoComplete() {

public static void main(String[] args) { CommandLine.run(new App(), System.err, args); }

@Command(name = "picocli.AutoComplete", description = "")
/**
* CLI command class for generating completion script.
*/
@Command(name = "picocli.AutoComplete", sortOptions = false,
description = "Generates a bash completion script for the specified command class.")
private static class App implements Runnable {

@Parameters(arity = "1", description = "Fully qualified class name of the annotated " +
"@Command class to generate a completion script for.")
String commandLineFQCN;

@Option(names = {"-n", "--name"}, description = "Name of the command to create a completion script for. " +
"When omitted, the annotated class @Command 'name' attribute is used. " +
"If no @Command 'name' attribute exists, '<CLASS-SIMPLE-NAME>' (in lower-case) is used.")
String commandName;

@Option(names = {"-o", "--completionScript"},
description = "Name of the completion script file to generate. " +
description = "Path of the completion script file to generate. " +
"When omitted, a file named '<commandName>_completion' " +
"is generated in the current directory.")
File autoCompleteScript;
Expand All @@ -63,16 +71,19 @@ private static class App implements Runnable {
"as the completion script.")
boolean writeCommandScript;

@Parameters(arity = "1", description = "Fully qualified class name of the annotated " +
"@Command class to generate a completion script for.")
String commandLineFQCN;

@Option(names = {"-f", "--force"}, description = "Overwrite existing script files.")
boolean overwriteIfExists;

@Option(names = { "-h", "--help"}, usageHelp = true, description = "Display this help message and quit.")
boolean usageHelpRequested;

@Override
public void run() {
try {
if (usageHelpRequested || commandLineFQCN == null) {
CommandLine.usage(new App(), System.err);
return;
}
Class<?> cls = Class.forName(commandLineFQCN);
CommandLine commandLine = new CommandLine(cls.newInstance());

Expand All @@ -91,9 +102,12 @@ public void run() {
}
if (commandScript != null && !overwriteIfExists && checkExists(commandScript)) { return; }
if (!overwriteIfExists && checkExists(autoCompleteScript)) { return; }

AutoComplete.bash(commandName, autoCompleteScript, commandScript, commandLine);

} catch (Exception ex) {
ex.printStackTrace();
CommandLine.usage(new App(), System.err);
}
}

Expand Down

0 comments on commit f45922a

Please sign in to comment.