[Bash-completion-devel] Bash completion for lpr, and lprm

Marcin Szamotulski mszamot at gmail.com
Mon Mar 7 19:52:54 UTC 2011


Hello,

I wrote a bash completion script for lpr (lp) and lprm commands.

Best regards,
Marcin Szamotulski
-------------- next part --------------
# lpr(1) and lprm(1) completions by Marcin Szamotulski <mszamot at gmail.com> March 2011

# This script uses two functions from 'base': _known_hosts_real and _user_at_host.
# to complete -H and -U switches of the lpr command (-h and -U of lprm).

_lpr() 
{
    local cur prev opts comp_opts configfile
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    comp_opts="+o default +o filenames"

    if [[ ${prev} == "=" ]];then 
	prev="${COMP_WORDS[COMP_CWORD-2]}"
    fi

    if [[ ${cur} == "-" ]];then     
	opts="-E -H -C -T -J -P -U -# -h -l -m -o -p -q -r"
	COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    else
    case ${prev} in
	-o) 
	    opts="scaling= media= sides= Collate= orientation-requested= job-sheets= job-hold-until= page-ranges= page-set= number-up= page-border= number-up-layout= outputorder= cpi= lpi= ppi= columns= page-left= page-right= page-top= page-bottom= position= natural-scaling= hue= saturation= penwidth= landscape mirror raw fitplot prettyprint nowrap blackplot";;
	sides)
	    opts="two-sided-long-edge two-sided-short-edge one-sided";;
	Collate)
	    opts="True False";;
	job-hold-until)
	    opts="indefinite day-time night second-shift third-shift weekend";;
	job-sheets)
	    opts="none classified confidential secret standard topsecret unclassified";;
	media)
	    opts="Letter Legal A4 COM10 DL Transparency Upper Lower MultiPurpose LargeCapacity Custom\.";;
	number-up-layout)
	    opts="btlr btrl lrbt lrtb rlbt rltb tblr tbrl";;
	outputorder)
	    opts="normal reverse";;	    
	page-border)
	    opts="double none double-thick single single-thick";;
	page-set)
	    opts="odd even";;
	position)
	    opts="center top left right top-left top-right bottom bottom-left bottom-right";;
	orientation-requested)
	    opts="3 4 5 6";;
	-P)
	    opts=`lpstat -a | awk '{print $1}'`;;
	-i) 
	    opts=`lpstat -R | awk '{print $2}' | sed 's/^[a-zA-Z0-9\-]*\-//'`;;
	-H)
	    comp_opts="no compreply"
	    _known_hosts_real "$cur";;
	-U)
	    comp_opts="no compreply"
	    _user_at_host;;
	-C)
	    comp_opts="-o filenames";;
	-J)
	    comp_opts="-o filenames";;
	-T)
	    comp_opts="-o filenames";;
	-#)
	    comp_opts="-o filenames";;
	*)
	    opts=""
	    if [[ ${cur} != "=" ]];then
		compopt +o filenames
		_filedir
		comp_opts="no compreply"
	    else
		return 0
	    fi;;

    esac
    fi

    [[ ${cur} == "=" ]] && cur="" 
    if [[ ${comp_opts} != "no compreply" ]];then 
	if [[ ${comp_opts} != "" ]];then
	    compopt ${comp_opts}
	fi
	COMPREPLY=( $(compgen  -W "${opts}" -- ${cur}) );
    fi
 
    # For options from opt_array do not append a white space after completin
    opt_array=("scaling media sides Collate orientation-requested job-sheets job-hold-until page-ranges page-set number-up page-border number-up-layout outputorder cpi lpi ppi columns page-left page-right page-top page-bottom position natural-scaling hue saturation penwidth")
    nospace=0
    for i in ${COMPREPLY};do
	for j in ${opt_array};do
	    if [[ $i =~ $j ]];then 
		nospace=1; 
	    fi
	done
    done
    [[ $nospace == 1 ]] && compopt -o nospace 

    return 0;
}
complete -F _lpr lpr lp

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

    [[ ${prev} == "=" ]] && prev="${COMP_WORDS[COMP_CWORD-2]}"

    if [[ ${cur} == "-" ]];then     
	opts="-E -P -U -h"
	COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    else
	case ${prev} in
	    -P)
		opts=`lpstat -a | awk '{print $1}'`
		COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );;
	    -h)
		_known_hosts_real "$cur";;
	    -U)
		_user_at_host;;
	    *)
		opts=`lpstat -R | awk '{print $2}' | sed 's/.*-\([0-9\-]*\)$/\1/'`
		COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );;
	esac
    fi

    return 0;
}

complete -F _lprm lprm


More information about the Bash-completion-devel mailing list