Commit f32feec7 authored by Emeric Verschuur's avatar Emeric Verschuur

Add enumeration type

parent 062518cc
......@@ -41,11 +41,12 @@ Declare each options and associated settings like name, arguments, etc. with ```
bashopts_declare option list:
- ```-n <val>```: Option/variable name (all option value will be stored into $<variable name>)
- ```-v <val>```: Default value
- ```-e <val>```: Bash expression: like default but this expression is computed on time and can contain variables and other bash expression
- ```-x <val>```: Bash expression: like default but this expression is computed on time and can contain variables and other bash expression
- ```-o <val>```: Short option name (one character, e.g.: 'h' for -h argument)
- ```-l <val>```: Long option (several characters, e.g.: 'my-opt' for --my-opt argument)
- ```-d <val>```: Option description
- ```-t <val>```: Value type: string, number, boolean (default)
- ```-t <val>```: Value type: string, enumeration, number, boolean (default)
- ```-e <val>```: Enum element: restrict possible values with a list of '-e <element>' options (you have to set one '-e <val>' by elements)
- ```-m <val>```: Method: set (DEFAULT: simple value where $<variable name> will be a simple value), add (list with several values where $<variable name> will be a bash array)
- ```-k <val>```: Custom check method (bash function like bashopts_check_number in bashopts.sh file)
- ```-r```: Value required
......
This diff is collapsed.
......@@ -10,11 +10,12 @@ trap 'bashopts_exit_handle' ERR
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 age -l number -d "Age" -t number
bashopts_declare -n email_list -t string -m add -l email -d "Email adress" -i
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" -x "\"\$FIRST_NAME \$LAST_NAME\""
bashopts_declare -n AGE -l number -d "Age" -t number
bashopts_declare -n MEANS_OF_TRANSPORTATION -l transport -t enum -e 'c|car' -e 'w|walk' -e 'b|bike' -e 't|transport' -v car -d "Means of transportation" -i
bashopts_declare -n EMAIL_LIST -t string -m add -l email -d "Email adress" -i
# Parse arguments
bashopts_parse_args "$@"
......@@ -24,10 +25,11 @@ bashopts_process_opts
# Display the values
echo
echo "First name: $first_name"
echo "Last name: $last_name"
echo "Display name: $display_name"
echo "Age: $age"
for email in "${email_list[@]}"; do
echo "First name: $FIRST_NAME"
echo "Last name: $LAST_NAME"
echo "Display name: $DISPLAY_NAME"
echo "Means of transportation: $MEANS_OF_TRANSPORTATION"
echo "Age: $AGE"
for email in "${EMAIL_LIST[@]}"; do
echo "Email: $email"
done
......@@ -52,7 +52,7 @@ _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" -s
bashopts_declare -n config_path -l config -o c -d "Configuration path" -t string -x "\$base_path/myconf.cfg" -s
bashopts_parse_args --base /tmp -n
bashopts_process_opts
......@@ -108,7 +108,7 @@ _test_case_5() {
_test_case_6() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n computed_value -e '$(( 2 + 3 * 4 ))' -t string
bashopts_declare -n computed_value -x '$(( 2 + 3 * 4 ))' -t string
bashopts_parse_args
bashopts_process_opts
......@@ -132,7 +132,7 @@ _test_case_7() {
_test_case_8() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -e '("value 1" "value 2")'
bashopts_declare -n array_value -l value -m add -d "Array value" -t string -x '("value 1" "value 2")'
bashopts_parse_args
bashopts_process_opts
......@@ -224,7 +224,7 @@ _test_case_15() {
rm -f /tmp/bashopts_testrc
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -e '("value 1" "value 2")'
bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -x '("value 1" "value 2")'
bashopts_parse_args --value ""
bashopts_process_opts > /dev/null <<< '( "value 1" "value 3" )'
......@@ -243,7 +243,7 @@ _test_case_16() {
rm -f /tmp/bashopts_testrc
bashopts_setup -n "$0" -d "Test case $0" -s /tmp/bashopts_testrc
bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -e '("value 1" "value 2")'
bashopts_declare -n array_value -l value -m add -d "Array value" -s -i -t string -x '("value 1" "value 2")'
bashopts_parse_args --value ""
bashopts_process_opts > /dev/null <<< '( "value 1" "value 2" )'
......@@ -277,11 +277,11 @@ _test_case_18() {
bashopts_parse_args
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru" \
| grep '^\[ERR')" "[ERRO] 'tru' is not a valid boolean value"
| grep '^\[ERR')" "[ERRO] Option a_boolean_value: 'tru' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "fa" \
| grep '^\[ERR')" "[ERRO] 'fa' is not a valid boolean value"
| grep '^\[ERR')" "[ERRO] Option a_boolean_value: 'fa' is not a valid boolean value"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "5" \
| grep '^\[ERR')" "[ERRO] '5' is not a valid boolean value"
| grep '^\[ERR')" "[ERRO] Option a_boolean_value: '5' is not a valid boolean value"
}
_test_case_19() {
......@@ -391,6 +391,22 @@ _test_case_27() {
"[CRIT] bashopts_declare: -r and -v options cannot be activated at the same time"
}
_test_case_28() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -m add -d "list of enum" -t enum -e '1|one' -e '2|two' -e '3|tree'
req_test_eq "$(bashopts_parse_args --value "4" -n 2>&1 >/dev/null \
| grep '^\[ERR')" "[ERRO] Option value: Invalid value '4' (accepted values are: 'one' 'two' 'tree')"
bashopts_parse_args --value "1" --value "two" -n
bashopts_process_opts
req_test_eq "${#value[@]}" "2"
req_test_eq "${value[0]}" "one"
req_test_eq "${value[1]}" "two"
req_test_eq "$(bashopts_get_valid_value_list --value | tr '\n' '#')" '"one"#"two"#"tree"#'
}
_test() {
bashopts_setup -n "$0" -d "Test case $0"
......
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