Skip to content

Commit

Permalink
#121 added first case switch in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jul 22, 2017
1 parent b8b7dd9 commit 4ad12df
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
55 changes: 52 additions & 3 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,49 @@ private AutoComplete() {
" lastword=$(_get_lastword)\n" +
"\n";

private static final String FOOTER = "}\n\n" +
private static final String CASE_START = "\n" +
" # Un-comment this for debug purposes:\n" +
" # echo -e \"\\nprev = $prev, cur = $cur, firstword = $firstword, lastword = $lastword\\n\"\n" +
"\n" +
" case \"${firstword}\" in\n";

private static final String CASE_END = "" +
" -*)\n" +
" complete_options=\"$GLOBAL_OPTIONS\"\n" +
" ;;\n" +
" *)\n" +
" case \"${prev}\" in\n" +
" --log|--localdir|-l)\n" +
" # Special handling: return directories, no space at the end\n" +
"\n" +
" compopt -o nospace\n" +
" COMPREPLY=( $( compgen -d -S \"/\" -- $cur ) )\n" +
"\n" +
" return 0\n" +
" ;;\n" +
"\n" +
" --loglevel)\n" +
" complete_words=\"$GLOBAL_LOGLEVELS\"\n" +
" ;;\n" +
"\n" +
" *)\n" +
" complete_words=\"$GLOBAL_COMMANDS\"\n" +
" complete_options=\"$GLOBAL_OPTIONS\"\n" +
" ;;\n" +
" esac\n" +
" ;;\n" +
" esac\n" +
"\n" +
" # Either display words or options, depending on the user input\n" +
" if [[ $cur == -* ]]; then\n" +
" COMPREPLY=( $( compgen -W \"$complete_options\" -- $cur ))\n" +
"\n" +
" else\n" +
" COMPREPLY=( $( compgen -W \"$complete_words\" -- $cur ))\n" +
" fi\n";

private static final String FOOTER = " return 0\n" +
"}\n\n" +
"# Determines the first non-option word of the command line. This is usually the command.\n" +
"_get_firstword() {\n" +
" local firstword i\n" +
Expand All @@ -108,15 +150,16 @@ private AutoComplete() {
" fi\n" +
" done\n" +
" echo $lastword\n" +
"}\n";
"}\n" +
"complete -F _%1$s -o %1$s\n";

public static String bash(String scriptName, CommandLine commandLine) {
if (scriptName == null) { throw new NullPointerException("scriptName"); }
if (commandLine == null) { throw new NullPointerException("commandLine"); }
String result = "";
result += String.format(HEADER, scriptName);
result += generateAutoComplete("_", commandLine);
return result + FOOTER;
return result + String.format(FOOTER, scriptName);
}

private static String generateAutoComplete(String prefix, CommandLine commandLine) {
Expand All @@ -140,9 +183,15 @@ private static String generateAutoComplete(String prefix, CommandLine commandLin
for (Map.Entry<String, CommandLine> entry : commands.entrySet()) {
result += generateAutoComplete(prefix + entry.getKey() + "_", entry.getValue());
}

result += caseBodyStart(optionName2Field);
return result;
}

private static String caseBodyStart(final Map<String, Field> optionName2Field) {
return CASE_START + CASE_END;
}

private static String optionDeclaration(String prefix, Map<String, Field> options) {
if (options.isEmpty()) { return ""; }
StringBuilder result = new StringBuilder(" ").append(prefix).append("OPTIONS=\"\\\n");
Expand Down
42 changes: 40 additions & 2 deletions src/test/resources/script1.bash
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,45 @@ _script1() {
--timeUnit\
-u"


# Un-comment this for debug purposes:
# echo -e "\nprev = $prev, cur = $cur, firstword = $firstword, lastword = $lastword\n"

case "${firstword}" in
-*)
complete_options="$GLOBAL_OPTIONS"
;;
*)
case "${prev}" in
--log|--localdir|-l)
# Special handling: return directories, no space at the end

compopt -o nospace
COMPREPLY=( $( compgen -d -S "/" -- $cur ) )

return 0
;;

--loglevel)
complete_words="$GLOBAL_LOGLEVELS"
;;

*)
complete_words="$GLOBAL_COMMANDS"
complete_options="$GLOBAL_OPTIONS"
;;
esac
;;
esac

# Either display words or options, depending on the user input
if [[ $cur == -* ]]; then
COMPREPLY=( $( compgen -W "$complete_options" -- $cur ))

else
COMPREPLY=( $( compgen -W "$complete_words" -- $cur ))
fi
return 0
}

# Determines the first non-option word of the command line. This is usually the command.
Expand All @@ -83,5 +122,4 @@ _get_lastword() {
done
echo $lastword
}

complete -F _script1 script1
complete -F _script1 -o script1

0 comments on commit 4ad12df

Please sign in to comment.