Commit 14f88ab2 authored by Emeric Verschuur's avatar Emeric Verschuur

Merge branch 'bashopts_process_opt' into 'master'

Bashopts process opt

See merge request !1
parents f8698895 dbfcaa37
Pipeline #51 passed with stage
in 15 seconds
......@@ -8,7 +8,7 @@ There are mainly 5 steps:
- Setup bashopts with ```bashopts_setup``` function
- Declare options/arguments with ```bashopts_declare``` (one call per option)
- Parse arguments with ```bashopts_parse_args```
- Process arguments with ```bashopts_process_args```
- Process arguments with ```bashopts_process_opts```
### Load the library
Load all the function from the ```bashopts.sh``` file and you can add the line ```trap 'bashopts_exit_handle' ERR``` to display backtrace on error
......@@ -26,7 +26,9 @@ bashopts_setup option list:
- ```-d <val>```: Tool description
- ```-u <val>```: Tool usage (optional)
- ```-s <val>```: Define a setting file path (optional)
- ```-f```: Force edition for interactive options (default: a prompt is displayed only if not given via the command line)
- ```-y```: Set non interactive mode as the default mode (optional)
- ```-x```: Disable entirely interactive mode (optional)
- ```-p```: Force value storage even if the value is equal to the default one (optional)
Code example:
```bash
......@@ -71,7 +73,7 @@ bashopts_parse_args "$@"
```
### Argument processing
Process all the options with ```bashopts_process_args```
Process all the options with ```bashopts_process_opts```
This function will fill all options values using the following order:
- Value from the command line
......@@ -81,9 +83,9 @@ This function will fill all options values using the following order:
- The default option value (may be defined with the ```bashopts_declare -v```)
bashopts_process_args usage:
bashopts_process_opts usage:
```bash
bashopts_process_args
bashopts_process_opts
```
### Option exploitation
......@@ -105,20 +107,20 @@ bashopts_process_args
trap 'bashopts_exit_handle' ERR
# Initialize the library
bashopts_setup -n "$0" -d "This is myapp tool description displayed on help message" -s "$HOME/.config/myapprc"
bashopts_setup -n "example.sh" -d "This is myapp tool description displayed on help message" -s "$HOME/.config/myapprc"
# Declare the options
bashopts_declare -n first_name -l first -o f -d "First name" -t string -i -s -r
bashopts_declare -n last_name -l last -o l -d "Last name" -t string -i -s -r
bashopts_declare -n display_name -l display-name -t string -d "Display name" -e "\$first_name \$last_name"
bashopts_declare -n display_name -l display-name -t string -d "Display name" -e "\"\$first_name \$last_name\""
bashopts_declare -n age -l number -d "Age" -t number
bashopts_declare -n email_list -t string -m add -l email -d "Email adress"
# Parse arguments
bashopts_parse_args "$@"
# Process argument
bashopts_process_args
# Process options
bashopts_process_opts
# Display the values
echo
......@@ -139,17 +141,17 @@ $ ./example.sh --help
Output:
```
NAME:
./example.sh - This is myapp tool description displayed on help message
example.sh - This is myapp tool description displayed on help message
USAGE:
[options and commands] [-- [extra args]]
example.sh [options and commands] [-- [extra args]]
OPTIONS:
-h,--help Display this help
-n,--non-interactive true Non interactive mode - [$bashopts_non_interactive] (type:boolean, default:false)
-n,--non-interactive false Non interactive mode - [$bashopts_non_interactive] (type:boolean, default:false)
-f,--first "John" First name - [$first_name] (type:string, default:"")
-l,--last "Smith" Last name - [$last_name] (type:string, default:"")
--display-name "John Smith" Display name - [$display_name] (type:string, default:"$first_name $last_name")
--display-name "John Smith" Display name - [$display_name] (type:string, default:"\"$first_name $last_name\"")
--number 0 Age - [$age] (type:number, default:0)
--email Email adress - [$email_list] (type:string, default:"")
```
......
This diff is collapsed.
......@@ -12,15 +12,15 @@ bashopts_setup -n "example.sh" -d "This is myapp tool description displayed on h
# Declare the options
bashopts_declare -n first_name -l first -o f -d "First name" -t string -i -s -r
bashopts_declare -n last_name -l last -o l -d "Last name" -t string -i -s -r
bashopts_declare -n display_name -l display-name -t string -d "Display name" -e "\$first_name \$last_name"
bashopts_declare -n display_name -l display-name -t string -d "Display name" -e "\"\$first_name \$last_name\""
bashopts_declare -n age -l number -d "Age" -t number
bashopts_declare -n email_list -t string -m add -l email -d "Email adress"
# Parse arguments
bashopts_parse_args "$@"
# Process argument
bashopts_process_args
# Process options
bashopts_process_opts
# Display the values
echo
......
......@@ -52,10 +52,10 @@ _test_case_2() {
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n base_path -l base -o b -d "Base path" -t string -v "/opt/test" -i -s
bashopts_declare -n config_path -l config -o c -d "Configuration path" -t string -e "\$base_path/myconf.cfg" -i -s
bashopts_declare -n config_path -l config -o c -d "Configuration path" -t string -e "\$base_path/myconf.cfg" -s
bashopts_parse_args --base /tmp -n
bashopts_process_args
bashopts_process_opts
req_test_eq "$config_path" "/tmp/myconf.cfg"
req_test_eq "$(cat /tmp/bashopts_testrc)" "base_path=/tmp"
......@@ -68,7 +68,7 @@ _test_case_3() {
bashopts_declare -n process_file_list -l input-file -o f -d "Input file" -t string -m add -s
bashopts_parse_args --input-file foobar --input-file=/foo/bar
bashopts_process_args
bashopts_process_opts
req_test_eq "$(bashopts_join_by , ${process_file_list[@]})" "foobar,/foo/bar"
eval declare -x test="$(grep -E "^process_file_list=" /tmp/bashopts_testrc | sed -E "s/^[^=]+=//g")"
......@@ -83,7 +83,7 @@ _test_case_4() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
bashopts_parse_args --value "value1" --value "value 2" --value "value '\"' \$3"
bashopts_process_args
bashopts_process_opts
req_test_eq "${array_value[0]}" "value1"
req_test_eq "${array_value[1]}" "value 2"
......@@ -100,7 +100,7 @@ _test_case_5() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -s
bashopts_parse_args
bashopts_process_args
bashopts_process_opts
req_test_eq "${#array_value[@]}" "0"
}
......@@ -111,7 +111,7 @@ _test_case_6() {
bashopts_declare -n computed_value -e '$(( 2 + 3 * 4 ))' -t string
bashopts_parse_args
bashopts_process_args
bashopts_process_opts
req_test_eq "$computed_value" "14"
}
......@@ -122,7 +122,7 @@ _test_case_7() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t string
bashopts_parse_args --value "value 1" --value "value 1" --value "" --value "value 3" --value "value 4"
bashopts_process_args
bashopts_process_opts
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 3"
......@@ -135,7 +135,7 @@ _test_case_8() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -e '("value 1" "value 2")'
bashopts_parse_args
bashopts_process_args
bashopts_process_opts
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 1"
......@@ -146,7 +146,7 @@ _test_case_9() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_parse_args command1 "command 2"
bashopts_process_args
bashopts_process_opts
req_test_eq "${#bashopts_commands[@]}" "2"
req_test_eq "${bashopts_commands[0]}" "command1"
......@@ -159,7 +159,7 @@ _test_case_10() {
bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'
bashopts_parse_args --value=test
req_test_eq "$(bashopts_process_args 2>&1)" "[WARNING] No settings file specified"
req_test_eq "$(bashopts_process_opts 2>&1)" "[WARNING] No settings file specified"
}
_test_case_11() {
......@@ -168,7 +168,7 @@ _test_case_11() {
bashopts_declare -n simple_val -s -l value -d "Simple value" -t string -v 'val'
bashopts_parse_args --value=test
req_test_eq "$(bashopts_process_args 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file"
req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file"
}
_test_case_12() {
......@@ -177,7 +177,7 @@ _test_case_12() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -i
bashopts_parse_args
bashopts_process_args > /dev/null <<< "( \"value 1\" \"value 2\" )"
bashopts_process_opts > /dev/null <<< "( \"value 1\" \"value 2\" )"
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 1"
......@@ -190,7 +190,7 @@ _test_case_13() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t number -i
bashopts_parse_args
bashopts_process_args > /dev/null <<< "( 3 -8 )"
bashopts_process_opts > /dev/null <<< "( 3 -8 )"
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "3"
......@@ -203,11 +203,21 @@ _test_case_14() {
bashopts_declare -n array_value -l value -m add -d "Array value" -t boolean -i
bashopts_parse_args
bashopts_process_args > /dev/null <<< "( true false )"
bashopts_process_opts > /dev/null <<< "( true false 1 True TRUE t T 0 False FALSE f F )"
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${#array_value[@]}" "12"
req_test_eq "${array_value[0]}" "true"
req_test_eq "${array_value[1]}" "false"
req_test_eq "${array_value[2]}" "true"
req_test_eq "${array_value[3]}" "true"
req_test_eq "${array_value[4]}" "true"
req_test_eq "${array_value[5]}" "true"
req_test_eq "${array_value[6]}" "true"
req_test_eq "${array_value[7]}" "false"
req_test_eq "${array_value[8]}" "false"
req_test_eq "${array_value[9]}" "false"
req_test_eq "${array_value[10]}" "false"
req_test_eq "${array_value[11]}" "false"
}
_test_case_15() {
......@@ -217,7 +227,7 @@ _test_case_15() {
bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -e '("value 1" "value 2")'
bashopts_parse_args --value ""
bashopts_process_args > /dev/null <<< '( "value 1" "value 3" )'
bashopts_process_opts > /dev/null <<< '( "value 1" "value 3" )'
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 1"
......@@ -236,7 +246,7 @@ _test_case_16() {
bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -e '("value 1" "value 2")'
bashopts_parse_args --value ""
bashopts_process_args > /dev/null <<< '( "value 1" "value 2" )'
bashopts_process_opts > /dev/null <<< '( "value 1" "value 2" )'
req_test_eq "${#array_value[@]}" "2"
req_test_eq "${array_value[0]}" "value 1"
......@@ -254,16 +264,97 @@ _test_case_17() {
bashopts_declare -n a_value -l value -d "A value" -s -i -t string -v "default value"
bashopts_parse_args
bashopts_process_args > /dev/null <<< ""
bashopts_process_opts > /dev/null <<< ""
req_test_eq "$a_value" "default value"
req_test_eq "$(cat /tmp/bashopts_testrc)" "a_value='default value'"
}
_test_case_18() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n a_boolean_value -l boolean -m set -d "A boolean value" -t boolean -i
bashopts_parse_args
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru")" "[ERROR] 'tru' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa")" "[ERROR] 'fa' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5")" "[ERROR] '5' is not a valid boolean value"
}
_test_case_19() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n a_boolean_value -o b -m set -d "A boolean value" -t boolean
bashopts_parse_args -b
bashopts_process_opts
req_test_eq "$a_boolean_value" "true"
}
is_eq_to_null() {
if [ "$1" == "NULL" ];then
echo "$1"
return 0
fi
bashopts_error "Not equal to NULL"
return 1
}
_test_case_20() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -m add -d "A str value" -t string
bashopts_parse_args
bashopts_process_opts
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "na")" \
| grep '\[ERROR\] Not equal to NULL' \
| head -n 1 || true)" '[ERROR] Not equal to NULL'
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_eq_to_null -r 2>&1 > /dev/null <<< "()")" \
| grep '\[ERROR\] At least one value required' \
| head -n 1 || true)" '[ERROR] At least one value required'
bashopts_process_option -n value -k is_eq_to_null -r > /dev/null 2>&1 <<< "NULL"
}
_test_case_21() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -m add -d "A str value" -t string
bashopts_parse_args -n
bashopts_process_opts
req_test_eq "$(echo "$(bashopts_process_option -n value -k is_not_empty -r 2>&1 > /dev/null)" \
| grep '\[ERROR\] At least one value required' \
| head -n 1)" '[ERROR] At least one value required'
}
_test_case_22() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -m add -d "A str value" -t string -i
bashopts_parse_args --value "NULL" -n
bashopts_process_opts
bashopts_process_option -n value -k is_eq_to_null -r
}
_test() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -m add -d "A str value" -t string -i
bashopts_parse_args --value "NULL" -n
bashopts_process_opts
bashopts_process_option -n value -k is_eq_to_null -r
}
if [ ${#} -eq 0 ]; then
echo
for t in $(grep -E -o '_test_case_\w+\>' $0); do
echo "=> Test case $t"
echo "### Test case $t ###"
$0 $t
echo
done
fi
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment