con-libopt.bash

con-dev command line options bash library.

Author: absurd@olurdix.de
Date: 20240708
Copyright: GPL-2
Version: con-dev-0.3.3.snapshot20260830093111git51b7b7c+local
Manual section:3
Manual group:bash

SYNOPSIS

PATH="${PATH}:/usr/share/con-dev" . con-lib.bash
con::include opt

[ your code, main function and/or commands ]

con_opt::run "${@}"

DESCRIPTION

Abstraction around bash builtin getopts, with "comfort features" like:

Any program may have options, plus optionally either positional arguments or commands:

prog options [positionals|commands]

Workflow

  1. Usage and parse values are stored in global variables.
  2. init(): Resets these globals.
  3. parse(): Updated these globals from a usage text.
  4. parse(): Taints these globals form a arguments.

Shortcuts to set or get a global libopt value con_opt::completion ------------------- Print bash completion code (uff!).

EXAMPLES

The positionals and commands examples are usually installed in /usr/share/doc/con-dev/examples/.

Options and positionals

#!/bin/bash -e

# Example demonstrating con-libopt: Script with options and positionals.

PATH="${PATH}:/usr/share/con-dev" . con-lib.bash
con::include opt

positionals_version="1.2.3"
# Main usage. Main function will have these options available with the prefix 'main_opt_'.
positionals_usage="\
1st line of usage: Short description of the program.

Any further lines here NOT starting with \"-\" or \"<\" are detailed
documentation.

Detailed docs are only shown via \"-H\".

-a        : Simple option (access as \${main_opt_a}).
 Lines below an option line (NOT starting with \"-\" or \"<\") is detailed
 documentation for that option.
-b <host> : Simplest argument option (access as \${main_opt_b}).
 Flag 'type': Implements a check for the argument value (for possible
 types, see 'man 3 con-lib'); defaults to 'string' if not given.
 type=hostname
-c <var>  : Regex for option variables to print (access as \${main_opt_c}).
 type=variable
 default=c_default
-D        : Simple advanced (capital letter) option; you need \"-H\" to see me (access as \${main_opt_D}).
-E <var>  : The empty string may also be a default value.
 default=
-R <regex>: Regex for option variables to print.
 default=[^_]\+\$
<my-arg> : A positional option.
 Position argument strings act as id for the option. Special characters like '-'
 or '.' are allowed -- but will be replaced by underscores for the access
 variable; thus, this option would be accessed as \${main_opt_my_arg}.

 Flag 'completion': Overwrite bash completion code when implicit completion
 via type is not enough; value is used as 'compgen' argument:

 completion=-W \"\$(find /etc/ -maxdepth 1 2>/dev/null)\"

 Flag 'interactive': You will always be prompted for the value of this
 option (after parsing, before running). The caller may disable that by
 running with CON_AUTO_MODE=true (-A). Any option (option or
 positional) may have this flag.

 interactive=true

<token>...: A multi-positional option -- always must be the very last option.
 The supplied option variable will be a bash array.

 Each single item of the array is subject to type checking:
 type=string:tok_.*

 Multi default values start with the delimiter as very 1st char:
 default=,tok_default0,tok_default1"

# __init__: This is called before the main function is actually run.
# Use this (instead of merely putting global code before con_opt::run)
# for any somewhat more sophisticated code that might batter with usages
# where main is not run at all (like -h -H -B).
positionals:__init__()
{
        con::log:notice "Common init code."
}

# The main function (must be named like the program name).
positionals()
{
        con::opt:debug:printVars "${main_opt_R}"
}

con_opt::run "${@}"

Options and commands

#!/bin/bash -e

PATH="${PATH}:/usr/share/con-dev" . con-lib.bash
con::include opt

# Main usage
commands_usage="\
Example demonstrating con-libopt: Script with individual commands.

See example ``positionals`` for a more complete list of option variants.

-g        : Common main option.
-R <regex>: Regex for option variables to print.
 default=[^_]\+\$"

# Common usage for all commands.
commands_cmd_usage="\
-j: Common option for all commands."

# A 'top level' command
commands_cmd_status_usage="\
Print all variables set by libopt.

-s: Option for status only."
commands:cmd:status()
{
        con::opt:debug:printVars "${main_opt_R}"
}

# Group "net": Commands may be grouped (separating subcommands via "_")
# Groups may have a common usage as well.
commands_cmd_net_usage="\
Commands all around the network.

Adds nice and useless little commands around your
networks.

-i <iface>: Interface to use. Common option for the 'net' command group.
 type=choice:i-default:i-given:eth0:eth1:wlan0:wlan1
 default=i-default"

# Commands may have init functions on any level ("after cmd:")
commands:cmd:__init__()
{
        con::log:notice "Common init code for any command."
}

commands:cmd:net:__init__()
{
        con::log:notice "Init code for the net command group."
        declare -g COMMANDS_NET_INIT_CALLED="true"
}

commands_cmd_net_status_usage="\
Show network status."
commands:cmd:net_status()
{
        [ "${COMMANDS_NET_INIT_CALLED}" = "true" ]
        con::opt:debug:printVars "${main_opt_R}"
}

commands_cmd_net_up_usage="\
Bring the network up, then run <script>.

<script>: Run this script after network is up
 type=file"

commands:cmd:net_up()
{
        [ "${COMMANDS_NET_INIT_CALLED}" = "true" ]
        con::opt:debug:printVars "${main_opt_R}"
}

con_opt::run "${@}"

SEE ALSO

con-lib(3).