GithubHelp home page GithubHelp logo

optparse's Introduction

Optparse

A BASH wrapper for getopts, for simple command-line argument parsing

( ! ) ATTENTION MAC USERS

Optparse requires gnu-sed instead of the default Mac version of sed, which you can do with the following:

brew install gnu-sed --with-default-names

What is this?

A wrapper that provides a clean and easy way to parse arguments to your BASH scripts. It lets you define short and long option names, handle flag variables, and set default values for optional arguments, all while aiming to be as minimal as possible: One line per argument definition.

Usage

See sample_head.sh for a demonstration of optparse

1. Define your arguments

Each argument to the script is defined with optparse.define, which specifies the option names, a short description, the variable it sets and the default value (if any).

optparse.define short=f long=file desc="The input file" variable=filename

Flags are defined in exactly the same way, but with an extra parameter value that is assigned to the variable.

optparse.define short=v long=verbose desc="Set flag for verbose mode" variable=verbose_mode value=true default=false

2. Evaluate your arguments

The optparse.build function creates a temporary header script based on the provided argument definitions. Simply source the file the function returns, to parse the arguments.

source $( optparse.build )

That's it!

The script can now make use of the variables. Running the script (without any arguments) should give you a neat usage description.

usage: ./script.sh [OPTIONS]

OPTIONS:

    -f --file  :  The input file
	-v --verbose  :  Set flag for verbose mode

	-? --help  :  usage

Supported definition parameters

All definition parameters for optparse.define are provided as key=value pairs, seperated by an = sign.

short

a short, single-letter name for the option

long

a longer expanded option name

variable

the target variable that the argument represents

value(optional)

the value to set the variable to. If unspecified, user is expected to provide a value.

desc(optional)

a short description of the argument (to build the usage description)

default(optional)

the default value to set the variable to if argument not specified

Installation

  1. Download/clone optparse.bash
  2. Add
`source /path/to/optparse.bash` 

to ~/.bashrc

optparse's People

Contributors

dol avatar nk412 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.