- 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.
-
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. Bothsuccess
anderr
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 eithererr
orsuccess
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)
-
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
With this script comes a wtfmagic interactive mode which prompts the user to enter variables through stdin instead of the command line.
-
This works by first defining which variables should be prompted for in the
$interactive_opts
variable. -
Making sure
usage
outputs valid information, where an options longname (eg. --password) has the same name as the variable ininteractive_opts
. -
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). -
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.
- Daniel Mills, options.bash