#!/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 "${@}"
