Browse Source

Updated fbopt to version 0.2.

dev
Franco Masotti 4 years ago
parent
commit
dcb7dfec7a
  1. 43
      _posts/2018-11-18-my-bash-option-parsing-template.md

43
_posts/2018-11-18-my-bash-option-parsing-template.md

@ -52,23 +52,30 @@ These elements should be common sense and not be specific to `fbopt`
## Dependencies and compliancy
### fbopt version 0.1
The template is known to work with the following packages
| Package | Version | Version command |
|---------|---------|-----------------|
| [GNU Bash](http://www.gnu.org/software/bash/bash.html) | `GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu)` | `$ bash --version` |
| [getopt](https://www.kernel.org/pub/linux/utils/util-linux/) | `getopt from util-linux 2.33` | `$ getopt -V` |
| Package | Version command |
|---------|-----------------|
| [GNU Bash](http://www.gnu.org/software/bash/bash.html) | `$ bash --version` |
| [getopt](https://www.kernel.org/pub/linux/utils/util-linux/) | `$ getopt --version` |
`fbopt` compatibility:
| Package | Package version | fbopt version |
|---------|-----------------|---------------|
| GNU Bash | `GNU bash, version 4.4.23(1)-release (x86_64-unknown-linux-gnu)` | `0.1`, `0.2` |
| getopt | `getopt from util-linux 2.33` | `0.1`, `0.2` |
## The template
To allow inclusion in any project the template is released under the [CC0 1.0 license](https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt).
What follows is `fbopt` version `0.2`.
```shell
#!/bin/bash
#
# fbopt version 0.1
# fbopt version 0.2
#
# Written in 2018 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
#
@ -99,7 +106,8 @@ Options:
-f enable and or set f
with an optional argument
-h, --help print this help
--print-flags print the enabled options
--print-flags print the enabled options. This can also
be used to print the default options
Exit status:
0 if OK,
@ -138,7 +146,7 @@ main()
# Set the options. flag_f has an optional argument.
local getopt_short_options='a:b:c:def::hi'
local getopt_long_options="flag-a:,flag-b:,flag-e,print-flags,help,"
local getopt_long_options='flag-a:,flag-b:,flag-e,print-flags,help,'
# Set the default values for the flags.
local flag_a=''
@ -147,7 +155,6 @@ main()
local flag_d='false'
local flag_e='false'
local flag_f='no'
local print_flags='false'
local program_name="${0}"
opts="$(getopt \
@ -197,6 +204,14 @@ main()
# Everything else after '--' is an argument.
argc="${*}"
# Check that the flags that must be non empty are actually not empty.
# A user might infact circumvent getopt's mechanisms like this
# ./program -flag ''
# This can also be done inside the option parser loop but to avoid nestings
# I prefer it done here.
{ [ -z "${flag_a}" ] \
|| [ -z "${flag_b}" ]; } \
&& getopt_error && return 1
[ "${print_flags}" = 'true' ] \
&& show_flags \
'flag_a' \
@ -207,14 +222,6 @@ main()
'flag_f' \
&& return 0
[ "${help}" = 'true' ] && show_help && return 0
# Check that the flags that must be non empty are actually not empty.
# A user might infact circumvent getopt's mechanisms like this
# ./program -flag ''
# This can also be done inside the option parser loop but to avoid nestings
# I prefer it done here.
{ [ -z "${flag_a}" ] \
|| [ -z "${flag_b}" ]; } \
&& getopt_error && return 1
# From now on you should call a function or an external program
# using the values of the flag variables.

Loading…
Cancel
Save