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

Merge branch 'bashopts_process_opt' into 'master'

Bashopts process opt

See merge request !1
parents f8698895 dbfcaa37
Loading
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -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:"")
```
+234 −154

File changed.

Preview size limit exceeded, changes collapsed.

+3 −3
Original line number Diff line number Diff line
@@ -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
+110 −19

File changed.

Preview size limit exceeded, changes collapsed.