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 utilities
isBoolean
Boolean test
Usage
isBoolean [ --help ] [ value ]
Boolean test
If you want "true-ish" use isTrue.
Returns 0 if value is boolean false oHar true.
Is this a boolean? (true or false)
Arguments
--help- Flag. Optional. Display this help.value- String. Optional. Value to check if it is a boolean.
Return codes
0- if value is a boolean1- if value is not a boolean
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
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 environment
- argument (2) - issue with arguments
- assert (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 found
- user-interrupt - (130) User interrupt (Ctrl-C)
- interrupt - (141) Interrupt signal
- internal - (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.
Arguments
name ...- String. Optional. Exit code value to output.
Return codes
0- success
returnCodeString
Output the exit code as a string
Usage
returnCodeString [ code ... ] [ --help ]
Output the exit code as a string
Arguments
code ...- UnsignedInteger. String. Exit code value to output.--help- Flag. Optional. Display this help.
Writes to standard output
exitCodeToken, one per line
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- Integer. Required. Exit code to return.item- Exists. Optional. One or more files or folders to delete, failures are logged to stderr.
Return codes
0- Success1- Environment error2- Argument error
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- Flag. Optional. Display this help.code- UnsignedInteger. Required. Exit code to return.undoFunction- Callable. 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
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)
Arguments
exitCode- UnsignedInteger. Required. Exit code to return. Default is 1.message ...- String. Optional. Message to output
Return codes
- exitCode
returnEnvironment
Return
environmenterror code. Outputsmessage ...tostderr.
Usage
returnEnvironment [ message ... ]
Return environment error code. Outputs message ... to stderr.
Arguments
message ...- String. Optional. Message to output.
Return codes
- 1
returnArgument
Return
argumenterror code. Outputsmessage ...tostderr.
Usage
returnArgument [ message ... ]
Return argument error code. Outputs message ... to stderr.
Arguments
message ...- String. Optional. Message to output.
Return codes
- 2
Run-related
execute
Run binary and output failed command upon error
Usage
execute [ --help ] binary [ ... ]
Run binary and output failed command upon error
Arguments
--help- Flag. Optional. Display this help.binary- Callable. Required. Command to run....- Arguments. Optional. Any arguments are passed tobinary.
Return codes
0- Success1- Environment error2- Argument error
executeEcho
Output the
command ...to stdout prior to running, then
Usage
executeEcho [ command ... ] [ --help ]
Output the command ... to stdout prior to running, then execute it
Arguments
command ...- Any command and arguments to run.--help- Flag. Optional. Display this help.
Return codes
- Any
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
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- Flag. Optional. Display this help.value- String. A value.from- String. When value matchesfrom, instead printtoto- String. The value to print whenfrommatchesvalue...- String. Optional. 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
Throw and Catch
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- UnsignedInteger. Required. Exit code to returnhandler- Function. Required. Failure command, passed remaining arguments and error code.command ...- Callable. Required. Command to run....- Arguments. Optional. Arguments tocommand
Return codes
0- Success1- Environment error2- Argument error
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- String. Required. Failure commandcommand ...- Callable. Required. Command to run....- Arguments. Optional. Arguments tocommand
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- String. Required. Failure commandcommand ...- Callable. Required. Command to run....- Arguments. Optional. Arguments tocommand
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- Function. Required. Error handler.message ...- String. Optional. Error message
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- Function. Required. Failure commandmessage ...- String. Optional. Error message to display.
Return codes
0- Success1- Environment error2- Argument error