Git Product home page Git Product logo

paramparser's Introduction

Paramparser

The best way to parse command line arguments in BASH

Use paramparser to write a command-line interfaces. You only define optional and requiered arguments and paramparser will store parsed argument value in the generated bash variable. paramparser also generates help and error message when the users provide a wrong command.

How to Install paramparser

System installation

sudo mkdir -p /usr/lib/bash
sudo curl https://raw.githubusercontent.com/Maillol/paramparser/master/paramparser.sh  --output /usr/lib/bash/paramparser.sh

User installation

mkdir -p ~/.local/lib/bash
curl https://raw.githubusercontent.com/Maillol/paramparser/master/paramparser.sh  --output ~/.local/lib/bash/paramparser.sh

Example using paramparser :-D

source paramparser.sh

optAdd  'config' 'CONFIG_FILE' 'Input config file' '?' ':NO_CONFIG:'
optAdd  'col' 'NUM_COLUMN' 'Display this column'   '*'
optAddFlag 'verbose' 'Displays more descriptive information than the default output.'
optAddPos 'output_file'  'Tabular file with first column is an id'
optAddPos 'input_file' 'Path to output file'  '+'

optParse "$@"

# paramparser will generate bash variable containing the parsing parameters for you.
echo 'config:' $config
echo 'col:' ${col[@]}
echo 'verbose:' $verbose
echo 'output_file:' $output_file
echo 'input_file:' "${input_file[@]}"

Tutorial

1. paramparser.sh is included.

source paramparser.sh

2. Optionals and positionals arguments are defined.

# Add optionals arguments, inline help and cardinality.
# Use optAdd to add option.
# optAdd take 5 parameters:
#   1 - The name of bash variable to create
#   2 - The name of parameter to display in the help
#   3 - The inline help to display
#   4 - The command-line parameters can be present ('?' one or zero) ('*' zero or several) time
#   5 - The default value to store in the variable to create.

optAdd  'config' 'CONFIG_FILE' 'Input config file' '?' ':NO_CONFIG:'

# optAddFlag is like optAdd but the stored value in the created variable will be true or false.
optAddFlag 'verbose' 'Displays more descriptive information than the default output.'

# Add positionals arguments
optAddPos 'output_file'  'Tabular file with first column is an id'
optAddPos 'input_file' 'Path to input files'  '+'  # '+' means one or multiple input files

3. User parameters are parsed.

# Use optParse function to parse users arguments.
optParse "$@"

# OptParse has initialised variables specified with optAdd, optAddFlag or optAddPos functions.
echo 'config :' $config
echo 'verbose :' $verbose
echo 'positional argument 1 :' $output_file
echo 'last positionals arguments  :' "${input_file[@]}"

4. It is done ! Try your program :-)

$ ./example.sh  -h
USAGE
	example.sh [--config] [--verbose] output_file input_file [input_file]...


POSITIONALS ARGUMENTS
	output_file
		Tabular file with first column is an id


	input_file
		Path to output file


OPTIONS
	--config CONFIG_FILE
		Input config file


	--verbose
		Displays more descriptive information than the default output.


	-h --help
		Display this help and exit.
$ ./example.sh  --config my_config  foo  bar1 bar2
config : my_config
verbose : false
positional argument 1 : foo
last positionals arguments  : bar1 bar2

Example with Sub-commands

#!/bin/bash

source paramparser.sh

optAddCommand prepare 'Use this command to prepare a drink'
optAddCommand serve-drink-in-the-right-way 'Use this command to serve a drink' 'serve'


function prepare {
    optAddCommand prepare-tea 'Prepare a tea' 'tea'
    optAddCommand prepare-coffe 'Prepare a coffe' 'coffe'
    optParse $@
}


function prepare-coffe {
    optAddFlag 'cold' 'Create a cold coffe'
    optParse $@

    echo 'cold :' $cold
}


function prepare-tea {
    optAdd 'color' 'COLOR' 'red, yellow, green or black' '?' 'green'
    optParse $@

    echo 'color :' $color
}


function serve-drink-in-the-right-way {
    optAddPos 'customer' 'The name of the customer' '+'
    optParse $@

    echo 'customer :' ${customer[@]}
}


optParse $@

How provide paramparser in the script using paramparser

if [[ -f ~/.local/lib/bash/paramparser.sh ]]; then
    source ~/.local/lib/bash/paramparser.sh
elif [[ -f /usr/lib/bash/paramparser.sh ]]; then
    source /usr/lib/bash/paramparser.sh
else
    mkdir -p ~/.local/lib/bash
    curl https://raw.githubusercontent.com/Maillol/paramparser/master/paramparser.sh  --output ~/.sources/paramparser.sh
    source ~/.sources/paramparser.sh
fi

Documentation

You can read the generated documentation executing paramparser.sh

bash paramparser.sh

paramparser's People

Contributors

maillol avatar

Stargazers

 avatar

Watchers

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