Sugar Core
Sugar refers to syntactic sugar - code which makes other code more readable.
Most functions are used in the form suffixed with || return $? which takes the returned code and returns immediately
from the function.
returnMessage 1 "This failed" || return $?
catchArgument "$handler" isInteger "$1" || return $?
Alternately, these can be used within an if or other compound statement but the return code should be returned to the
user, typically.
Quick guide:
isPositiveInteger value- Returns 0 if value passed is an integer, otherwise returns 1.isBoolean value- Returns 0 if value passed istrueorfalse, otherwise returns 1.chooseBoolean testValue trueValue falseValue- OutputstrueValuewhen[ "$testValue" = "true" ]otherwise outputsfalseValue.
Error codes:
returnCode name ...- Exit codes. Outputs integers based on error names, one per line.returnCodeString integer ...- Exit strings. Reverse ofreturnCode.
Return errors:
returnMessage code message ...- Returncodeand outputsmessage ...tostderrIFFcodeis non-zero, otherwise outputmessage ...tostdout.returnEnvironment message ...- Return1always. Outputsmessage ...tostderr.returnArgument message ...- Return2always. Outputsmessage ...tostderr.
Run-related:
execute command ...- Runcommand ...(with any arguments) and then_returnif it fails.executeEcho command ...- Output thecommand ...to stdout prior to running, thenexecuteit (helpful to debug statements within other scripts)catchEnvironment handler command ...- Runcommand ...(with any arguments) and then runhandlerwith an environment error if it fails.
Sugar Functions References
Sugar utilities
isBoolean
Boolean test
Usage
isBoolean [ --help ] [ value ]
Boolean test
If you want "true-ish" use isTrue.
Returns 0 if value is boolean false or true.
Is this a boolean? (true or false)
Return Code: 0 - if value is a boolean
Return Code: 1 - if value is not a boolean
Arguments
--help- Optional. Flag. Display this help.value- Optional. String. Value to check if it is a boolean.
Return codes
0- Success1- Environment error2- Argument error
Requires
usageDocument printf
booleanChoose
Boolean selector
Usage
booleanChoose testValue [ trueChoice ] [ falseChoice ]
Boolean selector
Arguments
testValue- Boolean. Required. Test valuetrueChoice- EmptyString. Optional. Value to output when testValue istruefalseChoice- EmptyString. Optional. Value to output when testValue isfalse
Return codes
0- Success1- Environment error2- Argument error
Requires
isBoolean returnArgument printf
returnCode
Print one or more return codes by name.
Usage
returnCode [ name ... ]
Print one or more return codes by name.
Known codes:
success(0) - success!environment(1) - generic issue with environmentargument(2) - issue with argumentsassert(97) - assertion failed (ASCII 97 =a)identical(105) - identical check failed (ASCII 105 =i)leak(108) - function leaked globals (ASCII 108 =l)timeout(116) - timeout exceeded (ASCII 116 =t)exit- (120) exit function immediately (ASCII 120 =x)not-found- (127) command not founduser-interrupt- (127) User interrupt (Ctrl-C)interrupt- (141) Interrupt signalinternal- (253) internal errors
Unknown error code is 254, end of range is 255 which is not used. Use returnCodeString to get a string from an exit code integer.
Return Code: 0 - success
Arguments
name ...- Optional. String. Exit code value to output.
Return codes
0- Success1- Environment error2- Argument error
Requires
usageDocument printf
returnCodeString
Output the exit code as a string
Usage
returnCodeString [ code ... ] [ --help ]
Output the exit code as a string
Writes to standard output
exitCodeToken, one per line
Arguments
code ...- UnsignedInteger. String. Exit code value to output.--help- Optional. Flag. Display this help.
Return codes
0- Success1- Environment error2- Argument error
Cleanup
returnClean
Delete files or directories and return the same exit code
Usage
returnClean exitCode [ item ]
Delete files or directories and return the same exit code passed in.
Arguments
exitCode- Required. Integer. Exit code to return.item- Optional. One or more files or folders to delete, failures are logged to stderr.
Return codes
0- Success1- Environment error2- Argument error
Requires
isUnsignedInteger returnArgument throwEnvironment usageDocument throwArgument
returnUndo
Run a function and preserve exit code
Usage
returnUndo [ --help ] code [ undoFunction ] [ -- ]
Run a function and preserve exit code
Returns code
As a caveat, your command to undo can NOT take the argument -- as a parameter.
Arguments
--help- Optional. Flag. Display this help.code- Required. UnsignedInteger. Exit code to return.undoFunction- Optional. Command to run to undo something. Return status is ignored.--- Flag. Optional. Used to delimit multiple commands.
Examples
local undo thing
thing=$(catchEnvironment "$handler" createLargeResource) || return $?
undo+=(-- deleteLargeResource "$thing")
thing=$(catchEnvironment "$handler" createMassiveResource) || returnUndo $? "${undo[@]}" || return $?
undo+=(-- deleteMassiveResource "$thing")
Return codes
0- Success1- Environment error2- Argument error
Requires
isPositiveInteger catchArgument decorate execute usageDocument
Fail with an error code
returnMessage
Return passed in integer return code and output message to
Usage
returnMessage exitCode [ message ... ]
Return passed in integer return code and output message to stderr (non-zero) or stdout (zero)
Return Code: exitCode
Arguments
exitCode- Required. UnsignedInteger. Exit code to return. Default is 1.message ...- Optional. String. Message to output
Return codes
0- Success1- Environment error2- Argument error
Requires
isUnsignedInteger printf returnMessage
returnEnvironment
Return
environmenterror code. Outputsmessage ...tostderr.
Usage
returnEnvironment [ message ... ]
Return environment error code. Outputs message ... to stderr.
Return Code: 1
Arguments
message ...- String. Optional. Message to output.
Return codes
0- Success1- Environment error2- Argument error
Requires
returnMessage
returnArgument
Return
argumenterror code. Outputsmessage ...tostderr.
Usage
returnArgument [ message ... ]
Return argument error code. Outputs message ... to stderr.
Return Code: 2
Arguments
message ...- String. Optional. Message to output.
Return codes
0- Success1- Environment error2- Argument error
Requires
returnMessage
Run-related
execute
Run binary and output failed command upon error
Usage
execute binary ...
Run binary and output failed command upon error
Arguments
binary ...- Required. Executable. Any arguments are passed tobinary.
Return codes
0- Success1- Environment error2- Argument error
Requires
returnMessage
executeEcho
Output the
command ...to stdout prior to running, then
Usage
executeEcho command ...
Output the command ... to stdout prior to running, then execute it
Return Code: Any
Arguments
command ...- Any command and arguments to run.
Return codes
0- Success1- Environment error2- Argument error
Requires
printf decorate execute __decorateExtensionQuote __decorateExtensionEach
returnThrow
Run
handlerwith a passed return code
Usage
returnThrow returnCode handler [ message ... ]
Run handler with a passed return code
Arguments
returnCode- Integer. Required. Return code.handler- Function. Required. Error handler.message ...- String. Optional. Error message
Return codes
0- Success1- Environment error2- Argument error
Requires
returnArgument
convertValue
map a value from one value to another given from-to
Usage
convertValue [ --help ] [ value ] [ from ] [ to ] [ ... ]
map a value from one value to another given from-to pairs
Prints the mapped value to stdout
Arguments
--help- Optional. Flag. Display this help.value- String. A value.from- String. When value matchesfrom, instead printtoto- String. The value to print whenfrommatchesvalue...- Additional from-to pairs can be passed, first matching value is used, all values will be examined if none match
Return codes
0- Success1- Environment error2- Argument error
catchCode
Run
command, handle failure withhandlerwithcodeandcommand
Usage
catchCode code handler command ...
Run command, handle failure with handler with code and command as error
Arguments
code- Required. UnsignedInteger. Exit code to returnhandler- Required. Function. Failure command, passed remaining arguments and error code.command- Required. String. Command to run.
Return codes
0- Success1- Environment error2- Argument error
Requires
isUnsignedInteger returnArgument isFunction isCallable
catchEnvironment
Run
command, upon failure runhandlerwith an environment error
Usage
catchEnvironment handler command ...
Run command, upon failure run handler with an environment error
Arguments
handler- Required. Function. Failure commandcommand- Required. Command to run.
Return codes
0- Success1- Environment error2- Argument error
catchArgument
Run
command, upon failure runhandlerwith an argument error
Usage
catchArgument handler command
Run command, upon failure run handler with an argument error
Arguments
handler- Required. Function. Failure commandcommand- Required. Command to run.
Return codes
0- Success1- Environment error2- Argument error
throwEnvironment
Run
handlerwith an environment error
Usage
throwEnvironment handler [ message ]
Run handler with an environment error
Arguments
handler- Required. Function. Failure commandmessage- Optional. Error message to display.
Return codes
0- Success1- Environment error2- Argument error
throwArgument
Run
handlerwith an argument error
Usage
throwArgument handler [ message ]
Run handler with an argument error
Arguments
handler- Required. Function. Failure commandmessage- Optional. Error message to display.
Return codes
0- Success1- Environment error2- Argument error