Skip to content

Shell wrapper providing task name shortcuts

Lele Gaifax edited this page Jan 12, 2021 · 5 revisions

What follows is simple Bash function I wrote and use, that allows me to use shorter aliases for the task names, 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

See also issue https://github.com/pydoit/doit/issues/73

Clone this wiki locally