From b1a19c618e81c4db9b6cc508640e408aed49d7fe Mon Sep 17 00:00:00 2001 From: Knut Kohl Date: Tue, 15 Jul 2014 13:08:52 +0200 Subject: [PATCH] - Add handling of "--" - Add "-h" as also possible help request --- optparse.bash | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/optparse.bash b/optparse.bash index 894c8fc..4041156 100644 --- a/optparse.bash +++ b/optparse.bash @@ -59,14 +59,18 @@ function optparse.define(){ # build OPTIONS and help optparse_usage="${optparse_usage}#NL#TB${short} $(printf "%-25s %s" "${long}:" "${desc}")" if [ "$default" != "" ]; then - optparse_usage="${optparse_usage} [default:$default]" + if [ "$val" != "\$OPTARG" ]; then + optparse_usage="${optparse_usage} [flag]" + else + optparse_usage="${optparse_usage} [default:$default]" + fi fi optparse_contractions="${optparse_contractions}#NL#TB#TB${long})#NL#TB#TB#TBparams=\"\$params ${short}\";;" if [ "$default" != "" ]; then optparse_defaults="${optparse_defaults}#NL${variable}=${default}" fi optparse_arguments_string="${optparse_arguments_string}${shortname}" - if [ "$val" = "\$OPTARG" ]; then + if [ "$val" == "\$OPTARG" ]; then optparse_arguments_string="${optparse_arguments_string}:" fi optparse_process="${optparse_process}#NL#TB#TB${shortname})#NL#TB#TB#TB${variable}=\"$val\";;" @@ -82,12 +86,12 @@ function optparse.build(){ cat << EOF > $build_file function usage(){ cat << XXX -usage: \$0 [OPTIONS] +usage: \$0 [OPTIONS] \$ARGUMENTS OPTIONS: $optparse_usage - -? --help : usage + -? -h --help: usage XXX } @@ -100,11 +104,15 @@ while [ \$# -ne 0 ]; do case "\$param" in $optparse_contractions - "-?"|--help) + "-?"|-h|--help) usage exit 0;; *) - if [[ "\$param" == --* ]]; then + if [[ "\$param" == -- ]]; then + # getopts will handle the "--" correct and stops parsing + params="\$params -- \$@" + break + elif [[ "\$param" == --* ]]; then echo -e "Unrecognized long option: \$param" usage exit 1 @@ -132,6 +140,9 @@ while getopts "$optparse_arguments_string" option; do esac done +# Shift out all args parsed by getopts +shift \$((\$OPTIND - 1)) + # Clean up after self rm $build_file