diff --git a/README.md b/README.md index 151c3f4..87b6689 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,22 @@ This function is used to tell the library what command line arguments you accept cmdarg 'u:' 'source_ldap_username' 'Source (old) LDAP Username' cmdarg 'c:' 'groupmap' 'A CSV file mapping usernames to groups that they should belong to post-conversion' '' 'test -e $OPTARG' -All arguments are OPTIONAL by default. An argument that has ':' on the end of its single character option, and does not specify a default value (empty string is considered "not specified"), is REQUIRED. The arguments can be set on the command line either via '-X' or '--Y', where X is the short option and Y is the long option. Example: +The first argument to cmdarg must be an argument specification. Argument specifications take the form 'NOT', where: + +- N : The single letter Name of the argument +- O : Whether the option is optional or not. Use ':' here for a required argument, '?' for an optional argument. If you provide a default value for a required argument (:), then it becomes optional. +- T : The type. Leave empty for a string argument, use '[]' for an array argument, use '{}' for a hash argument. + +If O and T are both unset, and only the single letter N is provided, then the argument is a boolean argument which will default to false. + +The arguments can be set on the command line either via '-X' or '--Y', where X is the short option and Y is the long option. Example: cmdarg 'r:' 'required-thing' 'Some thing I require' + cmdarg 'o?' 'optional-thing' 'Some optional thing' + cmdarg 'b' 'boolean-thing' 'Some boolean thing' - # your_script.sh -r some_thingy - # your_script.sh --required-thing some_thingy + # your_script.sh -r some_thingy -b -o optional_thing + # your_script.sh --required-thing some_thingy --boolean-thing Because cmdarg does key off of the short options, you are limited to as many unique single characters are in your character set (likely 61 - 26 lower & upper alpha, +9 numerics). @@ -169,8 +179,8 @@ You can use the cmdarg function to accept arrays and hashes from the command lin declare -a array declare -A hash - cmdarg 'a:[]' 'array' 'Some array you can set indexes in' - cmdarg 'H:{}' 'hash' 'Some hash you can set keys in' + cmdarg 'a?[]' 'array' 'Some array you can set indexes in' + cmdarg 'H?{}' 'hash' 'Some hash you can set keys in' your_script -a 32 --array something -H key=value --hash other_key=value @@ -206,4 +216,4 @@ cmdarg does not use getopt or getopts for option parsing. Its parser is written Tests ===== -cmdarg is testable by the shunit bash unit testing tool. See the tests/ directory. +cmdarg is testable by the shunit bash unit testing tool (https://www.github.com/akesterson/shunit/). See the tests/ directory. diff --git a/cmdarg.sh b/cmdarg.sh index 761abc2..a099ed3 100644 --- a/cmdarg.sh +++ b/cmdarg.sh @@ -8,7 +8,8 @@ fi CMDARG_FLAG_NOARG=0 -CMDARG_FLAG_WITHARG=1 +CMDARG_FLAG_REQARG=1 +CMDARG_FLAG_OPTARG=2 CMDARG_TYPE_ARRAY=1 CMDARG_TYPE_HASH=2 @@ -42,8 +43,12 @@ function cmdarg exit 1 fi - if [[ "${1:1:1}" == ":" ]]; then - CMDARG_FLAGS[$shortopt]=$CMDARG_FLAG_WITHARG + declare -xA argtypemap + argtypemap[':']=$CMDARG_FLAG_REQARG + argtypemap['?']=$CMDARG_FLAG_OPTARG + argtype=${1:1:1} + if [[ "$argtype" != "" ]]; then + CMDARG_FLAGS[$shortopt]=${argtypemap["$argtype"]} if [[ "${1:2:4}" == "[]" ]]; then declare -p ${key} >/dev/null 2>&1 if [[ $? -ne 0 ]]; then @@ -71,7 +76,7 @@ function cmdarg CMDARG_REV["$2"]=$shortopt CMDARG_DESC["$shortopt"]=$3 CMDARG_DEFAULT["$shortopt"]=${4:-} - if [[ ${CMDARG_FLAGS[$shortopt]} -eq $CMDARG_FLAG_WITHARG ]] && [[ "${4:-}" == "" ]]; then + if [[ ${CMDARG_FLAGS[$shortopt]} -eq $CMDARG_FLAG_REQARG ]] && [[ "${4:-}" == "" ]]; then CMDARG_REQUIRED+=($shortopt) else CMDARG_OPTIONAL+=($shortopt) @@ -258,7 +263,7 @@ function cmdarg_parse exit 1 fi - if [[ ${CMDARG_FLAGS[$opt]} -eq $CMDARG_FLAG_WITHARG ]]; then + if [[ ${CMDARG_FLAGS[$opt]} -eq $CMDARG_FLAG_REQARG ]] || [[ ${CMDARG_FLAGS[$opt]} -eq ${CMDARG_FLAG_OPTARG} ]]; then optarg=$1 shift fi