GithubHelp home page GithubHelp logo

optparse's Introduction

optparse

Bash Options Parser

Attribution with conditions & description of options and flags

Requirements:

- BAsh interpreter

No installation:

- Let the source file be in the PATH environment variable

Usage:

Here is a quick script opt_demo to introduce optparse:

#!/bin/bash

# Mandatory: opts & flags array
opts=(
"n:'number between 5 and 10' [[ n -lt 10 && n -gt 5 ]]"
"x:'low-case string' [[ x =~ ^[a-z]+$ ]]"
)

# -h flag is by default
flags=("A:Affect all options as variables")

# Optional description
function about() {
    echo "Demonstration script"
}

# Let the script do the job
source optparse

# If you want to affect options as variables -A:
[[ "${OPTS[A]}" =~ true ]] && affect_opt
echo "n="$n

# TODO CODE HERE :)

exit 0

Some outputs:

$ opt_demo -h

Usage: ./opt_demo [options] [flags]

Demonstration script

Options:
        -n               'number between 5 and 10' [[ n -lt 10 && n -gt 5 ]]
        -x               'low-case string' [[ x =~ ^[a-z]+$ ]]   
Flags:
        -A    false      Affect all options as variables     
        -h    true

Given conditions are tested at first:

$ opt_demo -x coucou -n 3
Condition failed: [[ 3 -lt 10 && 3 -gt 5 ]]

No output, arguments are good to be used:

$ opt_demo -x coucou -n 6

We can affect all options as variables using -A:

$ opt_demo -n 6 -A
n=6

Description part is a string without empty spaces (the trick to have spaces on the screen is to use AltGr+SpaceBar when you type it).

optparse's People

Contributors

dukethis avatar

Watchers

James Cloos 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.