GithubHelp home page GithubHelp logo

Comments (2)

jmcantrell avatar jmcantrell commented on August 30, 2024 1

It's really not complicated. All it does is arrange a handful of variables and format them a little.
It only accepts one argument and it's an error code which gets passed to exit. In fact, usage is essentially an alias to exit plus some documentation.

It's intended to be used like this:

unset OPTIND
while getopts ":h" option; do
    case $option in
        h) usage 0 ;;
        *) usage 1 ;;
    esac
done && shift $(($OPTIND - 1))

Or:

some_command_that_may_return_nonzero || usage 1

Here are some examples to show it's full range:

The only way to get any output is to set the script name.

source bashful-messages
SCRIPT_NAME=thingy
usage

Which outputs:

Usage: thingy [OPTIONS] 

GENERAL OPTIONS

    -h    Display this help message.

Adding some more variables:

source bashful-messages
SCRIPT_NAME=thingy
SCRIPT_ARGUMENTS="[key] [value]"
SCRIPT_USAGE="This is a thing that sets a value for the given key."
SCRIPT_DESCRIPTION="Sometimes you want to define a value for some key.
In those instances, you may consider using this command.
At some point, I plan on making this description more detailed."
SCRIPT_EXAMPLES="thingy some_setting on
thingy number_of_things 5
thingy -l
thingy -d some_setting"
SCRIPT_OPTIONS="-d    Delete key.
-l    List keys."
# for these, if they're set to anything, it will show in usage
INTERACTIVE=0  # interactive is off, but still set
VERBOSE=1      # verbose is on
usage

Will output this:

Usage: thingy [OPTIONS] [key] [value]
This is a thing that sets a value for the given key.

Sometimes you want to define a value for some key.
In those instances, you may consider using this command.
At some point, I plan on making this description more detailed.

EXAMPLES

thingy some_setting on
thingy number_of_things 5
thingy -l
thingy -d some_setting

GENERAL OPTIONS

    -h    Display this help message.

    -i    Interactive. Prompt for certain actions.
    -f    Don't prompt.

    -v    Be verbose.
    -q    Be quiet.

APPLICATION OPTIONS

    -d    Delete key.
    -l    List keys.

Try removing or changing those values to see the effect. Revisiting this function is reminding me how semi-pointless it is. It's very opinionated, and minimally useful, but it illustrates a way to formalize a style for command line documentation.

from bashful.

eleftrik avatar eleftrik commented on August 30, 2024

Thank you very much @jmcantrell!

from bashful.

Related Issues (12)

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.