Commit cf8c2eb0 authored by Michael Völske's avatar Michael Völske

update shflags

parent 87b42461
# $Id$
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# Copyright 2008-2018 Kate Ward. All Rights Reserved.
# Released under the Apache License 2.0 license.
# http://www.apache.org/licenses/LICENSE-2.0
#
# shFlags -- Advanced command-line flag library for Unix shell scripts.
# http://code.google.com/p/shflags/
# shFlags is an advanced command-line flag library for Unix shell scripts.
#
# Author: kate.ward@forestent.com (Kate Ward)
# https://github.com/kward/shflags
#
# This module implements something like the google-gflags library available
# from http://code.google.com/p/google-gflags/.
# This module implements something like the gflags library available
# from https://github.com/gflags/gflags.
#
# FLAG TYPES: This is a list of the DEFINE_*'s that you can do. All flags take
# a name, default value, help-string, and optional 'short' name (one-letter
# name). Some flags have other arguments, which are described with the flag.
# name). Some flags have other arguments, which are described with the flag.
#
# DEFINE_string: takes any input, and intreprets it as a string.
# DEFINE_string: takes any input, and interprets it as a string.
#
# DEFINE_boolean: typically does not take any argument: say --myflag to set
# FLAGS_myflag to true, or --nomyflag to set FLAGS_myflag to false.
# Alternately, you can say
# --myflag=true or --myflag=t or --myflag=0 or
# --myflag=false or --myflag=f or --myflag=1
# Passing an option has the same affect as passing the option once.
# DEFINE_boolean: does not take any arguments. Say --myflag to set
# FLAGS_myflag to true, or --nomyflag to set FLAGS_myflag to false. For short
# flags, passing the flag on the command-line negates the default value, i.e.
# if the default is true, passing the flag sets the value to false.
#
# DEFINE_float: takes an input and intreprets it as a floating point number. As
# DEFINE_float: takes an input and interprets it as a floating point number. As
# shell does not support floats per-se, the input is merely validated as
# being a valid floating point value.
#
# DEFINE_integer: takes an input and intreprets it as an integer.
# DEFINE_integer: takes an input and interprets it as an integer.
#
# SPECIAL FLAGS: There are a few flags that have special meaning:
# --help (or -?) prints a list of all the flags in a human-readable fashion
......@@ -50,14 +48,22 @@
# $ ./hello.sh -n Kate
# Hello, Kate.
#
# NOTE: Not all systems include a getopt version that supports long flags. On
# these systems, only short flags are recognized.
# CUSTOMIZABLE BEHAVIOR:
#
# A script can override the default 'getopt' command by providing the path to
# an alternate implementation by defining the FLAGS_GETOPT_CMD variable.
#
# NOTES:
#
# * Not all systems include a getopt version that supports long flags. On these
# systems, only short flags are recognized.
#==============================================================================
# shFlags
#
# Shared attributes:
# flags_error: last error message
# flags_error: last error message
# flags_output: last function output (rarely valid)
# flags_return: last return value
#
# __flags_longNames: list of long names for all flags
......@@ -75,63 +81,145 @@
#
# Notes:
# - lists of strings are space separated, and a null value is the '~' char.
# return if FLAGS already loaded
#
### ShellCheck (http://www.shellcheck.net/)
# expr may be antiquated, but it is the only solution in some cases.
# shellcheck disable=SC2003
# $() are not fully portable (POSIX != portable).
# shellcheck disable=SC2006
# [ p -a q ] are well defined enough (vs [ p ] && [ q ]).
# shellcheck disable=SC2166
# Return if FLAGS already loaded.
[ -n "${FLAGS_VERSION:-}" ] && return 0
FLAGS_VERSION='1.0.3'
FLAGS_VERSION='1.2.3'
# return values
# Return values that scripts can use.
FLAGS_TRUE=0
FLAGS_FALSE=1
FLAGS_ERROR=2
# reserved flag names
FLAGS_RESERVED='ARGC ARGV ERROR FALSE HELP PARENT RESERVED TRUE VERSION'
# Logging levels.
FLAGS_LEVEL_DEBUG=0
FLAGS_LEVEL_INFO=1
FLAGS_LEVEL_WARN=2
FLAGS_LEVEL_ERROR=3
FLAGS_LEVEL_FATAL=4
__FLAGS_LEVEL_DEFAULT=${FLAGS_LEVEL_WARN}
# Determine some reasonable command defaults.
__FLAGS_EXPR_CMD='expr --'
__FLAGS_UNAME_S=`uname -s`
if [ "${__FLAGS_UNAME_S}" = 'BSD' ]; then
__FLAGS_EXPR_CMD='gexpr --'
else
_flags_output_=`${__FLAGS_EXPR_CMD} 2>&1`
if [ $? -eq ${FLAGS_TRUE} -a "${_flags_output_}" = '--' ]; then
# We are likely running inside BusyBox.
__FLAGS_EXPR_CMD='expr'
fi
unset _flags_output_
fi
# Commands a user can override if desired.
FLAGS_EXPR_CMD=${FLAGS_EXPR_CMD:-${__FLAGS_EXPR_CMD}}
FLAGS_GETOPT_CMD=${FLAGS_GETOPT_CMD:-getopt}
#
# 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_
}
_flags_debug() { echo "flags:DEBUG $@" >&2; }
_flags_warn() { echo "flags:WARN $@" >&2; }
_flags_error() { echo "flags:ERROR $@" >&2; }
_flags_fatal() { echo "flags:FATAL $@" >&2; }
#
# Shell checks.
#
# specific shell checks
if [ -n "${ZSH_VERSION:-}" ]; then
setopt |grep "^shwordsplit$" >/dev/null
if [ $? -ne ${FLAGS_TRUE} ]; then
_flags_fatal 'zsh shwordsplit option is required for proper zsh operation'
exit ${FLAGS_ERROR}
fi
if [ -z "${FLAGS_PARENT:-}" ]; then
_flags_fatal "zsh does not pass \$0 through properly. please declare' \
\"FLAGS_PARENT=\$0\" before calling shFlags"
exit ${FLAGS_ERROR}
fi
fi
# Can we use built-ins?
( echo "${FLAGS_TRUE#0}"; ) >/dev/null 2>&1
if [ $? -eq ${FLAGS_TRUE} ]; then
__FLAGS_USE_BUILTIN=${FLAGS_TRUE}
else
__FLAGS_USE_BUILTIN=${FLAGS_FALSE}
fi
#
# constants
# Constants.
#
# getopt version
# Reserved flag names.
__FLAGS_RESERVED_LIST=' ARGC ARGV ERROR FALSE GETOPT_CMD HELP PARENT TRUE '
__FLAGS_RESERVED_LIST="${__FLAGS_RESERVED_LIST} VERSION "
# Determined getopt version (standard or enhanced).
__FLAGS_GETOPT_VERS_STD=0
__FLAGS_GETOPT_VERS_ENH=1
__FLAGS_GETOPT_VERS_BSD=2
getopt >/dev/null 2>&1
case $? in
0) __FLAGS_GETOPT_VERS=${__FLAGS_GETOPT_VERS_STD} ;; # bsd getopt
2)
# TODO(kward): look into '-T' option to test the internal getopt() version
if [ "`getopt --version`" = '-- ' ]; then
__FLAGS_GETOPT_VERS=${__FLAGS_GETOPT_VERS_STD}
else
__FLAGS_GETOPT_VERS=${__FLAGS_GETOPT_VERS_ENH}
fi
;;
*)
_flags_fatal 'unable to determine getopt version'
exit ${FLAGS_ERROR}
;;
esac
# shellcheck disable=SC2120
_flags_getopt_vers() {
_flags_getopt_cmd_=${1:-${FLAGS_GETOPT_CMD}}
case "`${_flags_getopt_cmd_} -lfoo '' --foo 2>&1`" in
' -- --foo') echo ${__FLAGS_GETOPT_VERS_STD} ;;
' --foo --') echo ${__FLAGS_GETOPT_VERS_ENH} ;;
# Unrecognized output. Assuming standard getopt version.
*) echo ${__FLAGS_GETOPT_VERS_STD} ;;
esac
unset _flags_getopt_cmd_
}
# shellcheck disable=SC2119
__FLAGS_GETOPT_VERS=`_flags_getopt_vers`
# getopt optstring lengths
__FLAGS_OPTSTR_SHORT=0
......@@ -139,57 +227,55 @@ __FLAGS_OPTSTR_LONG=1
__FLAGS_NULL='~'
# flag info strings
# Flag info strings.
__FLAGS_INFO_DEFAULT='default'
__FLAGS_INFO_HELP='help'
__FLAGS_INFO_SHORT='short'
__FLAGS_INFO_TYPE='type'
# flag lengths
# Flag lengths.
__FLAGS_LEN_SHORT=0
__FLAGS_LEN_LONG=1
# flag types
# Flag types.
__FLAGS_TYPE_NONE=0
__FLAGS_TYPE_BOOLEAN=1
__FLAGS_TYPE_FLOAT=2
__FLAGS_TYPE_INTEGER=3
__FLAGS_TYPE_STRING=4
# set the constants readonly
# Set the constants readonly.
__flags_constants=`set |awk -F= '/^FLAGS_/ || /^__FLAGS_/ {print $1}'`
for __flags_const in ${__flags_constants}; do
# skip certain flags
# Skip certain flags.
case ${__flags_const} in
FLAGS_HELP) continue ;;
FLAGS_PARENT) continue ;;
esac
# set flag readonly
# Set flag readonly.
if [ -z "${ZSH_VERSION:-}" ]; then
readonly ${__flags_const}
else # handle zsh
case ${ZSH_VERSION} in
[123].*) readonly ${__flags_const} ;;
*) readonly -g ${__flags_const} ;; # declare readonly constants globally
esac
readonly "${__flags_const}"
continue
fi
case ${ZSH_VERSION} in
[123].*) readonly "${__flags_const}" ;;
*) readonly -g "${__flags_const}" ;; # Declare readonly constants globally.
esac
done
unset __flags_const __flags_constants
#
# internal variables
# Internal variables.
#
__flags_boolNames=' ' # space separated list of boolean flag names
__flags_longNames=' ' # space separated list of long flag names
__flags_shortNames=' ' # space separated list of short flag names
# Space separated lists.
__flags_boolNames=' ' # Boolean flag names.
__flags_longNames=' ' # Long flag names.
__flags_shortNames=' ' # Short flag names.
__flags_definedNames=' ' # Defined flag names (used for validation).
__flags_columns='' # screen width in columns
__flags_opts='' # temporary storage for parsed getopt flags
#------------------------------------------------------------------------------
# private functions
#
__flags_columns='' # Screen width in columns.
__flags_opts='' # Temporary storage for parsed getopt flags.
# Define a flag.
#
......@@ -202,15 +288,14 @@ __flags_opts='' # temporary storage for parsed getopt flags
# __flags_flagname_type - the type of flag (one of __FLAGS_TYPE_*)
#
# Args:
# _flags__type: integer: internal type of flag (__FLAGS_TYPE_*)
# _flags__name: string: long flag name
# _flags__default: default flag value
# _flags__help: string: help string
# _flags__short: string: (optional) short flag name
# _flags_type_: integer: internal type of flag (__FLAGS_TYPE_*)
# _flags_name_: string: long flag name
# _flags_default_: default flag value
# _flags_help_: string: help string
# _flags_short_: string: (optional) short flag name
# Returns:
# integer: success of operation, or error
_flags_define()
{
_flags_define() {
if [ $# -lt 4 ]; then
flags_error='DEFINE error: too few arguments'
flags_return=${FLAGS_ERROR}
......@@ -221,58 +306,59 @@ _flags_define()
_flags_type_=$1
_flags_name_=$2
_flags_default_=$3
_flags_help_=$4
_flags_help_=${4:-§} # Special value '§' indicates no help string provided.
_flags_short_=${5:-${__FLAGS_NULL}}
_flags_return_=${FLAGS_TRUE}
_flags_debug "type:${_flags_type_} name:${_flags_name_}" \
"default:'${_flags_default_}' help:'${_flags_help_}'" \
"short:${_flags_short_}"
# TODO(kward): check for validity of the flag name (e.g. dashes)
_flags_return_=${FLAGS_TRUE}
_flags_usName_="`_flags_underscoreName "${_flags_name_}"`"
# check whether the flag name is reserved
echo " ${FLAGS_RESERVED} " |grep " ${_flags_name_} " >/dev/null
if [ $? -eq 0 ]; then
# Check whether the flag name is reserved.
_flags_itemInList "${_flags_usName_}" "${__FLAGS_RESERVED_LIST}"
if [ $? -eq ${FLAGS_TRUE} ]; then
flags_error="flag name (${_flags_name_}) is reserved"
_flags_return_=${FLAGS_ERROR}
fi
# require short option for getopt that don't support long options
# Require short option for getopt that don't support long options.
if [ ${_flags_return_} -eq ${FLAGS_TRUE} \
-a ${__FLAGS_GETOPT_VERS} -ne ${__FLAGS_GETOPT_VERS_ENH} \
-a "${__FLAGS_GETOPT_VERS}" -ne "${__FLAGS_GETOPT_VERS_ENH}" \
-a "${_flags_short_}" = "${__FLAGS_NULL}" ]
then
flags_error="short flag required for (${_flags_name_}) on this platform"
_flags_return_=${FLAGS_ERROR}
fi
# check for existing long name definition
# Check for existing long name definition.
if [ ${_flags_return_} -eq ${FLAGS_TRUE} ]; then
if _flags_itemInList "${_flags_name_}" \
${__flags_longNames} ${__flags_boolNames}
then
flags_error="flag name ([no]${_flags_name_}) already defined"
if _flags_itemInList "${_flags_usName_}" "${__flags_definedNames}"; then
flags_error="definition for ([no]${_flags_name_}) already exists"
_flags_warn "${flags_error}"
_flags_return_=${FLAGS_FALSE}
fi
fi
# check for existing short name definition
# Check for existing short name definition.
if [ ${_flags_return_} -eq ${FLAGS_TRUE} \
-a "${_flags_short_}" != "${__FLAGS_NULL}" ]
then
if _flags_itemInList "${_flags_short_}" ${__flags_shortNames}; then
if _flags_itemInList "${_flags_short_}" "${__flags_shortNames}"; then
flags_error="flag short name (${_flags_short_}) already defined"
_flags_warn "${flags_error}"
_flags_return_=${FLAGS_FALSE}
fi
fi
# handle default value. note, on several occasions the 'if' portion of an
# if/then/else contains just a ':' which does nothing. a binary reversal via
# Handle default value. Note, on several occasions the 'if' portion of an
# if/then/else contains just a ':' which does nothing. A binary reversal via
# '!' is not done because it does not work on all shells.
if [ ${_flags_return_} -eq ${FLAGS_TRUE} ]; then
case ${_flags_type_} in
${__FLAGS_TYPE_BOOLEAN})
if _flags_validateBoolean "${_flags_default_}"; then
if _flags_validBool "${_flags_default_}"; then
case ${_flags_default_} in
true|t|0) _flags_default_=${FLAGS_TRUE} ;;
false|f|1) _flags_default_=${FLAGS_FALSE} ;;
......@@ -284,7 +370,7 @@ _flags_define()
;;
${__FLAGS_TYPE_FLOAT})
if _flags_validateFloat "${_flags_default_}"; then
if _flags_validFloat "${_flags_default_}"; then
:
else
flags_error="invalid default flag value '${_flags_default_}'"
......@@ -293,7 +379,7 @@ _flags_define()
;;
${__FLAGS_TYPE_INTEGER})
if _flags_validateInteger "${_flags_default_}"; then
if _flags_validInt "${_flags_default_}"; then
:
else
flags_error="invalid default flag value '${_flags_default_}'"
......@@ -301,7 +387,7 @@ _flags_define()
fi
;;
${__FLAGS_TYPE_STRING}) ;; # everything in shell is a valid string
${__FLAGS_TYPE_STRING}) ;; # Everything in shell is a valid string.
*)
flags_error="unrecognized flag type '${_flags_type_}'"
......@@ -311,28 +397,43 @@ _flags_define()
fi
if [ ${_flags_return_} -eq ${FLAGS_TRUE} ]; then
# store flag information
eval "FLAGS_${_flags_name_}='${_flags_default_}'"
eval "__flags_${_flags_name_}_${__FLAGS_INFO_TYPE}=${_flags_type_}"
eval "__flags_${_flags_name_}_${__FLAGS_INFO_DEFAULT}=\
# Store flag information.
eval "FLAGS_${_flags_usName_}='${_flags_default_}'"
eval "__flags_${_flags_usName_}_${__FLAGS_INFO_TYPE}=${_flags_type_}"
eval "__flags_${_flags_usName_}_${__FLAGS_INFO_DEFAULT}=\
\"${_flags_default_}\""
eval "__flags_${_flags_name_}_${__FLAGS_INFO_HELP}=\"${_flags_help_}\""
eval "__flags_${_flags_name_}_${__FLAGS_INFO_SHORT}='${_flags_short_}'"
eval "__flags_${_flags_usName_}_${__FLAGS_INFO_HELP}=\"${_flags_help_}\""
eval "__flags_${_flags_usName_}_${__FLAGS_INFO_SHORT}='${_flags_short_}'"
# append flag name(s) to list of names
__flags_longNames="${__flags_longNames}${_flags_name_} "
# append flag names to name lists
__flags_shortNames="${__flags_shortNames}${_flags_short_} "
[ ${_flags_type_} -eq ${__FLAGS_TYPE_BOOLEAN} ] && \
__flags_longNames="${__flags_longNames}${_flags_name_} "
[ "${_flags_type_}" -eq "${__FLAGS_TYPE_BOOLEAN}" ] && \
__flags_boolNames="${__flags_boolNames}no${_flags_name_} "
# Append flag names to defined names for later validation checks.
__flags_definedNames="${__flags_definedNames}${_flags_usName_} "
[ "${_flags_type_}" -eq "${__FLAGS_TYPE_BOOLEAN}" ] && \
__flags_definedNames="${__flags_definedNames}no${_flags_usName_} "
fi
flags_return=${_flags_return_}
unset _flags_default_ _flags_help_ _flags_name_ _flags_return_ _flags_short_ \
_flags_type_
unset _flags_default_ _flags_help_ _flags_name_ _flags_return_ \
_flags_short_ _flags_type_ _flags_usName_
[ ${flags_return} -eq ${FLAGS_ERROR} ] && _flags_error "${flags_error}"
return ${flags_return}
}
# Underscore a flag name by replacing dashes with underscores.
#
# Args:
# unnamed: string: log flag name
# Output:
# string: underscored name
_flags_underscoreName() {
echo "$1" |tr z- z_
}
# Return valid getopt options using currently defined list of long options.
#
# This function builds a proper getopt option string for short (and long)
......@@ -344,106 +445,117 @@ _flags_define()
# string: generated option string for getopt
# Returns:
# boolean: success of operation (always returns True)
_flags_genOptStr()
{
_flags_genOptStr() {
_flags_optStrType_=$1
_flags_opts_=''
for _flags_flag_ in ${__flags_longNames}; do
_flags_type_=`_flags_getFlagInfo ${_flags_flag_} ${__FLAGS_INFO_TYPE}`
for _flags_name_ in ${__flags_longNames}; do
_flags_usName_="`_flags_underscoreName "${_flags_name_}"`"
_flags_type_="`_flags_getFlagInfo "${_flags_usName_}" "${__FLAGS_INFO_TYPE}"`"
[ $? -eq ${FLAGS_TRUE} ] || _flags_fatal 'call to _flags_type_ failed'
case ${_flags_optStrType_} in
${__FLAGS_OPTSTR_SHORT})
_flags_shortName_=`_flags_getFlagInfo \
${_flags_flag_} ${__FLAGS_INFO_SHORT}`
_flags_shortName_="`_flags_getFlagInfo \
"${_flags_usName_}" "${__FLAGS_INFO_SHORT}"`"
if [ "${_flags_shortName_}" != "${__FLAGS_NULL}" ]; then
_flags_opts_="${_flags_opts_}${_flags_shortName_}"
# getopt needs a trailing ':' to indicate a required argument
[ ${_flags_type_} -ne ${__FLAGS_TYPE_BOOLEAN} ] && \
# getopt needs a trailing ':' to indicate a required argument.
[ "${_flags_type_}" -ne "${__FLAGS_TYPE_BOOLEAN}" ] && \
_flags_opts_="${_flags_opts_}:"
fi
;;
${__FLAGS_OPTSTR_LONG})
_flags_opts_="${_flags_opts_:+${_flags_opts_},}${_flags_flag_}"
_flags_opts_="${_flags_opts_:+${_flags_opts_},}${_flags_name_}"
# getopt needs a trailing ':' to indicate a required argument
[ ${_flags_type_} -ne ${__FLAGS_TYPE_BOOLEAN} ] && \
[ "${_flags_type_}" -ne "${__FLAGS_TYPE_BOOLEAN}" ] && \
_flags_opts_="${_flags_opts_}:"
;;
esac
done
echo "${_flags_opts_}"
unset _flags_flag_ _flags_opts_ _flags_optStrType_ _flags_shortName_ \
_flags_type_
unset _flags_name_ _flags_opts_ _flags_optStrType_ _flags_shortName_ \
_flags_type_ _flags_usName_
return ${FLAGS_TRUE}
}
# Returns flag details based on a flag name and flag info.
#
# Args:
# string: long flag name
# string: underscored flag name
# string: flag info (see the _flags_define function for valid info types)
# Output:
# string: value of dereferenced flag variable
# Returns:
# integer: one of FLAGS_{TRUE|FALSE|ERROR}
_flags_getFlagInfo()
{
_flags_name_=$1
_flags_info_=$2
_flags_nameVar_="__flags_${_flags_name_}_${_flags_info_}"
_flags_strToEval_="_flags_value_=\"\${${_flags_nameVar_}:-}\""
_flags_getFlagInfo() {
# Note: adding gFI to variable names to prevent naming conflicts with calling
# functions
_flags_gFI_usName_=$1
_flags_gFI_info_=$2
# Example: given argument usName (underscored flag name) of 'my_flag', and
# argument info of 'help', set the _flags_infoValue_ variable to the value of
# ${__flags_my_flag_help}, and see if it is non-empty.
_flags_infoVar_="__flags_${_flags_gFI_usName_}_${_flags_gFI_info_}"
_flags_strToEval_="_flags_infoValue_=\"\${${_flags_infoVar_}:-}\""
eval "${_flags_strToEval_}"
if [ -n "${_flags_value_}" ]; then
if [ -n "${_flags_infoValue_}" ]; then
# Special value '§' indicates no help string provided.
[ "${_flags_gFI_info_}" = ${__FLAGS_INFO_HELP} \
-a "${_flags_infoValue_}" = '§' ] && _flags_infoValue_=''
flags_return=${FLAGS_TRUE}
else
# see if the _flags_name_ variable is a string as strings can be empty...
# note: the DRY principle would say to have this function call itself for
# See if the _flags_gFI_usName_ variable is a string as strings can be
# empty...
# Note: the DRY principle would say to have this function call itself for
# the next three lines, but doing so results in an infinite loop as an
# invalid _flags_name_ will also not have the associated _type variable.
# Because it doesn't (it will evaluate to an empty string) the logic will
# try to find the _type variable of the _type variable, and so on. Not so
# good ;-)
_flags_typeVar_="__flags_${_flags_name_}_${__FLAGS_INFO_TYPE}"
_flags_strToEval_="_flags_type_=\"\${${_flags_typeVar_}:-}\""
#
# Example cont.: set the _flags_typeValue_ variable to the value of
# ${__flags_my_flag_type}, and see if it equals '4'.
_flags_typeVar_="__flags_${_flags_gFI_usName_}_${__FLAGS_INFO_TYPE}"
_flags_strToEval_="_flags_typeValue_=\"\${${_flags_typeVar_}:-}\""
eval "${_flags_strToEval_}"
if [ "${_flags_type_}" = "${__FLAGS_TYPE_STRING}" ]; then
# shellcheck disable=SC2154
if [ "${_flags_typeValue_}" = "${__FLAGS_TYPE_STRING}" ]; then
flags_return=${FLAGS_TRUE}
else
flags_return=${FLAGS_ERROR}
flags_error="invalid flag name (${_flags_nameVar_})"
flags_error="missing flag info variable (${_flags_infoVar_})"
fi
fi
echo "${_flags_value_}"
unset _flags_info_ _flags_name_ _flags_strToEval_ _flags_type_ _flags_value_ \
_flags_nameVar_ _flags_typeVar_
echo "${_flags_infoValue_}"
unset _flags_gFI_usName_ _flags_gfI_info_ _flags_infoValue_ _flags_infoVar_ \
_flags_strToEval_ _flags_typeValue_ _flags_typeVar_
[ ${flags_return} -eq ${FLAGS_ERROR} ] && _flags_error "${flags_error}"
return ${flags_return}
}
# check for presense of item in a list. passed a string (e.g. 'abc'), this
# function will determine if the string is present in the list of strings (e.g.
# ' foo bar abc ').
# Check for presence of item in a list.
#
# Passed a string (e.g. 'abc'), this function will determine if the string is
# present in the list of strings (e.g. ' foo bar abc ').
#
# Args:
# _flags__str: string: string to search for in a list of strings
# _flags_str_: string: string to search for in a list of strings
# unnamed: list: list of strings
# Returns:
# boolean: true if item is in the list
_flags_itemInList()
{
_flags_itemInList() {
_flags_str_=$1
shift
echo " ${*:-} " |grep " ${_flags_str_} " >/dev/null
if [ $? -eq 0 ]; then
flags_return=${FLAGS_TRUE}
else
flags_return=${FLAGS_FALSE}
fi
case " ${*:-} " in
*\ ${_flags_str_}\ *) flags_return=${FLAGS_TRUE} ;;
*) flags_return=${FLAGS_FALSE} ;;
esac
unset _flags_str_
return ${flags_return}
......@@ -453,22 +565,23 @@ _flags_itemInList()
#
# Output:
# integer: width in columns of the current screen.
_flags_columns()
{
_flags_columns() {
if [ -z "${__flags_columns}" ]; then
# determine the value and store it
if eval stty size >/dev/null 2>&1; then
# stty size worked :-)
# shellcheck disable=SC2046
set -- `stty size`
__flags_columns=$2
elif eval tput cols >/dev/null 2>&1; then
__flags_columns="${2:-}"
fi
fi
if [ -z "${__flags_columns}" ]; then
if eval tput cols >/dev/null 2>&1; then
# shellcheck disable=SC2046
set -- `tput cols`
__flags_columns=$1
else
__flags_columns=80 # default terminal width
__flags_columns="${1:-}"
fi
fi
echo ${__flags_columns}
echo "${__flags_columns:-80}"
}
# Validate a boolean.
......@@ -477,8 +590,7 @@ _flags_columns()
# _flags__bool: boolean: value to validate
# Returns:
# bool: true if the value is a valid boolean
_flags_validateBoolean()
{
_flags_validBool() {
_flags_bool_=$1
flags_return=${FLAGS_TRUE}
......@@ -495,55 +607,53 @@ _flags_validateBoolean()
# Validate a float.
#
# Args:
# _flags__float: float: value to validate
# _flags_float_: float: value to validate
# Returns:
# bool: true if the value is a valid float
_flags_validateFloat()
{
# bool: true if the value is a valid integer
_flags_validFloat() {
flags_return=${FLAGS_FALSE}
[ -n "$1" ] || return ${flags_return}
_flags_float_=$1
if _flags_validateInteger ${_flags_float_}; then
if _flags_validInt "${_flags_float_}"; then
flags_return=${FLAGS_TRUE}
elif _flags_useBuiltin; then
_flags_float_whole_=${_flags_float_%.*}
_flags_float_fraction_=${_flags_float_#*.}
[ "${_flags_float_whole_}" = '-' ] && _flags_float_whole_='-0'
if _flags_validInt "${_flags_float_whole_:-0}" -a \
_flags_validInt "${_flags_float_fraction_}"; then
flags_return=${FLAGS_TRUE}
fi
unset _flags_float_whole_ _flags_float_fraction_
else
flags_return=${FLAGS_TRUE}
case ${_flags_float_} in
-*) # negative floats
_flags_test_=`expr "${_flags_float_}" : '\(-[0-9][0-9]*\.[0-9][0-9]*\)'`
-*) # Negative floats.
_flags_test_=`${FLAGS_EXPR_CMD} "${_flags_float_}" :\
'\(-[0-9]*\.[0-9]*\)'`
;;
*) # positive floats
_flags_test_=`expr "${_flags_float_}" : '\([0-9][0-9]*\.[0-9][0-9]*\)'`
*) # Positive floats.
_flags_test_=`${FLAGS_EXPR_CMD} "${_flags_float_}" :\
'\([0-9]*\.[0-9]*\)'`
;;
esac
[ "${_flags_test_}" != "${_flags_float_}" ] && flags_return=${FLAGS_FALSE}
unset _flags_test_
fi
unset _flags_float_ _flags_test_
unset _flags_float_ _flags_float_whole_ _flags_float_fraction_
return ${flags_return}
}
# Validate an integer.
#
# Args:
# _flags__integer: interger: value to validate
# _flags_int_: integer: value to validate
# Returns:
# bool: true if the value is a valid integer
_flags_validateInteger()
{
_flags_int_=$1
flags_return=${FLAGS_TRUE}
case ${_flags_int_} in
-*) # negative ints
_flags_test_=`expr "${_flags_int_}" : '\(-[0-9][0-9]*\)'`
;;
*) # positive ints
_flags_test_=`expr "${_flags_int_}" : '\([0-9][0-9]*\)'`
;;
esac
[ "${_flags_test_}" != "${_flags_int_}" ] && flags_return=${FLAGS_FALSE}