Coder Social home page Coder Social logo

ursadk / getopts_long Goto Github PK

View Code? Open in Web Editor NEW
20.0 0.0 6.0 2.66 MB

An "upgrade" to BASH built-in getopts, with support for GNU-style long option.

License: MIT License

Shell 91.84% Dockerfile 8.16%
bash bash-script bash-function getopts getopt-long

getopts_long's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

getopts_long's Issues

Double hyphen not handled correctly in some situations

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.

Possible to parse multiple long options?

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?

Cannot get BASH_ARGV into a nested function

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 ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.