aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKate Ward <kate.ward@forestent.com>2020-03-29 17:10:24 +0200
committerKate Ward <kate.ward@forestent.com>2020-03-29 17:10:24 +0200
commitbefbecbb9ee52e0c5c056974e87577f81815a690 (patch)
treee0c17f0e87a3444205445648d77af7fd8e9c33f6
parenta056c12df0f13174ace84dd71edf76d4074b20c3 (diff)
parent8c168e58cadd5bf92ed5bfc02c159b9251cf3eea (diff)
downloadshflags-befbecbb9ee52e0c5c056974e87577f81815a690.tar.gz
Merged from GitHub.
-rw-r--r--CONTRIBUTING.md62
-rw-r--r--README.md8
-rw-r--r--doc/CHANGES-1.0.md42
-rw-r--r--doc/CHANGES-1.2.md51
-rw-r--r--doc/RELEASE_NOTES-1.2.2.md36
-rw-r--r--doc/RELEASE_NOTES-1.2.3.md67
-rwxr-xr-xexamples/debug_output.sh16
-rwxr-xr-xexamples/hello_world.sh8
-rwxr-xr-xexamples/write_date.sh47
-rwxr-xr-x[-rw-r--r--]lib/shunit20
-rw-r--r--shflags107
-rwxr-xr-xshflags_issue_28_test.sh4
-rwxr-xr-xshflags_private_test.sh5
-rwxr-xr-xshflags_public_test.sh12
14 files changed, 269 insertions, 196 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index b071876..9915b70 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,5 +1,4 @@
-Coding Standards
-================
+# Coding Standards
shFlags is more than just a simple 20 line shell script. It is a pretty
significant library of shell code that at first glance is not that easy to
@@ -7,8 +6,7 @@ understand. To improve code readability and usability, some guidelines have been
set down to make the code more understandable for anyone who wants to read or
modify it.
-Function declaration
---------------------
+## Function declaration
Declare functions using the following form:
@@ -24,31 +22,30 @@ One-line functions are allowed if they can fit within the 80 char line limit.
doSomething() { echo 'done!'; }
```
-Function documentation
-----------------------
+## Function documentation
Each function should be preceded by a header that provides the following:
-1. A one-sentence summary of what the function does.
+1. A one-sentence summary of what the function does.
-1. (optional) A longer description of what the function does, and perhaps some
- special information that helps convey its usage better.
+1. (optional) A longer description of what the function does, and perhaps some
+ special information that helps convey its usage better.
-1. Args: a one-line summary of each argument of the form:
+1. Args: a one-line summary of each argument of the form:
- `name: type: description`
+ `name: type: description`
-1. Output: a one-line summary of the output provided. Only output to STDOUT
- must be documented, unless the output to STDERR is of significance (i.e. not
- just an error message). The output should be of the form:
+1. Output: a one-line summary of the output provided. Only output to STDOUT
+ must be documented, unless the output to STDERR is of significance (i.e. not
+ just an error message). The output should be of the form:
- `type: description`
+ `type: description`
-1. Returns: a one-line summary of the value returned. Returns in shell are
- always integers, but if the output is a true/false for success (i.e. a
- boolean), it should be noted. The output should be of the form:
+1. Returns: a one-line summary of the value returned. Returns in shell are
+ always integers, but if the output is a true/false for success (i.e. a
+ boolean), it should be noted. The output should be of the form:
- `type: description`
+ `type: description`
Here is a sample header:
@@ -66,8 +63,7 @@ Here is a sample header:
# boolean: success of operation (always returns True)
```
-Variable and function names
----------------------------
+## Variable and function names
All shFlags specific constants, variables, and functions will be prefixed
appropriately with 'flags'. This is to distinguish usage in the shFlags code
@@ -82,39 +78,37 @@ naming and use of variables, both public and private, is very important.
Accidental overriding of the variables can occur easily if care is not taken as
all variables are technically global variables in some shells.
-Type | Sample
----- | ------
+Type | Sample
+-------------------------------- | ---------------------
global public constant | `FLAGS_TRUE`
global private constant | `__FLAGS_SHELL_FLAGS`
global public variable | `flags_variable`
global private variable | `__flags_variable`
global macro | `_FLAGS_SOME_MACRO_`
public function | `flags_function`
-public function, local variable | ``flags_variable_`
+public function, local variable | `flags_variable_`
private function | `_flags_function`
private function, local variable | `_flags_variable_`
-Where it makes sense to improve readability, variables can have the first
-letter of the second and later words capitalized. For example, the local
-variable name for the help string length is `flags_helpStrLen_`.
+Where it makes sense to improve readability, variables can have the first letter
+of the second and later words capitalized. For example, the local variable name
+for the help string length is `flags_helpStrLen_`.
There are three special-case global public variables used. They are used due to
overcome the limitations of shell scoping or to prevent forking. The three
variables are:
-- `flags_error`
-- `flags_output`
-- `flags_return`
+- `flags_error`
+- `flags_output`
+- `flags_return`
-Local variable cleanup
-----------------------
+## Local variable cleanup
As many shells do not support local variables, no support for cleanup of
variables is present either. As such, all variables local to a function must be
cleared up with the `unset` built-in command at the end of each function.
-Indentation
------------
+## Indentation
Code block indentation is two (2) spaces, and tabs may not be used.
diff --git a/README.md b/README.md
index f33cf90..a6fac74 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,4 @@
-shFlags README
-==============
+# shFlags README
shFlags is a port of the Google [gflags](http://gflags.github.io/gflags/)
library for Unix shell. The code is written in a way to be as portable as
@@ -9,4 +8,7 @@ possible to work across a wide array of Unix variants. It is also tested with
If you'd like to use shFlags, feel free to read the documentation.
https://github.com/kward/shflags/wiki/Documentation12x
-[![Travis CI](https://travis-ci.org/kward/shflags.png?branch=master)](https://travis-ci.org/kward/shflags)
+[![Travis CI][travis-ci-png]][travis-ci]
+
+[travis-ci-png]: https://travis-ci.org/kward/shflags.png?branch=master
+[travis-ci]: https://travis-ci.org/kward/shflags
diff --git a/doc/CHANGES-1.0.md b/doc/CHANGES-1.0.md
index 102ddac..9437599 100644
--- a/doc/CHANGES-1.0.md
+++ b/doc/CHANGES-1.0.md
@@ -1,23 +1,21 @@
-Changes in shFlags
-==================
+# Changes in shFlags
-Changes with 1.0.3
-------------------
+## Changes with 1.0.3
-MAJOR CHANGE! `FLAGS_ARGC` is now obsolete, and is replaced by
-`FLAGS_ARGV`. See below for more info.
+MAJOR CHANGE! `FLAGS_ARGC` is now obsolete, and is replaced by `FLAGS_ARGV`. See
+below for more info.
Fixed issue# 7 where long flags defined with '=' (e.g. `--abc=123`) made it
impossible for the user to know how many non-flag command-line arguments were
-available because the value returned by `FLAGS_ARGC` was wrong. The
-`FLAGS_ARGC` value is now obsolete, but will be maintained for backwards
-compatibility. The new method of getting the non-flag arguments is by executing
-`eval set -- "${FLAGS_ARGV}"` after the `FLAGS` call. The arguments will
-then be available using the standard shell $#, $@, $*, $1, etc. variables.
+available because the value returned by `FLAGS_ARGC` was wrong. The `FLAGS_ARGC`
+value is now obsolete, but will be maintained for backwards compatibility. The
+new method of getting the non-flag arguments is by executing `eval set --
+"${FLAGS_ARGV}"` after the `FLAGS` call. The arguments will then be available
+using the standard shell $#, $@, $*, $1, etc. variables.
-Due to above fix for issue# 7, there is now proper support for mixing flags
-with non-flag arguments on the command-line. Previously, all non-flag arguments
-had to be at the end of the command-line.
+Due to above fix for issue# 7, there is now proper support for mixing flags with
+non-flag arguments on the command-line. Previously, all non-flag arguments had
+to be at the end of the command-line.
Renamed `_flags_standardGetopt()` and `_flags_enhancedGetopt()` functions to
`_flags_getoptStandard()` and `_flags_getoptEnhanced()`.
@@ -42,9 +40,7 @@ Added the OS version to OS release for Solaris.
Fixed `flags_reset()` so it unsets the default value environment vars.
-
-Changes with 1.0.2
-------------------
+## Changes with 1.0.2
FLAGS_PARENT no longer transforms into a constant so that it can be defined at
run time in scripts.
@@ -64,8 +60,8 @@ Improved `assert[Warn|Error]Msg()` test helper grepping.
Replaced shell_versions.sh with a new versions library and created
`gen_test_results.sh` to make releases easier.
-Copied the coding standards from shUnit2, but haven't fully implemented them
-in shFlags yet.
+Copied the coding standards from shUnit2, but haven't fully implemented them in
+shFlags yet.
Issue# 1: When a user defines their own `--help` flag, no more warning is thrown
when `FLAGS()` is called stating that the help flag already defined.
@@ -74,9 +70,7 @@ Issue# 2: Passing the `--nohelp` option no longer gives help output.
Issue# 3: Added support for screen width detection.
-
-Changes with 1.0.1
-------------------
+## Changes with 1.0.1
Fixed bug where the help output added '[no]' to all flag names
@@ -93,8 +87,6 @@ execute the code in-line, but later. As such, variables that are defined in the
library cannot be used until functions are called from the main code. This
required the 'help' flag definition to be moved inside the FLAGS command.
-
-Changes with 1.0.0
-------------------
+## Changes with 1.0.0
This is the first official release, so everything is new.
diff --git a/doc/CHANGES-1.2.md b/doc/CHANGES-1.2.md
index ea527ed..325a3c5 100644
--- a/doc/CHANGES-1.2.md
+++ b/doc/CHANGES-1.2.md
@@ -2,17 +2,21 @@
## Changes with 1.2.3pre
+Upgraded shUnit2 to 2.1.7.
+
+Fixed the examples to work again with the new code structure.
+
Removed `gen_test_report.sh` as it isn't used anymore.
Minor fix for `_flags_underscoreName()` to insure POSIX compliance.
Cleanup of pre-GitHub cruft.
-Fixed bug in `_flags_columns()` where `stty size` sometimes gave unexpected output, causing the function to not work.
+Fixed bug in `_flags_columns()` where `stty size` sometimes gave unexpected
+output, causing the function to not work.
Replaced `test_runner` with upstream from https://github.com/kward/shlib.
-
## Changes with 1.2.2
Ran all scripts through [ShellCheck](http://www.shellcheck.net/).
@@ -22,12 +26,11 @@ https://github.com/kward/shlib.
Fixed issue #45. Empty help string causes `shflags_test_issue_28.sh` to fail.
-Continuous integration testing setup with
-[Travis CI](https://travis-ci.org/kward/shflags).
+Continuous integration testing setup with [Travis
+CI](https://travis-ci.org/kward/shflags).
Restructured code to be more GitHub like.
-
## Changes with 1.2.1
Fixed issue #43. Added support for BusyBox `ash` shell.
@@ -40,11 +43,10 @@ Fixed issue #28. DEFINE_boolean misbehaves when help-string is empty.
Fixed issue #25. Fix some typos.
-
## Changes with 1.2.0
-Changed from the LGPL v2.1 license to the Apache v2.0 license so that others
-can include the library or make changes without needing to release the modified
+Changed from the LGPL v2.1 license to the Apache v2.0 license so that others can
+include the library or make changes without needing to release the modified
source code as well.
Moved documentation to Markdown.
@@ -74,8 +76,7 @@ Updated `gen_test_results.sh` and versions from shUnit2 source.
Fixed issues# 13, 14. Added support for dashes '-' in long flag names. The
defined flag will still be declared with underscores '\_' due to shell
limitations, so only one of a dashed flag name or an underscored flag name are
-allowed, not both.
-(Backslash on \_ to prevent Markdown formatting.)
+allowed, not both. (Backslash on \_ to prevent Markdown formatting.)
Issue #20. Updated LGPL v2.1 license from
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
@@ -95,24 +96,23 @@ built-ins where possible to increase performance and reduce the usage of the
Added separate built-in and `expr` functions for doing math.
-
## Changes with 1.0.3
-MAJOR CHANGE! `FLAGS_ARGC` is now obsolete, and is replaced by
-`FLAGS_ARGV`. See below for more info.
+MAJOR CHANGE! `FLAGS_ARGC` is now obsolete, and is replaced by `FLAGS_ARGV`. See
+below for more info.
Fixed issue# 7 where long flags defined with '=' (e.g. `--abc=123`) made it
impossible for the user to know how many non-flag command-line arguments were
-available because the value returned by `FLAGS_ARGC` was wrong. The
-`FLAGS_ARGC` value is now obsolete, but will be maintained for backwards
-compatibility. The new method of getting the non-flag arguments is by executing
-`eval set -- "${FLAGS_ARGV}"` after the `FLAGS` call. The arguments will
-then be available using the standard shell $#, $@, $\*, $1, etc. variables.
-(Backslash on \* to prevent Markdown formatting.)
+available because the value returned by `FLAGS_ARGC` was wrong. The `FLAGS_ARGC`
+value is now obsolete, but will be maintained for backwards compatibility. The
+new method of getting the non-flag arguments is by executing `eval set --
+"${FLAGS_ARGV}"` after the `FLAGS` call. The arguments will then be available
+using the standard shell $#, $@, $\*, $1, etc. variables. (Backslash on \* to
+prevent Markdown formatting.)
-Due to above fix for issue# 7, there is now proper support for mixing flags
-with non-flag arguments on the command-line. Previously, all non-flag arguments
-had to be at the end of the command-line.
+Due to above fix for issue# 7, there is now proper support for mixing flags with
+non-flag arguments on the command-line. Previously, all non-flag arguments had
+to be at the end of the command-line.
Renamed `_flags_standardGetopt()` and `_flags_enhancedGetopt()` functions to
`_flags_getoptStandard()` and `_flags_getoptEnhanced()`.
@@ -137,7 +137,6 @@ Added the OS version to OS release for Solaris.
Fixed `flags_reset()` so it unsets the default value environment vars.
-
## Changes with 1.0.2
${FLAGS_PARENT} no longer transforms into a constant so that it can be defined
@@ -158,8 +157,8 @@ Improved `assert[Warn|Error]Msg()` test helper grepping.
Replaced shell_versions.sh with a new versions library and created
`gen_test_results.sh` to make releases easier.
-Copied the coding standards from shUnit2, but haven't fully implemented them
-in shFlags yet.
+Copied the coding standards from shUnit2, but haven't fully implemented them in
+shFlags yet.
Issue# 1: When a user defines their own `--help` flag, no more warning is thrown
when `FLAGS()` is called stating that the help flag already defined.
@@ -168,7 +167,6 @@ Issue# 2: Passing the `--nohelp` option no longer gives help output.
Issue# 3: Added support for screen width detection.
-
## Changes with 1.0.1
Fixed bug where the help output added '[no]' to all flag names
@@ -186,7 +184,6 @@ execute the code in-line, but later. As such, variables that are defined in the
library cannot be used until functions are called from the main code. This
required the 'help' flag definition to be moved inside the FLAGS command.
-
## Changes with 1.0.0
This is the first official release, so everything is new.
diff --git a/doc/RELEASE_NOTES-1.2.2.md b/doc/RELEASE_NOTES-1.2.2.md
index c919314..7e09fda 100644
--- a/doc/RELEASE_NOTES-1.2.2.md
+++ b/doc/RELEASE_NOTES-1.2.2.md
@@ -7,7 +7,6 @@ https://github.com/kward/shflags
This document covers any known issues and workarounds for the stated release of
shFlags.
-
## Release info
This is a minor bug fix release.
@@ -16,9 +15,10 @@ Please see the `CHANGES-1.2.md` file for a complete list of changes.
### Notable changes
-Continuous integration testing setup with
-[Travis CI](https://travis-ci.org/kward/shflags). This helps keep the code in
-a working state by executing the unit tests on every submit.
+Continuous integration testing setup with [Travis CI][travis-ci]. This helps
+keep the code in a working state by executing the unit tests on every submit.
+
+[travis-ci]: https://travis-ci.org/kward/shflags
All code run through [ShellCheck](http://www.shellcheck.net/). This is an
excellent shell linter, and brings consistency to the coding patterns used.
@@ -30,7 +30,6 @@ more appealing to a wider audience.
Fixed issue #45. Empty help string causes `shflags_test_issue_28.sh` to fail.
-
## General info
### The unit tests
@@ -42,24 +41,23 @@ the tester that the supported functionality is not present, but an additional
test is present to verify that shFlags properly caught the limitation and
presented the user with an appropriate error message.
-shFlags tries to support both the standard and enhanced versions of `getopt`.
-As each responds differently, and not everything is supported on the standard
+shFlags tries to support both the standard and enhanced versions of `getopt`. As
+each responds differently, and not everything is supported on the standard
version, some unit tests will be skipped (i.e. ASSERTS will not be thrown) when
-the standard version of `getopt` is detected. The reason being that there is
-no point testing for functionality that is positively known not to exist. A
-tally of skipped tests will be kept for later reference.
+the standard version of `getopt` is detected. The reason being that there is no
+point testing for functionality that is positively known not to exist. A tally
+of skipped tests will be kept for later reference.
### Standard vs Enhanced getopt
Here is a matrix of the supported features of the various `getopt` variants.
-| Feature | std | enh |
-|-----------------------------------------|-----|-----|
-| short option names | Y | Y |
-| long option names | N | Y |
-| spaces in string options | N | Y |
-| intermixing of flag and non-flag values | N | Y |
-
+Feature | std | enh
+--------------------------------------- | --- | ---
+short option names | Y | Y
+long option names | N | Y
+spaces in string options | N | Y
+intermixing of flag and non-flag values | N | Y
## Known Issues
@@ -70,5 +68,5 @@ OpenSolaris) is the standard version.
## Workarounds
The Zsh shell requires the `shwordsplit` option to be set and the special
-`FLAGS_PARENT` variable must be defined. See `src/shflags_test_helpers` to
-see how the unit tests do this.
+`FLAGS_PARENT` variable must be defined. See `src/shflags_test_helpers` to see
+how the unit tests do this.
diff --git a/doc/RELEASE_NOTES-1.2.3.md b/doc/RELEASE_NOTES-1.2.3.md
new file mode 100644
index 0000000..2764721
--- /dev/null
+++ b/doc/RELEASE_NOTES-1.2.3.md
@@ -0,0 +1,67 @@
+# shFlags 1.2.3 Release Notes
+
+https://github.com/kward/shflags
+
+## Preface
+
+This document covers any known issues and workarounds for the stated release of
+shFlags.
+
+## Release info
+
+This is a minor bug fix and cleanup release.
+
+There were several minor changes thanks to feedback from a major code review by
+[James Youngman](https://www.linkedin.com/in/jamesyoungman/). Thank you!
+
+Please see the `CHANGES-1.2.md` file for a complete list of changes.
+
+### Notable changes
+
+None.
+
+### Notable bug fixes
+
+Fixed the examples to work again with the new code structure. These broke when
+the code was refactored for GitHub.
+
+## General info
+
+### The unit tests
+
+shFlags is designed to work on as many environments as possible, but not all
+environments are created equal. As such, not all of the unit tests will succeed
+on every platform. The unit tests are therefore designed to fail, indicating to
+the tester that the supported functionality is not present, but an additional
+test is present to verify that shFlags properly caught the limitation and
+presented the user with an appropriate error message.
+
+shFlags tries to support both the standard and enhanced versions of `getopt`. As
+each responds differently, and not everything is supported on the standard
+version, some unit tests will be skipped (i.e. ASSERTS will not be thrown) when
+the standard version of `getopt` is detected. The reason being that there is no
+point testing for functionality that is positively known not to exist. A tally
+of skipped tests will be kept for later reference.
+
+### Standard vs Enhanced getopt
+
+Here is a matrix of the supported features of the various `getopt` variants.
+
+Feature | std | enh
+--------------------------------------- | --- | ---
+short option names | Y | Y
+long option names | N | Y
+spaces in string options | N | Y
+intermixing of flag and non-flag values | N | Y
+
+## Known Issues
+
+The `getopt` version provided by default with all versions of Mac OS X (up to
+and including 10.13.0) and Solaris (up to and including Solaris 10 and
+OpenSolaris) is the standard version.
+
+## Workarounds
+
+The Zsh shell requires the `shwordsplit` option to be set and the special
+`FLAGS_PARENT` variable must be defined. See `src/shflags_test_helpers` to see
+how the unit tests do this.
diff --git a/examples/debug_output.sh b/examples/debug_output.sh
index b3ecec2..a5e61c8 100755
--- a/examples/debug_output.sh
+++ b/examples/debug_output.sh
@@ -11,10 +11,10 @@
# $ ./debug_output.sh sing
# $ ./debug_output.sh --debug sing
-# source shflags
-. ../src/shflags
+# Source shflags.
+. ../shflags
-# define flags
+# Define flags.
DEFINE_boolean 'debug' false 'enable debug mode' 'd'
FLAGS_HELP=`cat <<EOF
commands:
@@ -22,21 +22,17 @@ commands:
sing: sing something
EOF`
-
-debug()
-{
+debug() {
[ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return
echo "DEBUG: $@" >&2
}
-die() {
- [ $# -gt 0 ] && echo "error: $@" >&2
+die() { [ $# -gt 0 ] && echo "error: $@" >&2
flags_help
exit 1
}
-
-# parse the command-line
+# Parse the command-line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
diff --git a/examples/hello_world.sh b/examples/hello_world.sh
index 84b755a..e5fe81c 100755
--- a/examples/hello_world.sh
+++ b/examples/hello_world.sh
@@ -7,13 +7,13 @@
# '--name'). If a name is given, it is output, otherwise the default of 'world'
# is output.
-# source shflags
-. ../src/shflags
+# Source shflags.
+. ../shflags
-# define a 'name' command-line string flag
+# Define a 'name' command-line string flag.
DEFINE_string 'name' 'world' 'name to say hello to' 'n'
-# parse the command-line
+# Parse the command-line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
diff --git a/examples/write_date.sh b/examples/write_date.sh
index b8e610b..d9ae3ec 100755
--- a/examples/write_date.sh
+++ b/examples/write_date.sh
@@ -20,35 +20,44 @@
# $ ./write_date.sh -f now.out
# $ cat now.out
-# source shflags
-. ../src/shflags
+# Source shFlags.
+. ../shflags
-# configure shflags
+# Configure shFlags.
DEFINE_boolean 'force' false 'force overwriting' 'f'
FLAGS_HELP="USAGE: $0 [flags] filename"
-
-write_date()
-{
- date >"$1"
-}
-
-die()
-{
- [ $# -gt 0 ] && echo "error: $@" >&2
+die() {
+ [ $# -gt 0 ] && echo "error: $@"
flags_help
exit 1
}
-
-# parse the command-line
+# Parse the command-line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
-# check for filename
-[ $# -gt 0 ] || die 'filename missing'
+# Check for filename on command-line.
+[ $# -gt 0 ] || die 'filename missing.'
filename=$1
-[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \
- && die 'filename exists; not overwriting'
-write_date "${filename}"
+# Redirect STDOUT to the file ($1). This seemingly complicated method using exec
+# is used so that a potential race condition between checking for the presence
+# of the file and writing to the file is mitigated.
+if [ ${FLAGS_force} -eq ${FLAGS_FALSE} ] ; then
+ [ ! -f "${filename}" ] || die "file \"${filename}\" already exists."
+ # Set noclobber, redirect STDOUT to the file, first saving STDOUT to fd 4.
+ set -C
+ exec 4>&1 >"${filename}" # This fails if the file exists.
+else
+ # Forcefully overwrite (clobber) the file.
+ exec 4>&1 >|"${filename}"
+fi
+
+# What time is it?
+date
+
+# Restore STDOUT from file descriptor 4, and close fd 4.
+exec 1>&4 4>&-
+
+echo "The current date was written to \"${filename}\"."
diff --git a/lib/shunit2 b/lib/shunit2
index a093463..a093463 100644..100755
--- a/lib/shunit2
+++ b/lib/shunit2
diff --git a/shflags b/shflags
index 7232940..62a8e22 100644
--- a/shflags
+++ b/shflags
@@ -92,7 +92,7 @@
# Return if FLAGS already loaded.
[ -n "${FLAGS_VERSION:-}" ] && return 0
-FLAGS_VERSION='1.2.3pre'
+FLAGS_VERSION='1.2.3'
# Return values that scripts can use.
FLAGS_TRUE=0
@@ -125,7 +125,57 @@ fi
FLAGS_EXPR_CMD=${FLAGS_EXPR_CMD:-${__FLAGS_EXPR_CMD}}
FLAGS_GETOPT_CMD=${FLAGS_GETOPT_CMD:-getopt}
-# Specific shell checks.
+#
+# Logging functions.
+#
+
+__flags_level=0 # Default logging level.
+
+_flags_debug() {
+ [ ${__flags_level} -le ${FLAGS_LEVEL_DEBUG} ] || return
+ echo "flags:DEBUG $*" >&2
+}
+_flags_info() {
+ [ ${__flags_level} -le ${FLAGS_LEVEL_INFO} ] || return
+ echo "flags:INFO $*" >&2
+}
+_flags_warn() {
+ [ ${__flags_level} -le ${FLAGS_LEVEL_WARN} ] || return
+ echo "flags:WARN $*" >&2
+}
+_flags_error() {
+ [ ${__flags_level} -le ${FLAGS_LEVEL_ERROR} ] || return
+ echo "flags:ERROR $*" >&2
+}
+_flags_fatal() {
+ [ ${__flags_level} -le ${FLAGS_LEVEL_FATAL} ] || return
+ echo "flags:FATAL $*" >&2
+ exit ${FLAGS_ERROR}
+}
+
+# Get the logging level.
+flags_loggingLevel() { echo ${__flags_level}; }
+
+# Set the logging level.
+#
+# Args:
+# _flags_level_: integer: new logging level
+# Returns:
+# nothing
+flags_setLoggingLevel() {
+ [ $# -ne 1 ] && _flags_fatal "flags_setLevel(): logging level missing"
+ _flags_level_=$1
+ [ "${_flags_level_}" -ge "${FLAGS_LEVEL_DEBUG}" \
+ -a "${_flags_level_}" -le "${FLAGS_LEVEL_FATAL}" ] \
+ || _flags_fatal "Invalid logging level '${_flags_level_}' specified."
+ __flags_level=$1
+ unset _flags_level_
+}
+
+#
+# Shell checks.
+#
+
if [ -n "${ZSH_VERSION:-}" ]; then
setopt |grep "^shwordsplit$" >/dev/null
if [ $? -ne ${FLAGS_TRUE} ]; then
@@ -145,7 +195,6 @@ else
__FLAGS_USE_BUILTIN=${FLAGS_FALSE}
fi
-
#
# Constants.
#
@@ -210,7 +259,10 @@ for __flags_const in ${__flags_constants}; do
fi
case ${ZSH_VERSION} in
[123].*) readonly "${__flags_const}" ;;
- *) readonly -g "${__flags_const}" ;; # Declare readonly constants globally.
+ *)
+ # Declare readonly constants globally.
+ # shellcheck disable=SC2039
+ readonly -g "${__flags_const}" ;;
esac
done
unset __flags_const __flags_constants
@@ -226,55 +278,8 @@ __flags_shortNames=' ' # Short flag names.
__flags_definedNames=' ' # Defined flag names (used for validation).
__flags_columns='' # Screen width in columns.
-__flags_level=0 # Default logging level.
__flags_opts='' # Temporary storage for parsed getopt flags.
-#------------------------------------------------------------------------------
-# Private functions.
-#
-
-# Logging functions.
-_flags_debug() {
- [ ${__flags_level} -le ${FLAGS_LEVEL_DEBUG} ] || return
- echo "flags:DEBUG $*" >&2
-}
-_flags_info() {
- [ ${__flags_level} -le ${FLAGS_LEVEL_INFO} ] || return
- echo "flags:INFO $*" >&2
-}
-_flags_warn() {
- [ ${__flags_level} -le ${FLAGS_LEVEL_WARN} ] || return
- echo "flags:WARN $*" >&2
-}
-_flags_error() {
- [ ${__flags_level} -le ${FLAGS_LEVEL_ERROR} ] || return
- echo "flags:ERROR $*" >&2
-}
-_flags_fatal() {
- [ ${__flags_level} -le ${FLAGS_LEVEL_FATAL} ] || return
- echo "flags:FATAL $*" >&2
- exit ${FLAGS_ERROR}
-}
-
-# Get the logging level.
-flags_loggingLevel() { echo ${__flags_level}; }
-
-# Set the logging level.
-#
-# Args:
-# _flags_level_: integer: new logging level
-# Returns:
-# nothing
-flags_setLoggingLevel() {
- [ $# -ne 1 ] && _flags_fatal "flags_setLevel(): logging level missing"
- _flags_level_=$1
- [ "${_flags_level_}" -ge "${FLAGS_LEVEL_DEBUG}" \
- -a "${_flags_level_}" -le "${FLAGS_LEVEL_FATAL}" ] \
- || _flags_fatal "Invalid logging level '${_flags_level_}' specified."
- __flags_level=$1
- unset _flags_level_
-}
-
# Define a flag.
#
# Calling this function will define the following info variables for the
diff --git a/shflags_issue_28_test.sh b/shflags_issue_28_test.sh
index fbb17d9..69043eb 100755
--- a/shflags_issue_28_test.sh
+++ b/shflags_issue_28_test.sh
@@ -57,8 +57,9 @@ _testHelp() {
assertTrue 'setting FLAGS_HELP did not produce expected result' ${grepped}
[ ${grepped} -ne "${FLAGS_TRUE}" ] && th_showOutput
- # test that "'" chars work in help string
+ # Test that "'" chars work in help string.
(
+ # shellcheck disable=SC2034
DEFINE_boolean b false "help string containing a ' char" b
FLAGS "${flag}" >"${stdoutF}" 2>"${stderrF}"
)
@@ -73,6 +74,7 @@ mock_flags_columns() {
}
_doDefines() {
+ # shellcheck disable=SC2034
DEFINE_boolean 'force' false '' f
}
diff --git a/shflags_private_test.sh b/shflags_private_test.sh
index b80c177..6490a20 100755
--- a/shflags_private_test.sh
+++ b/shflags_private_test.sh
@@ -49,18 +49,23 @@ mock_getopt_enh() { echo ' --foo --'; return 0; }
testGenOptStr() {
_testGenOptStr '' ''
+ # shellcheck disable=SC2034
DEFINE_boolean bool false 'boolean value' b
_testGenOptStr 'b' 'bool'
+ # shellcheck disable=SC2034
DEFINE_float float 0.0 'float value' f
_testGenOptStr 'bf:' 'bool,float:'
+ # shellcheck disable=SC2034
DEFINE_integer int 0 'integer value' i
_testGenOptStr 'bf:i:' 'bool,float:,int:'
+ # shellcheck disable=SC2034
DEFINE_string str 0 'string value' s
_testGenOptStr 'bf:i:s:' 'bool,float:,int:,str:'
+ # shellcheck disable=SC2034
DEFINE_boolean help false 'show help' h
_testGenOptStr 'bf:i:s:h' 'bool,float:,int:,str:,help'
}
diff --git a/shflags_public_test.sh b/shflags_public_test.sh
index 72db11f..3d4ac90 100755
--- a/shflags_public_test.sh
+++ b/shflags_public_test.sh
@@ -59,8 +59,9 @@ _testHelp() {
assertTrue 'setting FLAGS_HELP did not produce expected result' ${grepped}
[ ${grepped} -ne "${FLAGS_TRUE}" ] && th_showOutput
- # test that "'" chars work in help string
+ # Test that "'" chars work in help string.
(
+ # shellcheck disable=SC2034
DEFINE_boolean b false "help string containing a ' char" b
FLAGS "${flag}" >"${stdoutF}" 2>"${stderrF}"
)
@@ -70,8 +71,7 @@ _testHelp() {
[ ${grepped} -ne "${FLAGS_TRUE}" ] && th_showOutput
}
-mock_flags_columns()
-{
+mock_flags_columns() {
echo 80
}
@@ -117,13 +117,19 @@ EOF
}
testEnhancedHelpOutput() {
+
flags_getoptIsEnh || startSkipping
+ # shellcheck disable=SC2034
DEFINE_boolean test_bool false 'test boolean' b
+ # shellcheck disable=SC2034
DEFINE_integer test_int 0 'test integer' i
+ # shellcheck disable=SC2034
DEFINE_string test_str '' 'test string' s
+ # shellcheck disable=SC2034
DEFINE_string long_desc 'blah' \
'testing of a long description to force wrap of default value' D
+ # shellcheck disable=SC2034
DEFINE_string long_default \
'this_is_a_long_default_value_to_force_alternate_indentation' \
'testing of long default value' F