Git Product home page Git Product logo

bash-boilerplate's Introduction

Bash boilerplate

Features

  • Interactive mode
  • Quiet mode
  • CLI options parser supporting -n --name --name=Oxy --name Oxy
  • Also supports bundling of flags. ie. -vf instead of -v -f
  • Helper functions for printing messages.
  • Automatically remove color escape codes if the script is piped.

Functions

Print functions

  • die() Output message to stderr and exit with error code 1.

  • out() Output message.

  • err() Output message to stderr but continue running.

  • success() Output message as a string. Both success and err will output message with a colorized symbol, as long as the script isn't piped.

  • log() Will only output message if user has activated verbose flag.

  • notify() Delegate the message to either err or success depending on the last return code. Remember this function needs to be called once a return code is available. Eg.

    foobar; notify "foobar copied files"
    
    notify "foobar copied files" $(foobar)

Misc helpers

  • escape() Escape slashes in a string

  • confirm() Prompt the user to answer Yes or No. This will automatically return true if --force is used. Eg.

    if ! confirm "Delete file"; then
      continue;
    fi

Interactive mode

With this script comes a wtfmagic interactive mode which prompts the user to enter variables through stdin instead of the command line.

  1. This works by first defining which variables should be prompted for in the $interactive_opts variable.

  2. Making sure usage outputs valid information, where an options longname (eg. --password) has the same name as the variable in interactive_opts.

  3. Once the script has parsed all variables supplied through the command line, it will iterate through the interactive_opts array and parse the usage file for the description (also supports multiline).

  4. Now the user will be prompted and can enter the value through stdin. Note, if the variable is named password, interactive mode will automatically hide the input from prying eyes.

Once a script has many CLI options it becomes annoying to remember them all and this is when interactive mode shines. You can support both the scriptable CLI as well as a user friendly alternative for that one time per year when you actually need the script.

Acknowledgment

bash-boilerplate's People

Contributors

brammeleman avatar oxyc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bash-boilerplate's Issues

Provide some examples

Thank you for the great work!
It would be great to see some simple usage examples.

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.