# Load gl-common.bash
readonly MYAPP_DIR=$(dirname "$(echo "$(readlink -f "$0")" | sed -e 's,\\,/,g')")
. "${MYAPP_DIR}/gl-common.sh" MYAPP_
if [[ $? > 0 ]]; then echo "Error when loading file '${MYAPP_DIR}/gl-common.sh'"; exit 1; fi# Load gl-common.bash from a remote URL
eval "$(curl -sL https://raw.githubusercontent.com/golflima/gl-common.bash/master/gl-common.sh)"
if [[ $? > 0 ]]; then echo "Error when loading gl-common.sh"; exit 1; fi
gl_common_set_var_prefix MYAPP_gl-common.bash uses a prefix to identify special variables to enable some features for your scripts.
gl_common_get_var_prefixdisplays current variable prefix usedgl_common_set_var_prefix PREFIXsets the prefix to PREFIX
<PREFIX>NAMEis the name of your script/program/application, used byassertok<PREFIX>VERSIONis the version of your script/program/application, used byassertok<PREFIX>EXEC_<FILE_EXTENSION>is the path to the program to use when openning file withfile_execwith given<FILE_EXTENSION><PREFIX>HELP_FILEis the Markdown file used when callingusage<PREFIX>HELP_TITLE_PREFIXis the prefix of title sections to ignore when callingusage<PREFIX>HELP_HEADERis the text to displays always first when callingusage
Note: <PREFIX> in above special variables must be replaced by your prefix.
These variables can be accessed and assigned with these commands, without specifying the prefix:
gl_common_get_var NAMEgets the value of normally prefixed variable NAMEgl_common_set_var NAME VALUEgets the VALUE of normally prefixed variable NAME
${GL_COMMON_BASH_VERSION}contains the current version of gl-common.bash${GL_COMMON_BASH_PROGRAM_VAR_PREFIX}stores the variable prefix for current application/script
${NC}clears all formatting (reset to default)${BLACK}will output following text in black${DARK_GRAY}will output following text in dark grey${RED}will output following text in red${LIGHT_RED}will output following text in light red${GREEN}will output following text in green${LIGHT_GREEN}will output following text in light green${BROWN}will output following text in brown / dark yellow${YELLOW}will output following text in yellow${BLUE}will output following text in blue${LIGHT_BLUE}will output following text in light blue${PURPLE}will output following text in purple${LIGHT_PURPLE}will output following text in light purple${CYAN}will output following text in cyan${LIGHT_CYAN}will output following text in light cyan${LIGHT_GRAY}will output following text in light gray${BG_BLACK}will output following background text in black${BG_RED}will output following background text in red${BG_GREEN}will output following background text in green${BG_BROWN}will output following background text in brown / dark yellow${BG_BLUE}will output following background text in blue${BG_PURPLE}will output following background text in purple${BG_CYAN}will output following background text in cyan${BG_LIGHT_GRAY}will output following background text in light gray${CLEAR_BEFORE}clears all characters on the current line before current position${CLEAR_ALL}clears the whole current line${TAB}outputs a tabulation, equivalent to\t${LF}outputs a line feed, equivalent to\n
${FLAGS_TRUE}is set to0if not already set, for compatibility with shFlags${FLAGS_FALSE}is set to1if not already set, for compatibility with shFlags
get_var NAMEgets the value of given variable NAMEset_var NAME VALUEsets the value of given variable NAME with VALUEinit_var NAME COMMANDchecks if variable NAME is defined or sets its value to standard output of given COMMAND otherwiseinit_var NAME COMMAND PREFIXchecks if variable PREFIXNAME is defined and sets its value to variable NAME or sets value of variable NAME to standard output of given COMMAND otherwiseis_set NAMEchecks if given variable NAME is setis_empty NAMEchecks if given variable NAME is set and empty
assert_ok COMMAND $LINENOchecks if COMMAND return code is0and displays an error message if notassert_ko COMMAND $LINENOchecks if COMMAND return code is not0and displays an error message if notassert_equals EXPECTED ACTUAL $LINENOchecks if EXPECTED value is equals to ACTUAL value and displays an error message if notassert_notequals UNEXPECTED ACTUAL $LINENOchecks if UNEXPECTED value is not equals to ACTUAL value and displays an error message if not
Note: Commands above use variables NAME and VERSION when displaying failed assertions.
Note : Commands below must be used in pipes only.
escape_pipedescapes the value of previous piped commandecho_pipedechoes the content of previous piped commandecho_piped OPTIONSechoes the content of previous piped command with givenechoOPTIONS
trace TEXTdisplays TEXT in dark greytrace_var NAMEdisplays name of given variable NAME and its content in dark greydebug TEXTdisplays TEXT in dark grey, only if flagdebugis setdebug_var NAMEdisplays name of given variable NAME and its content in dark grey, only if flagdebugis setinfo TEXTdisplays TEXT in light bluesuccess TEXTdisplays TEXT in greenwarn TEXTdisplays TEXT in brown / dark yellowdieexists script's execution with return code1die TEXTexists script's execution with return code1and displays TEXT in light redendexists script's execution with return code0end TEXTexists script's execution with return code0and displays TEXT in greenquestion TEXTdisplays TEXT in light purplequestion TEXT NAMEdisplays TEXT in light purple and stores user's input (always from/dev/tty) (validated with [Enter] key) in variable NAMEquestion TEXT NAME DEFAULTdisplays TEXT in light purple and stores user's input, prefilled with default value DEFAULT, (always from/dev/tty) (validated with [Enter] key) in variable NAMEpassword TEXT NAMEdisplays TEXT in light purple and stores user's input (always from/dev/tty) but without displaying itremove_colorsdisables all colors, typically called this way:[[ has_option 'c/no-color' ]] && remove_colors
has_option OPTION "$@"checks if option OPTION was passed to the scriptget_option OPTION "$@"gets value of option OPTION passed to the script
Note: OPTION in commands above can be formed like:
Oto represent a simple option, passed like-Oto the scriptOPTIONto represent a long option, passed like--OPTIONto the script'O/OPTION'to represent either a simple option-Oor a long option--OPTIONpassed to the script
gl-common.bash provides some functions to easily deals with shFlags. It is not mandatory to use it when using gl-common.bash, and following functions are compatible with shFlags:
has_flag FLAGchecks if FLAG was passed to the scripthasnt_flag FLAGchecks if FLAG wasn't passed to the scriptempty_flag FLAGchecks if FLAG value is emptyget_flag FLAGgets value of FLAG passed to the scriptset_flag FLAG VALUEsets VALUE of given FLAG
Note: FLAG in commands above can be formed like:
Fto represent a simple flag, passed like-Fto the scriptFLAGto represent a long flag, passed like--FLAGto the script
Note: gl-common.bash automatically disables the help feature of shFlags because it already have its powerfull usage function.
gl-common.bash is able to dynamically bind options to flags with following functions. They more effective, but less powerfull:
has_option_wf FLAG "$@"checks if FLAG is set with shFlags, fallback tohas_option FLAGotherwise and stores the result like shFlags doesget_option_wf FLAG "$@"gets content of FLAG sets with shFlags, fallback toget_option FLAGif empty (or not set) and stores the result like shFlags does
Note: FLAG in commands above can be formed like:
Fto represent a simple flag, passed like-Fto the scriptFLAGto represent a long flag, passed like--FLAGto the script
curl_http_code URLreturns the HTTP code for given URLcurl_http_code URL OPTIONSreturns the HTTP code for given URL and applies given curl OPTIONS
colorize_markdown TEXTdisplays a colorized version of given markdown TEXTusagedisplays general script's help contained in section HELP_TITLE_PREFIX of given help file HELP_FILE, preceeded by the text contained in HELP_HEADERusage COMMANDdisplays script's help for COMMAND contained in section HELP_TITLE_PREFIX COMMAND of given help file HELP_FILE, preceeded by the text contained in HELP_HEADERusage COMMAND SUBCOMMANDdisplays script's help for COMMAND SUBCOMMAND contained in section HELP_TITLE_PREFIX COMMAND SUBCOMMAND of given help file HELP_FILE, preceeded by the text contained in HELP_HEADER
spinner COMMANDdisplays a spinner while executing COMMAND and returns its standard outputspinner COMMAND BEFOREdisplays a spinner while executing COMMAND and returns its standard output, and append result of command BEFORE before the spinnerspinner COMMAND BEFORE AFTERdisplays a spinner while executing COMMAND and returns its standard output, and append result of commands BEFORE and AFTER respectively before and after the spinnerspinner COMMAND BEFORE AFTER MODEdisplays a spinner while executing COMMAND and returns its standard output, and append result of commands BEFORE and AFTER respectively before and after the spinner, and force spinner display to given MODEspinner COMMAND BEFORE AFTER MODE SLEEPdisplays a spinner while executing COMMAND and returns its standard output, and append result of commands BEFORE and AFTER respectively before and after the spinner, and force spinner display to given MODE, and force spinner refresh rate to given SLEEP time between each framespinner_greenbehaves exactly likespinnerand accepts the same arguments, except it force the color of spinner to light green