Code Monkey home page Code Monkey logo

simple_opts.sh's Introduction

simple_opts.sh

Simple Bash options parser, no nonsense.

# define some options
opt --file-name= -f
opt --no-prompt # now defaults to prompt=true
opt --verbose -v
# or on one line: opt --file-name= -f --verbose -v --no-prompt

# run the parser
opt_parse --file-name path/to/file.sh -v --no-prompt foo bar
# or: parse foo bar --file-name=path/to/file.sh -v --no-prompt
# or: parse foo --file_name path/to/file.sh bar -v --no-prompt
# or: parse --file_name=path/to/file.sh foo -v bar --no-prompt

# will set the following variables
echo "file_name: $file_name"
echo "verbose: $verbose"
echo "prompt: $prompt"
echo "ARGS: ${ARGS[@]}"

# file_name: path/to/file.sh
# verbose: true
# prompt:
# ARGS: foo bar

It is possible to define alternate variable names for options:

opt --file:path= -f:path=
opt --quiet:silent
opt -p:prompt

opt_parse --file path/to/file.sh --quiet -p

echo "path: $path"
echo "silent: $silent"
echo "prompt: $prompt"

# path: path/to/file.sh
# silent: true
# prompt: true

Short options can only be given with a long option that starts with the same letter, or with an alternate name.

Options can be given multiple times:

opt --tag=
opt_parse --tag foo --tag bar
echo "tags: $tags"
# tags: foo bar

Of course, in reality opt_parse would be called with $@:

opt --path= -p --verbose -v
opt_parse $@

simple_opts.sh's People

Contributors

dmytri avatar svenfuchs avatar

Watchers

 avatar

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.