[Bash-completion-devel] some patches

Raphaël raphael.droz at gmail.com
Sun Jun 12 09:53:43 UTC 2011


On Sun, Jun 12, 2011 at 07:42:30AM +0200, David Paleino wrote:
> On Sat, 11 Jun 2011 23:43:15 +0200, Raphaël wrote:
> 
> > [..]
> > 
> > > > - a completion specific to the mysql client.
> > > 
> > > Added, with some minor modifications (in a couple of lines you used tabs
> > > instead of four spaces), and with the usage of _parse_help instead of
> > > hardcoding the options list.
> > 
> > I didn't use it because of:
> > diff	\
> >  <(mysql --help|grep -woE -- '--[a-z0-9-]+'|sort -u|grep -v
> > 'variable-name')	\ <(_parse_help mysql|sort -u|sed s/=//)
> 
> Uhm.
> Using:
> 
>   _parse_help mysql | sort -u | sed -e 's/=//;s/\.$//' | grep -v 'variable-name'
> 
> I see only added variables, apart from skip-comments and skip-ssl. We can
> hardcode those two, I believe.
Yes, I probably forgot about underscore at that time.
I'm fine with hardcoding options missing from _parse_help.
But, initially, I didn't considered appending a space to option names.

I the bash completion guidelines argues in favor of appending '=' to
option names, I would then propose to use:

# if there is only one completion (ie: we know what we are going to
# append to the line), test if it ends with '=' and use
# compopt -o nospace in such a case
[[ ${#COMPREPLY[@]} -eq 1 ]] && \
   [[ $(grep -o '.$' <<<"${COMPREPLY[0]}") = '=' ]] && \
   compopt -o nospace

I just did it for wget and long options.

> 
> > [..]
> > > > - the patch previously mentioned about mplayer, options list and i18n
> > > 
> > > I'm not entirely sure about this. I don't understand why LC_LANG can't be
> > > used -- but usually "LANG=C command" works everywhere. If it works, I'd
> > > prefer this way, since otherwise we should hardcode every language mplayer
> > > is/will be translated to :)
> > 
> > I looked at the $(strings mplayer). English strings appears but it seems
> > to be only the untranslated strings.
> > The 'Name.*Min Max' string IS translated. It confirms what says
> > mplayer's ./configure:
> > 	  --language=lang		 default language to use [en]
> > 	  (The LINGUAS environment variable is honored).
> > Should we ask mplayer devs to put a hint character to help parsing ?
> > or use sed 1,3d ? :)
> 
> I still can't understand.
It is the alternative to sed '1,/Name/': hardcoding the number of lines
the help header contains: 3, it is language independant but less
independant of the mplayer version.

> 
> What does "LANG=C mplayer -list-options" yield? If it's still french, then it
> definitely is a mplayer bug :)
It prints "Nom Type Min Max Global CL Cfg".
I *compiled* mplayer, in french.
I agree that the help header should either show a mark either be in
english.

> 
> > I also forgot it above, I attached a completion for wget(1).
> 
> Again: mixed tabs and spaces. :/
fixed

> 
> "_parse_help wget" seems much better than "wget -h | sed -n
> 's/^[ [:blank:]]*\(-[a-zA-A46]\).*$/\1/p')" (remember that we prefer completing
> long options)
ok, fixed

(http://gitorious.org/drzraf/bash-completion/commit/a58e2c390aaa8ace7d30a0279fed37f0e16dac91)
(http://gitorious.org/drzraf/bash-completion/commit/7cc95d6b4287d1dacc1d91608a87ca5d49fe1edd)

[ new file attached ]

> 
> I just woke up, and am unable to fully understand the COMP_WORDBREAKS (and
> others) hackery done in the completion :)
> So I'll just skip that now, and review it again ASAP (if no other team member
> beats me at it :))

The --restrict-file-names option concern is about restricting the
completions to strings not yet used. Indeed, it's not clean code.

Good luck and have a nice Sunday.

Raph
-------------- next part --------------
# wget(1) completion for bash 4
# Raphaël Droz <raphael.droz+floss at gmail.com>
# License: GNU GPL v2 or later
# $Id: wget,v 0.5 2011/11/06 23:18:16 drzraf Exp $

have wget || return

_wget() {
    local cur prev words cword
    _init_completion -n = || return

    # If `cur' is long option containing `=', reassign prev and cur
    # because we used _get_comp_words_by_ref -n
    # it allows both form of long option ('=' and ' ')
    # if [[ "$cur" == --*=* ]]; then
    # 	prev=${cur/=*}
    # 	cur=${cur/*=}
    # fi

    # TODO: test this instead of the above
    _split_longopt

    case $prev in
	--progress)
	    COMPREPLY=( $( compgen -W 'bar dot' -- $cur ) )
	    return 0
	    ;;
	-D|-@(bind-address|@(exclude-|)domains))
	    _known_hosts_real
	    return 0
	    ;;
	--restrict-file-names)
	    local excludes
	    [[ "${cur}" =~ (unix|windows), ]] && excludes="|windows|unix"
	    [[ "${cur}" =~ (low|upp)ercase, ]] && excludes+="|lowercase|uppercase"
	    [[ "${cur}" =~ nocontrol ]] && excludes+="|nocontrol"
	    [[ "${cur}" =~ ascii ]] && excludes+="|ascii"
	    local lastopt=${cur/*,}
	    # non-workaround 1
	    # [[ ! "$COMP_WORDBREAKS =~ , ]] && WORDBREAKS=${COMP_WORDBREAKS},
	    # COMP_WORDBREAKS=${WORDBREAKS} COMPREPLY=( $( compgen -X "@(${excludes/|})" -W 'unix windows nocontrol ascii lowercase uppercase' -- $lastopt ) )

	    # workaround 2
	    local prevopt=
	    [[ "${cur}" =~ , ]] && prevopt=${cur%,*}
	    # prevopt is the previous options string used as a prefix
	    # to avoid COMPREPLY replacing them with the $lastopt completion
	    [ -n "$prevopt" ] && prevopt+=,
	    compopt -o nospace
	    COMPREPLY=( $( compgen -S ',' -P "${prevopt}" -X "@(${excludes/|})" -W 'unix windows nocontrol ascii lowercase uppercase' -- $lastopt ) )

	    # trying here to make the last option append a space (no more comma-separated option available)
	    #[[ ${#COMPREPLY[@]} -eq 0 ]] && {
	    #	compopt +o nospace
	    #	COMPREPLY=( $( compgen -P "${prevopt}" -X "@(${excludes/|})" -W 'unix windows nocontrol ascii lowercase uppercase' -- $lastopt ) )
	    #}
	    return 0
	    ;;
	--prefer-family)
	    COMPREPLY=( $( compgen -W 'IPv4 IPv6 none' -- $cur ) )
            return 0
            ;;
        -P|--directory-prefix|--ca-directory)
            # check if the line below is needed (should not)
            # compopt -o filenames
            _filedir -d
            return 0
            ;;
        -a|--load-cookies|--save-cookies|--post-file|--@(ca-|)certificate|--private-key|--random-file|--egd-file|--append-output|--default-page)
	    _filedir
	    return 0
	    ;;
	-i|--input-file)
	    _filedir && COMPREPLY=( "${COMPREPLY[@]}" - )
	    return 0
	    ;;
	--secure-protocol)
	    COMPREPLY=( $( compgen -W 'auto SSLv2 SSLv3 TLSv1' -- $cur ) )
	    return 0
	    ;;
	--@(certificate|private-key)-type)
	    COMPREPLY=( $( compgen -W 'PEM DER' -- $cur ) )
	    return 0
	    ;;
	--@(follow|ignore)-tags)
	    COMPREPLY=( $( compgen -W 'a abbr acronym address applet area b base basefont bdo big blockquote \
				body br button caption center cite code col colgroup dd del dir div dfn dl dt em fieldset \
				font form frame frameset h6 head hr html i iframe img input ins isindex kbd label legend \
				li link map menu meta noframes noscript object ol optgroup option p param pre q s samp \
				script select small span strike strong style sub sup table tbody td textarea tfoot th \
				thead title tr tt u ul var xmp' -- $cur ) )
	    return 0
	    ;;
	--tries|--@(dns-|connect-|)timeout|--limit-rate|--wait|--waitretry|--cut-dirs|--max-redirect)
	    COMPREPLY=( $( compgen -W "{0..9}" -- $cur ) )
	    return 0
	    ;;
	--quota)
	    COMPREPLY=( $( compgen -W "{0..9}{k,m}" -- $cur ) )
	    return 0
	    ;;
	--@(http-|proxy-|ftp-|)user)
	    COMPREPLY=( $( compgen -W "$(sed -n '/^login/s/^[[:blank:]]*login[[:blank:]]//p' ~/.netrc)" -- $cur ) )
	    return 0
	    ;;
	--level)
	    COMPREPLY=( $( compgen -W "{1..5}" -- $cur ) )
            return 0
            ;;
	--header)
	    COMPREPLY=( $( compgen -W 'Accept Accept-Charset Accept-Encoding Accept-Language \
				Accept-Ranges Age Allow Authorization Cache-Control Connection Content-Encoding \
				Content-Language Content-Length Content-Location Content-MD5 Content-Range \
				Content-Type Date ETag Expect Expires From Host If-Match If-Modified-Since \
				If-None-Match If-Range If-Unmodified-Since Last-Modified Location Max-Forwards \
				Pragma Proxy-Authenticate Proxy-Authorization Range Referer Retry-After \
				Server TE Trailer Transfer-Encoding Upgrade User-Agent Vary Via Warning \
				WWW-Authenticate' -- $cur ) )
	    return 0
	    ;;
    esac

    case $cur in
	--*)
            COMPREPLY=( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) )
	    [[ ${#COMPREPLY[@]} -eq 1 && $(grep -o '.$' <<<"${COMPREPLY[0]}") = '=' ]] && \
		compopt -o nospace
	    return 0
	    ;;
	# only complete long options
	-)
	   compopt -o nospace
	   COMPREPLY=( -- )
	   return 0
	   ;;
	*)
	    compopt -o nospace
	    # option 1 :
	    # cur=`_get_cword :`
	    # COMPREPLY=( $( compgen -W "http{,s}://{,www.} ftp://" -- $cur ) )
	    # option 2 (TODO: not the long-term solution):
	    # see: https://lists.gnu.org/archive/html/bug-bash/2011-06/msg00032.html
	    export COMP_WORDBREAKS=${COMP_WORDBREAKS//:/}
	    COMPREPLY=( $( compgen -W "http{,s}://{,www.} ftp://" -- $cur ) )
            return 0
            ;;
    esac
    return 0
}
complete -F _wget wget
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20110612/65a839dd/attachment-0001.pgp>


More information about the Bash-completion-devel mailing list