├── LICENSE ├── README.md ├── argparser.sh └── test.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Thanh Trung NGUYEN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Command-line arguments parser 2 | 3 | The script extract arguments from the arguments of the **script or function** and _auto-wire_ to shell variables 4 | 5 | Advantages over the traditional `getopts` : 6 | * less code to write thus boost development speed 7 | * variables are auto-populated so no manual assignment needed 8 | * every option has the visibility of other options so it's easier to do a combination ie `if -a and -b activated, then read --longarg` 9 | * options and order agnostic, can easily be used in any script without the need to declare `:a:b::cd`; it parses everything the user put into arguments (even garbages) 10 | * can be applied to top-level script, functions or even pure string 11 | 12 | ## How to use 13 | 14 | Include this library to your project using git submodule 15 | 16 | git submodule add https://github.com/flyingangel/argparser.git shell_modules/argparser 17 | 18 | Inside your script or even a function, simply call 19 | 20 | ```bash 21 | #make sure to include the script once then call parse_args 22 | source argparser.sh 23 | parse_args "$@" 24 | ``` 25 | 26 | ## Example 27 | 28 | ```bash 29 | testCommand -h \ 30 | --params \ 31 | --longarg \ 32 | --longarg_underscore \ 33 | --longargvalue=someVal \ 34 | --longargspace="space & equal=xyz" \ 35 | --longargfollow "value which follows" \ 36 | --longarg-dash \ 37 | --longarg-dash-value="dash value" \ 38 | --longarg-dash-follow "dash follow" \ 39 | simplearg "composite arg" \ 40 | -a -bc -D 41 | ``` 42 | 43 | Then at the beginning of your script or function 44 | 45 | ```bash 46 | parse_args "$@" 47 | 48 | #Now variables will be pre-populated within the environment with the following values 49 | $longarg = true 50 | $longarg_underscore = true 51 | $longargvalue = someVal 52 | $longargspace = space & equal=xyz 53 | $longargfollow = value which follows 54 | $longargDash = true 55 | $longargDashValue = dash value 56 | $longargDashFollow = dash follow 57 | 58 | $opta = true 59 | $optb = true 60 | $optc = true 61 | $optd = false 62 | $optD = true 63 | $optE = false 64 | 65 | $argument1 = simplearg 66 | $argument2 = composite arg 67 | 68 | #map -h to --help 69 | $opth = true 70 | $help = true 71 | 72 | #map -p to --params 73 | $optp = true 74 | $params = true 75 | 76 | #boolean opt can now easily be tested with 77 | if $opta; then do_smt fi 78 | if [ $optb == true ]; then do_smt fi 79 | [[ $optc ]] && do_case_active 80 | [[ ! $optd ]] && do_case_inactive 81 | $optd || do_case_inactive 82 | [[ $opta && $optb ]] && do_case_both_combine 83 | ``` 84 | 85 | All long opts will wire to the same variable name ie `--help` will populate `$help` 86 | All short opts will wire to variable with prefix **opt[letter]** i.e `-h` will populate `$opth` 87 | All arguments will wire to variable with prefix **argument[number]** i.e `$argument1, $argument2 ..` 88 | 89 | ## Config 90 | 91 | Define options mapping by configuring `$ARGPARSER_MAP`. Ex when we want to consider `-h` is the same as `--help` 92 | 93 | ```bash 94 | declare -A ARGPARSER_MAP 95 | ARGPARSER_MAP=( 96 | [h]=help 97 | [p]=params 98 | ) 99 | ``` 100 | 101 | Change short opt or argument prefix 102 | 103 | ```bash 104 | #default 105 | ARGPARSER_SHORT_PREFIX="opt" 106 | ARGPARSER_ARGUMENT_PREFIX="argument" 107 | ``` 108 | -------------------------------------------------------------------------------- /argparser.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Argument parse 3 | 4 | function parse_args() { 5 | local _i __i _argument _nextargument 6 | local count=0 7 | 8 | #scope only func 9 | #parse long args 10 | function parse_long() { 11 | local _arg _nextarg _name _value _i 12 | 13 | #strip -- 14 | _arg=${1#--} 15 | _nextarg=$2 16 | #strip after = 17 | _name="${_arg%%=*}" 18 | #if arg contains = 19 | if [[ $_arg == *=* ]]; then 20 | #strip before = 21 | _value="${_arg#*=}" 22 | 23 | #if next argument doesnt contain a dash it means it's the value 24 | elif [[ ! $_nextarg == -* ]]; then 25 | _value="$_nextarg" 26 | else 27 | _value=true 28 | fi 29 | 30 | #convert dash to camelcase 31 | if [[ $_name =~ "-" ]]; then 32 | _name=$(echo "$_name" | awk -F"-" '{for(i=2;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2)}} 1' OFS="") 33 | fi 34 | 35 | eval "$_name=\$_value" 36 | 37 | #set short if mapping is defined 38 | if [[ "${ARGPARSER_MAP[@]}" =~ "$_name" ]]; then 39 | for _i in "${!ARGPARSER_MAP[@]}"; do 40 | if [[ ${ARGPARSER_MAP[${_i}]} == "$_name" ]]; then 41 | _name=$ARGPARSER_SHORT_PREFIX$_i 42 | eval "$_name"=true 43 | fi 44 | done 45 | fi 46 | } 47 | 48 | #parse short args 49 | function parse_short() { 50 | local _i _arg _name _letter 51 | 52 | #strip - 53 | _arg=${1#-} 54 | 55 | #for each char 56 | for _i in $(seq 1 ${#_arg}); do 57 | _letter=${_arg:_i-1:1} 58 | _name=$ARGPARSER_SHORT_PREFIX$_letter 59 | eval "$_name"=true 60 | 61 | #set long if mapping is defined 62 | if [[ ${ARGPARSER_MAP[${_letter}]} ]]; then 63 | _name=${ARGPARSER_MAP[${_letter}]} 64 | eval "$_name"=true 65 | fi 66 | done 67 | } 68 | 69 | #parse argument 70 | function parse_argument() { 71 | local _name _value 72 | 73 | _name="$ARGPARSER_ARGUMENT_PREFIX$count" 74 | # shellcheck disable=SC2034 75 | _value=$1 76 | 77 | eval "$_name=\$_value" 78 | } 79 | 80 | #default short prefix if not configured 81 | [ -z "$ARGPARSER_SHORT_PREFIX" ] && ARGPARSER_SHORT_PREFIX="opt" 82 | 83 | #default argument prefix if not configured 84 | [ -z "$ARGPARSER_ARGUMENT_PREFIX" ] && ARGPARSER_ARGUMENT_PREFIX="argument" 85 | 86 | #init all short opt to false 87 | for _i in {{a..z},{A..Z}}; do 88 | _name=$ARGPARSER_SHORT_PREFIX$_i 89 | eval $_name=false 90 | done 91 | 92 | allargs=("$@") 93 | __i=0 94 | 95 | #analyze args 96 | for _i in "${!allargs[@]}"; do 97 | _argument=${allargs[$__i]} 98 | _nextargument=${allargs[$__i + 1]} 99 | 100 | case $_argument in 101 | --*) 102 | parse_long "$_argument" "$_nextargument" 103 | #skip next arg 104 | [[ ! $_nextargument == -* ]] && ((__i++)) 105 | ;; 106 | -*) 107 | parse_short "$_argument" 108 | ;; 109 | *) 110 | ((count++)) 111 | parse_argument "$_argument" 112 | ;; 113 | esac 114 | 115 | ((__i++)) 116 | done 117 | } 118 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source argparser.sh 4 | 5 | echo "Test case:" 6 | # shellcheck disable=SC1004 7 | echo 'testFunc -h \ 8 | --params \ 9 | --longarg \ 10 | --longarg_underscore \ 11 | --longargvalue=someVal \ 12 | --longargspace="space & equal=xyz" \ 13 | --longargfollow "value which follows" \ 14 | --longarg-dash \ 15 | --longarg-dash-value="dash value" \ 16 | --longarg-dash-follow "dash follow" \ 17 | simplearg "composite arg" \ 18 | -a -bc -D' 19 | echo 20 | echo 'parse_args "$@"' 21 | echo 22 | 23 | # shellcheck disable=SC2154 24 | function testFunc() { 25 | parse_args "$@" 26 | 27 | echo "\$longarg = $longarg" 28 | echo "\$longarg_underscore = $longarg_underscore" 29 | echo "\$longargvalue = $longargvalue" 30 | echo "\$longargspace = $longargspace" 31 | echo "\$longargfollow = $longargfollow" 32 | echo "\$longargDash = $longargDash" 33 | echo "\$longargDashValue = $longargDashValue" 34 | echo "\$longargDashFollow = $longargDashFollow" 35 | echo 36 | echo "\$opta = $opta" 37 | echo "\$optb = $optb" 38 | echo "\$optc = $optc" 39 | echo "\$optd = $optd" 40 | echo "\$optD = $optD" 41 | echo "\$optE = $optE" 42 | echo 43 | echo "\$argument1 = $argument1" 44 | echo "\$argument2 = $argument2" 45 | echo 46 | echo "Test mapping -h to --help" 47 | echo "\$opth = $opth" 48 | echo "\$help = $help" 49 | echo 50 | echo "Test mapping --params to -p" 51 | echo "\$optp = $optp" 52 | echo "\$params = $params" 53 | echo 54 | 55 | #Check method 1 : if true without bracket 56 | if $opta; then 57 | echo "-a activated" 58 | fi 59 | 60 | #Check method 2 : linear logic 61 | $optb && echo "-b activated" 62 | ! $optx && echo "-x not activated" 63 | 64 | #Chech method 3 : linear bracket 65 | [[ $optc ]] && echo "-c activated" 66 | [[ ! $optc ]] && echo "-c not activated" 67 | 68 | #Combine logic 69 | $optb && $optc && echo "-bc activated" 70 | 71 | #Inversion check : if ! $optd; then 72 | $optd || echo "-d not activated" 73 | 74 | #Check method 4 : traditional explicit 75 | if [ "$optD" == true ]; then 76 | echo "-D activated" 77 | fi 78 | } 79 | 80 | declare -A ARGPARSER_MAP 81 | ARGPARSER_MAP=( 82 | [h]=help 83 | [p]=params 84 | ) 85 | 86 | testFunc -h \ 87 | --params \ 88 | --longarg \ 89 | --longarg_underscore \ 90 | --longargvalue=someVal \ 91 | --longargspace="space & equal=xyz" \ 92 | --longargfollow "value which follows" \ 93 | --longarg-dash \ 94 | --longarg-dash-value="dash value" \ 95 | --longarg-dash-follow "dash follow" \ 96 | simplearg \ 97 | "composite arg" \ 98 | -a -bc -D 99 | --------------------------------------------------------------------------------