I thought I'd give it a try myself... It works fine, except for a couple
of points:
1) there is no real way to check what option parser a program is
using; atm I'm checking if a program links to glib-2.0, but this
is both too weak (it may not use the parser) and too strong (it
may be a script that uses glib's parser via some bindings)
2) if some programs generate strings with double quotes or other
special characters, they won't be escaped and parsed properly
(but at least it should detect the error and fail gracefully,
rather than generating wrong completions).
If someone uses it, please let me have some feedback :)
regards
alberto
-------- Forwarded Message --------
> From: Alberto Colombo <[email protected]>
> To: [email protected]
> Subject: glib programs autocompetion
> Date: Wed, 15 Apr 2009 12:38:00 +0100
>
> Hello list,
>
> I have recently discovered fish, and it has quickly become my default
> shell both at home and at work. (Clever tab-completion and sane
> tokenisation/syntax being the main two reasons).
>
> Now, to the topic. Programs that use the glib commandline option
> parser[1] have an auto-generated help text, which is printed to stdout
> when the program is called with the -?, --help or --help-all options
> (try e.g. "gedit --help-all").
>
> Has anyone done any work on using this text to generate auto-completion
> hints for fish?
>
> Regards
> alberto
>
> [1]
> http://library.gnome.org/devel/glib/stable/glib-Commandline-option-parser.html
>
function glib_autocomplete --description "Generate autocompletion for programs that use GLib command-line parser"
function glib_autocomplete__print_usage --description "TODO: should not be visible outside of main function"
echo "glib_autocomplete -- Generate autocompletion for programs that use GLib commandline parser"
echo "Usage: glib_autocomplete PROGRAM"
echo "where PROGRAM is an executable that uses the command-line parser from glib-2.0"
functions --erase glib_autocomplete__print_usage
end
set -l progname
if [ (count $argv) -eq 1 ]
switch $argv[1]
case -h --h --he --hel --help
glib_autocomplete__print_usage
return 0
case '*'
set progname $argv[1]
end
if not ldd (which $progname) | grep glib-2.0 > /dev/null
echo $progname does not use glib-2.0
return 1
end
for line in (eval "$progname --help-all" | grep '^ -')
set -l tokens
eval set tokens (echo "$line" | sed -e "s/^ \(-\(.\), \)\?--\([-a-zA-Z0-9]\+\)\(=\([A-Z0-9=:]\+\)\)\?\( \)\+\([a-zA-Z0-9].*\)\$/\"\2\" \"\3\" \"\5\" \"\7\" /")
if not [ (count $tokens) -eq 4 ]
echo "Error, not 4 tokens in the following line:"
echo $line
echo "it was tokenised as " \"$tokens\"
return 1
end
complete --command $progname --short-option $tokens[1] --long-option $tokens[2] --arguments $tokens[3] --description $tokens[4] (test -z $tokens[3]; or echo "--require-parameter")
end
else
glib_autocomplete__print_usage
return 1
end
end
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Fish-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/fish-users