Skip to content

Aliasing with options

Ravi J edited this page Dec 27, 2020 · 7 revisions

One of the ways to let user defined aliases using qkw is to pass into a shell macro function the following arguments

runme(){
   `qkw -gC $1` ${@:2}
}

The above serves as a definition to be used in .bashrc. The first option in the function $1 is the <qkw-tag> and from the second option till the end, the application/function receives the arguments

In order to use the above at the CLI

runme <qkw-tag> <other-options>

As an example, consider ls -ltrh which is tagged with the following ls.1, and accepts directory parameter as the argument. Say the user needs to peek into the directory frequently, then

runme ls.1 /some/dir

would get the job done. While this is a trivial example, applying the same approach quoted above to complex CLI commands to applications like docker, kubernetes, kafka, etc helps simplify the workflows significantly.

Pushing in-between CLI options to the far right

Since the additional positional arguments are to be at the very end when using the macro, for the case when options are in between, one of the ways to make this possible is to have a script written as a function which in turn reads the options as if they are positional arguments.

Eg:

docker images --format "{{.Repository}}:{{.Tag}}" | grep ':latest' | xargs -L1 docker pull

where :latest is to be replaced, then put this line into a function, and attach to a qkw tag

f(){
  docker images --format "{{.Repository}}:{{.Tag}}" | grep $1 | xargs -L1 docker pull
}
f ":latest"

Binding this to qkw would help with converting the option into a variable in between the command.