Skip to content

Shell wrapper providing task name shortcuts

Emanuele Gaifas edited this page Oct 1, 2015 · 5 revisions

As explained in issue #73, even with the aid of TAB-completion of task names I find a bit annoying to type in those usually long strings, so I tried to implement a strategy I already used in a previous Paver-based project, that allowed me to say paver ddfm as an alias for paver do_that_for_me.

Since my approach did not meet Eduardo approval, I wrote and use the following simple dt (stands for do that...) Bash function that fulfills my needs without any change to the doit codebase.

It works by parsing the output of doit list: with a simple sed script it computes a regular expression that matches only the initial letter of a underscore-separated-list-of-words; if that matches one of the task names, it executes that task passing all the remaining arguments when the second argument (that is, the one right after the task name) is not either -h or --help: in such case it executes doit help original_task_name.

function dt() {
    local shortcut=${1:-list}
    local helpopt=$2

    if test "$shortcut" = "help"
    then
        shortcut=$helpopt
        helpopt="--help"
    fi

    local expr_rx=$(echo $shortcut | sed 's/\(.\)/\1[^_]*_/g')

    declare -A seen
    shift
    doit list | while read papabile helpdoc
    do
        case $shortcut in
            list)
                local alias=$(echo $papabile | sed 's/\(.\)[^_]*_*/\1/g')
                if test -z "${seen[$alias]}"
                then
                    echo -e "$alias, $papabile\n\t$helpdoc\n"
                    seen[$alias]=t
                fi
                ;;
            *)
                if test "$papabile" = "$shortcut" -o $(expr "$papabile" : "$expr_rx*") -gt 0
                then
                    if test "$helpopt" = "-h" -o "$helpopt" = "--help"
                    then
                        doit help $papabile
                    else
                        doit $papabile $*
                    fi
                    seen[$papabile]=t
                    break
                fi
                ;;
        esac
    done
}
complete -F _doit dt

As you can see, it can use the very same completion helper function generated by doit.

With dt list you get a slightly edited list of tasks, each one showing its standard name paired with the shortcut:

$ dt list
db, dc_build
	Rebuild needed containers.

de, dc_execute
	Execute an interactive command within a running container.

gacru, git_auto_commit_ref_updates
	Automatically commit submodules ref changes.
...

$ dt de -h
dc_execute  Execute an interactive command within a running container.
  -c ARG, --container=ARG   The name of the container, api by default
                            choices: api, apitests, appdata, [...edited out...]
  -e ARG, --execute=ARG     The command to execute, by default /bin/bash

Clone this wiki locally