Code Monkey home page Code Monkey logo

petopts's Introduction

petopts

Portable and simple option parsing, powered by getopts(1p).

pet /pɛt/ To stroke in a gentle or loving manner.

Unlike most (bash) option parser frameworks, where the complexity can easily dwarf that of the actual script, petopts is purposefully made as simple and concise as possible.

Features

  • Easy to use
  • Minimal/simple
  • Portable
  • No GNU style long options
  • No optional parameters
  • Count of option occurrences
  • Short usage message
  • Support -- argument to indicates the end of the options
  • Proper separation of option parsing and handling
  • POSIX utility conventions compliance

Usage

tl;dr

  1. Copy the content of petopts.sh to the top of a script
  2. Change options, usage and version
  3. Handle the parsed options and remaining operands

Given options="a b: c", petopts would set opt_a, opt_b and opt_c to the amount each of the options are supplied (empty if 0). arg_b would contain the option argument of the -b option.

Details

petopts.sh is supposed to be used at the top of a script. The following 3 variables have to be adapted:

options: A string containing the space separated list of alphanumeric option characters. If a character is followed by a <colon>, the option shall be expected to have an argument.

usage: The string printed by the usage() function.

version: The string printed by the version() function.

For each option in the options variable, petopts will set corresponding variables in the form of opt_* and arg_*, where * is the option character. The opt_* variables will hold the number of occurrences of the options (unset if an option does not occur), while the arg_* variables store the option arguments (empty if an option has no option argument).

After parsing the options, the positional parameters are shifted until only operands remain.

For example, to conditionally do something with the option 'a', 'h' and 'v', we could do:

[ "$opt_h" ] && usage
[ "$opt_v" ] && version
[ "$opt_a" ] && printf 'Option a present with argument: %s\n' "$arg_a"

petopts provides the following functions:

die(): Prints its arguments to stderr and exits the script with status 1.

usage(): Prints the content of the usage variable and exits the script with status 0.

version(): Prints the content of the version variable and exits the script with status 0.

For a complete script using petopts, check example.sh.

Usage of eval

eval is considered dangerous because it can execute "dirty" data and thus allow arbitrary code execution. However, how petopts uses eval, to dynamically set the option variables, is totally safe.

Limitations

  • No GNU style long options
  • No optional parameters

petopts's People

Contributors

aaronngi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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