ursadk / getopts_long Goto Github PK
View Code? Open in Web Editor NEWAn "upgrade" to BASH built-in getopts, with support for GNU-style long option.
License: MIT License
An "upgrade" to BASH built-in getopts, with support for GNU-style long option.
License: MIT License
Try
_usage () { echo "usage" 1>&2; exit ${1:-0}; }
_die () { printf "\n$1\n" 1>&2; _usage ${2:-1}; }
while read args ; do
OPTIND=1
while getopts_long ":hvab: alpha bravo: charlie" OPT $args ; do
case "$OPT" in
h|help ) _usage ;; # E.g., help option
v|verbose) ((++verbose)) ;; # E.g., an incrementing option
a|alpha ) alpha=1 ;; # E.g., non-arg-requiring option
b|bravo ) bravo="$OPTARG" ;; # E.g., argument-requiring option
charlie ) charlie=1 ;; # E.g., long-option-only
':') _die "OPTION '${OPTARG}': MISSING ARGUMENT" 1;;
'?') _die "UNKNOWN OPTION: '${OPTARG}'" 2;;
esac
echo "OPT=$OPT, OPTIND=$OPTIND, OPTARG=$OPTARG"
done
done < <( cat <<EOF
-a-- param
EOF
)
and compare with the behavior of getopts
. getopts
is correctly able to stop parsing when the double-hyphen is grouped with the non-argument-requiring option, whether or not it is space separated, and whether or not '-:' is included in the shortopt spec.
I have this code:
while getopts_long ':remoteHost: toDir:' OPTKEY; do
case ${OPTKEY} in
'remoteHost')
REMOTE_HOST="${OPTARG}"
;;
'toDir')
TO_DIR="${OPTARG}"
;;
'?')
echo "INVALID OPTION -- ${OPTARG}" >&2
exit 1
;;
':')
echo "MISSING ARGUMENT for option -- ${OPTARG}" >&2
exit 1
;;
*)
echo "UNIMPLEMENTED OPTION -- ${OPTKEY}" >&2
exit 1
;;
esac
done
and I'm invoking my script with arguments: --remoteHost=somehost --toDir=/tmp/foo
The result is:
INVALID OPTION -- remoteHost
The result is the same if I use as arguments: --remote-host somehost --to-dir /tmp/foo
How do I specify that both --remoteHost
and --toDir
are valid long options?
Hi,
I'm using getopts_long inside a nested function contained in the script I launch but when executing getopts_long, it seems to not retrieve the #BASH_ARGV since ${#BASH_ARGV} equals 0
Do you have any easy solution to solve this ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.