Input - nsrosenqvist/quickscript GitHub Wiki

Input

ask()

ask() 

Prints the question specified and lets the user answer yes or no

Parameter Explanation
$1 (string) The question proposed

return (int) - Returns 0 if the user answered "yes" and "1" if no

author: Niklas Rosenqvist


qs_opts()

qs_opts() 

A better featured alternative to traditional getopts

It's almost a complete drop-in replacement for getopts. OPTARG and OPTIND gets set but the returned variable gets set with the hyphen remaining, e.g. "-v" instead "v". Options are specified in the same way but the preceding colon doesn't alter the output. A subsequent colon however still specifies that the option requires a set value.

Variables that requires set values can have the value specified in two ways: either as a subsequent argument or by using an equal sign, -p "/path", -p="/path"

Global variables that get set:

  • PIPE_ARGS - An array containing all arguments that has been piped to the script
  • FILE_ARGS - An array containing all arguments that has been redirected from file
  • TERM_ARGS - An array containing all arguments that was entered from the terminal
  • NONOPT - An array with the arguments that weren't considered options ($1, $2, etc.)
  • OPTORIG - An array with the original terminal input
  • OPTALIAS - An assocative array that lets you specify aliases to options and option groups. Even though qs_opts supports long arguments such as --path, the aliases must point to the short versions of the options since long options get matched by checking the first letter: --path = -p. So two examples would be: OPTALIAS[--VERBOSE]=-v and OPTALIAS[--ALL]=-vnp
  • INPUT_PROCESSED - A boolean indicating if the input has been processed or not, set to 0 too be able to run qs_opts again.
  • INPUT_PIPE - Boolean indicating if we received input from pipe.
  • INPUT_FILE - Boolean indicating if we received input redirected from file.
  • INPUT_TERM - Boolean indicating if we received input from terminal.
  • OPTUPDATECMD - If you don't want to limit what position the user places arguments and options, in the way that getopts is limited to that all options must be placed before the arguments, you run this saved command to reset $1, $2, $3, etc. appropriately: eval "$OPTUPDATECMD"
Parameter Explanation
$1 (string) The options that are available: e.g. "vnp:Ot:"
$2 (string) The variable that gets set with the return value
(any) $3 Optionally you can add as many arguments to process as you want which can be useful if using qs_opts within functions. Then you can pass $* since qs_opts otherwise processes the originally supplied arguments by getting them through ${BASH_ARGV}

return (string) - Either the matched short version of an option or "?" if the option don't exist or "!" if the option requires a value and none was provided.

author: Niklas Rosenqvist


⚠️ **GitHub.com Fallback** ⚠️