Konubinix' opinionated web of thoughts

Clk Basic Bash Argument Format

Fleeting

clk basic bash argument format

When writing clk bash customcommand, you may have to provide options and arguments.

Those can be put in the help clk_usage command.

By default, it looks like the following.

  clk_usage () {
    cat<<EOF
$0

Description
--

EOF
}

The part after the -- is the location of the strings that will be parsed.

The format is simply colon separated values that indicate to clk what argument or options to parse.

arguments

  clk_usage () {
    cat<<EOF
$0

Description
--
A:arg-name:str:Some argument
EOF
}

This tells clk to add an argument called argname, which is a string with the documentation “Some argument”.

clk somecommand --help
Usage: clk somecommand [OPTIONS] ARG_NAME

  Description

  The current parameters set for this command are: --help

Arguments:
  ARG_NAME  Some argument

Options:
  --help-all  Show the full help message, automatic options included.
  --help      Show this message and exit.

The argument is available through the environment variable CLK___ARG_NAME.

The type can be either str, int, float or a list of choices with the syntax ["a", "b", "c"].

options

  clk_usage () {
    cat<<EOF
$0

Description
--
O:--opt-name:str:Some option
EOF
}
clk somecommand --help
Usage: clk somecommand [OPTIONS]

  Description

  The current parameters set for this command are: --help

Options:
  --opt-name TEXT  Some option
  --help-all       Show the full help message, automatic options included.
  --help           Show this message and exit.

Nothing fancy to tell here, expect that the option MUST start with dashes, like in click.

Access the option with CLK___OPT_NAME.

flag

Those as boolean options, hence there is no need to specify a type.

  clk_usage () {
    cat<<EOF
$0

Description
--
F:--flag-name:Some option
EOF
}
clk somecommand --help
Usage: clk somecommand [OPTIONS]

  Description

  The current parameters set for this command are: --help

Options:
  --flag-name  Some option  [default: False]
  --help-all   Show the full help message, automatic options included.
  --help       Show this message and exit.

Access the flag with CLK___FLAG_NAME.

remaining arguments

By default, clk won’t accept extra arguments, simply add the following line to let them pass through.

clk_usage () {
    cat<<EOF
$0

Description
--
N:content to say
EOF
}

There is no type checking in there and everything is accepted. Simply use ${*} to use them, as you would do in a traditional shell script.

To be noted that anything captured by clk won’t be put in the extra arguments, for instance consider the following description :

clk_usage () {
    cat<<EOF
$0

Description
--
A:arg:str:Some argument
O:--opt:str:Some option
N:content to say
EOF
}

clk_help_handler "$@"

echo "Arg: ${CLK___ARG}"
echo "Opt: ${CLK___OPT}"
echo "Remaining stuff: ${*}"
clk somecommand arg remaining --opt opt stuff
Arg: arg
Opt: opt
Remaining stuff: remaining stuff

Notes linking here