[Bash-completion-devel] Any common recipe for parsing command line options?

Raph gibboris at gmail.com
Fri Apr 30 22:25:39 UTC 2010


On Thu, Apr 29, 2010 at 11:22:01PM -0700, Alun Evans wrote:
> Hi,
> 
> I was wondering if there is any common recipe for adding completion for commands with arguments, e.g. say my format is:
> 
>  cmd [ -f | --file ] [ -o | --output ] [ -v | --verbose ]
> 
> if I do:
> 
>  cmd -<TAB>
> 
> I'd like to generate:
>   -f --file -o --output -v --verbose
> 
> but if I do:
> 
>  cmd -f foo -<TAB>
> or:
>  cmd --file foo -<TAB>
> 
> I'd like to see:
>   -o --output -v --verbose
> 
> I could cook something up, but I'm thinking this is a very common task, and wondering if it's been done already.
> 
> 
> Hmm, I guess this is some kind of getopts style...
> 
> 
> thanks,
> 
> 
> A.
> 
> -- 
> Alun Evans
It has already been (more or less) done (probably many times) but never
integrated (needed ?) in the bash-completion helpers.
I think it comes to consider arguments as entities which may be :
1) mutualy exclusives
2) uniques

To do both 1) and 2) we need to parse the whole command line.
Whether it should be done at each <tab> press is a matter of storing
information across bash completion invokations.
Anyway I'm quite sure all the processing which is already done for a basic
completion already exceed the cost of a full "parsing" of the
command-line. (maybe strace would do the proof)

If it has to be done in the bash completion, then I think it may be done
by setting 'compgen' an alias to a bash function wrapper which would process a
serialisation of the (mutual exclusivity|unicity) of arguments.
eg:
compgen -W "list,  ..." -U "uniq-arg" -x "yes|no' -x "foo|bar"
But to be exhaustive, we would need to handle the "dependency" an
argument may have against another (eg: 'tc', 'iptables' or 'mplayer')

best regards

Raph





More information about the Bash-completion-devel mailing list