Code Monkey home page Code Monkey logo

spongecrab's Introduction

Spongecrab - bringing powerful argument parsing to bash scripts

See '--example' and '--generate' for reference.

Usage: spongecrab [OPTIONS] [-- <INPUT>...]

Arguments:
  [INPUT]...  Raw text to parse

Options:
      --name <NAME>              Application name [default: myscript]
      --about <ABOUT>            Application about text
  -p, --positional <POSITIONAL>  Add a (required) positional argument
  -o, --optional <OPTIONAL>      Add an optional positional argument
  -O, --option <OPTION>          Add an optional argument
  -f, --flag <FLAG>              Add a flag argument
  -c, --collect <COLLECT>        Collect remaining positional arguments
  -C, --collect+ <COLLECT>       Collect (required) remaining positional arguments
  -e, --extra <EXTRA>            Collect extra optional arguments after '--'
  -P, --prefix <PREFIX>          Prefix for parsed variable names
  -G, --generate                 Generate script boilerplate (see also '--example')
      --example                  Generate example script
  -h, --help                     Print help
  -V, --version                  Print version

Generated code (using --generate):

# Command line interface (based on `spongecrab --generate`)
APP_NAME=$(basename "$0")
ABOUT="program description"
# Argument syntax: "<arg_name>;<help_text>;<default_value>;<short_name>"
# -o, -c, -C are mutually exclusive
CLI=(
    -p "arg1;Positional argument"
    -o "arg2;Optional positional argument;<default value>"
    -O "option;Optional argument;;o"
    -f "flag;Optional flag argument;;f"
    # -c "collect_any;Optional remaining positional arguments"
    # -C "collect_some;Required remaining positional arguments"
    -e "extra;Optional extra arguments after '--'"
)
CLI=$(spongecrab --name "$APP_NAME" --about "$ABOUT" "${CLI[@]}" -- "$@") || exit 1
eval "$CLI" || exit 1

See the example script.

spongecrab's People

Contributors

arielhorwitz avatar

Stargazers

Elichai Turkel avatar

Watchers

 avatar

spongecrab's Issues

Arguments do not retain order

Once a CLI is parsed, the order of arguments as defined in the CLI variable are lost and ordered by argument type instead of the order of their definition.

For example the following script:

CLI=(
    -f "some-flag"
    -O "some-optional-argument"
)
CLI=$(spongecrab "${CLI[@]}" -- "$@") || exit 1
eval "$CLI" || exit 1

When run with --help will display:

Usage: myscript [OPTIONS]

Options:
      --some-optional-argument <some-optional-argument>
      --some-flag
  -h, --help                                             Print help

With some-optional-argument before some-flag.


If anyone feels this behavior is desired, please react or comment to show interest.

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.