This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author andybuckley
Recipients andybuckley, bethard, gruszczy, r.david.murray, wplappert
Date 2010-04-05.15:44:29
SpamBayes Score 6.3726585e-10
Marked as misclassified No
Message-id <1270482273.26.0.710488603838.issue4256@psf.upfronthosting.co.za>
In-reply-to
Content
Thanks for the pointers to both of these... I wasn't aware of either. I see argparse has been recently approved for Python stdlib inclusion, too: http://www.python.org/dev/peps/pep-0389/ Congratulations!

As far as I can tell, genzshcomp is parsing the output of the help command to reverse-engineer what the allowed flags should be. Assuming that only one space occurs between the arg and its metavar, this should work 99% or the time... I'm not sure if there is any attempt to be clever when the formatting is ambiguous. But given that the opt parser already contains the structured information, life can be made easier by writing out a more readily parseable format.

Here's an example bash parser function and its usage, for a further-simplified form of the above format where each arg (long or short) gets a line of its own and the arguments are indicated by a separate word as in the current output:

Example input:
$ foo --help-options
#OPTPARSE_FORMAT 0
--version
-h
--help
-r REGEXP
--regexp REGEXP
-s N
--start N
-e M
--end M
-f FILE
--file FILE

and the parser/completion functions:

function _optparse_getargs() {
	local opts cur prev
	COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    PREVIFS=$IFS
    IFS=$'\n'
    for line in `$1 --help-options | egrep '^-'`; do
        opt=`echo $line | sed 's/^\(-[^ ]\+\).*$/\1/'`
        argeq=`echo $line | sed 's/^--[^ ]\+ \([A-Za-z0-9]*\)$/=/'`
        if [[ $argeq != "=" ]]; then argeq=""; fi
        opts="$opts $opt$argeq";
    done
    IFS=$PREVIFS
    unset PREVIFS
    opts=`echo $opts | sed -e 's/^ *//' -e 's/ *$//'`

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )        
        if test -n "$COMPREPLY"; then
            return 0
        fi
    fi

    return 0
}


function _foo() {
    _optparse_getargs foo
    return 0
}

complete -F _foo -o default foo
History
Date User Action Args
2010-04-05 15:44:33andybuckleysetrecipients: + andybuckley, bethard, wplappert, r.david.murray, gruszczy
2010-04-05 15:44:33andybuckleysetmessageid: <1270482273.26.0.710488603838.issue4256@psf.upfronthosting.co.za>
2010-04-05 15:44:31andybuckleylinkissue4256 messages
2010-04-05 15:44:29andybuckleycreate