Commit 83ec9852 authored by Emeric Verschuur's avatar Emeric Verschuur

Merge branch 'new_features' into 'master'

New features

See merge request !6
parents b099d64e fb0f5e5c
Pipeline #94 passed with stage
in 18 seconds
# Changelog
## 2.0.0
* Add version and Changelog
* Add enum type
* Improve list property input
* Replace message functions by bashopts_log function
* Consider invalid default value as a non fatal error
## 1.3.1
* Minor fix
## 1.3.0
* add BASH version check
* generic option var name to uppercase
* add bashopts_diplay_help_delayed
## 1.2.3
* [FIX] column width detection
## 1.2.2
* no need anymore to add -i for shell width detection
* spelling fixes
## 1.2.1
* [FIX] add test case 25, 26 & 27: fix required value
## 1.2
* [FIX] add test case 24 & minor upadtes
* Fix interactive not re ask in check failure
* Add bashopts_export_opts function
* README update
* Remove deprecated function
## 1.1.2
* Fix default value check for the add method
## 1.1.1
* Create parent settings file directory if not exists
## 1.1.0
* Add mode non interactive as default mode option
* Option process rewrite
## 1.0.0
* First stable release
......@@ -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"
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
......@@ -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_opts 2>&1)" "[WARNING] No settings file specified"
req_test_eq "$(bashopts_process_opts 2>&1)" "[WARN] 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_opts 2>&1 | grep 'WARNING')" "[WARNING] Please check the settings file"
req_test_eq "$(bashopts_process_opts 2>&1 | grep 'WARN')" "[WARN] Please check the settings file"
}
_test_case_12() {
......@@ -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" )'
......@@ -276,9 +276,12 @@ _test_case_18() {
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"
req_test_eq "$(bashopts_process_opts 2>&1 >/dev/null <<< "tru" \
| 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] 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] Option a_boolean_value: '5' is not a valid boolean value"
}
_test_case_19() {
......@@ -297,7 +300,7 @@ is_eq_to_null() {
echo "$1"
return 0
fi
bashopts_error "Not equal to NULL"
bashopts_log E "Not equal to NULL"
return 1
}
......@@ -309,11 +312,9 @@ _test_case_20() {
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'
| grep '^\[ERR' | tail -n 1 || true)" '[ERRO] 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'
| grep '^\[ERR' | tail -n 1 || true)" '[ERRO] At least one value required'
bashopts_process_option -n value -k is_eq_to_null -r > /dev/null 2>&1 <<< "NULL"
}
......@@ -325,8 +326,7 @@ _test_case_21() {
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'
| grep '^\[ERR' | head -n 1)" '[ERRO] At least one value required'
}
_test_case_22() {
......@@ -388,7 +388,31 @@ _test_case_27() {
bashopts_setup -n "$0" -d "Test case $0"
req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t string -r -v "test" 2>&1)" \
"[ERROR] bashopts_declare: -r and -v options cannot be activated at the same time"
"[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_case_29() {
bashopts_setup -n "$0" -d "Test case $0"
req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t number \
-v "invalid default value" 2>&1 >/dev/null | grep '^\[WARN')" \
"[WARN] Invalid default value for value option, this value will stay unset"
}
_test() {
......
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