Bash completion?

139 views
Skip to first unread message

Julien Ponge

unread,
Nov 29, 2013, 5:57:13 AM11/29/13
to jcomm...@googlegroups.com
Hi,

Does anyone know how to generate Bash completion scripts for JCommander-based CLI tools?

I know I can write one by hand, but I have an interface with subcommands and arguments, and it is quite cumbersome to get right.

Or maybe JCommander could help as a completion target, too?


Cheers

- Julien

Steve Ramage

unread,
Nov 29, 2013, 12:20:20 PM11/29/13
to jcomm...@googlegroups.com

I asked this several months ago, and sadly there didn't seem to be anything. I ended up writing something that I am more than happy to share, it doesn't support commands but does support delegates. It's not a quite stand alone utility, but is part of a set of utilities I use to generate a manual, and better usage screen from JCommander, etc. You'd probably have to tinker with it a bit, and I'm not a bash expert. I've attached an example of what gets automatically generated. Most of the specific options are enum's or custom 'sets'. I have modified JCommander to require boolean arguments be passed true or false, so it auto completes true or false with booleans. It 'should' work with an arbitrary object, annotated correctly. 

 

Anyway if you'd find it helpful I don't mind packaging it up seperately.

 

Steve 

 

 

 

 

 

--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

 

bash_autocomplete.sh

Julien Ponge

unread,
Nov 29, 2013, 12:39:40 PM11/29/13
to jcomm...@googlegroups.com
Thanks Steve, I'll have a look. 
<bash_autocomplete.sh>

Steve Ramage

unread,
Nov 29, 2013, 1:15:37 PM11/29/13
to jcomm...@googlegroups.com

Here you go,

 

Attached is a packaged jar with source and compiled version. I didn't remove all the manual generation code, but did make an example. You run the ca.ubc.cs.beta.aclib.misc.bashcompletion.BashCompletion class, supplying the name of the class, the name of the command, and the name of the output file. You can also set debug to true, to see the output. I included a dummy example which you can run as follows:

 

java ... ca.ubc.cs.beta.aclib.misc.bashcompletion.BashCompletion --class ca.ubc.cs.beta.aclib.example.ExampleOptions --commandName joker --outputFile /tmp/bashcompletion.sh --debug

 

It will will write the following to the output file:

 

_joker()
{
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
case "${prev}" in
--card-back|--card-back-image)
_filedir
return 0
;;
--cardSuite)
answers="Hearts  Diamonds  Clubs  Spades"
COMPREPLY=( $(compgen -W "${answers}" -- ${cur}) )
 return 0
;;
--jokers-wild)
answers="true  false"
COMPREPLY=( $(compgen -W "${answers}" -- ${cur}) )
 return 0
;;
*)
answers="--card-back --card-back-image --cardSuite --jokers-wild --name --number "
if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${answers}" -- ${cur}) )
       return 0
    fi
;;
esac
}
complete -F _joker joker
jcomBash.jar

Julien Ponge

unread,
Nov 30, 2013, 5:31:00 AM11/30/13
to jcomm...@googlegroups.com
Thanks a lot Steve!
Reply all
Reply to author
Forward
0 new messages