2015-10-02 15:04:39 +00:00
API / Function Documentation
============================
This documentation is generated automatically from the source of [mo] thanks to [tomdoc.sh].
2020-08-05 20:44:04 +00:00
`mo()`
------
2015-10-02 15:04:39 +00:00
Public: Template parser function. Writes templates to stdout.
2020-08-05 20:44:04 +00:00
* $0 - Name of the mo file, used for getting the help message.
* $@ - Filenames to parse.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
Options:
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
--allow-function-arguments
2015-10-02 15:04:39 +00:00
2020-08-05 20:44:04 +00:00
Permit functions in templates to be called with additional arguments. This puts template data directly in to the path of an eval statement. Use with caution. Not listed in the help because it only makes sense when mo is sourced.
2015-10-02 15:04:39 +00:00
2020-08-05 20:44:04 +00:00
-u, --fail-not-set
2015-10-02 15:04:39 +00:00
2020-08-05 20:44:04 +00:00
Fail upon expansion of an unset variable. Default behavior is to silently ignore and expand into empty string.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
-x, --fail-on-function
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
Fail when a function used by a template returns an error status code. Alternately, ou may set the MO_FAIL_ON_FUNCTION environment variable to a non-empty value to enable this behavior.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
-e, --false
2017-06-20 19:19:44 +00:00
2020-08-05 20:44:04 +00:00
Treat "false" as an empty value. You may set the MO_FALSE_IS_EMPTY environment variable instead to a non-empty value to enable this behavior.
2017-06-20 19:19:44 +00:00
2020-08-05 20:44:04 +00:00
-h, --help
2017-06-20 19:19:44 +00:00
2020-08-05 20:44:04 +00:00
Display a help message.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
-s=FILE, --source=FILE
2016-08-03 15:19:07 +00:00
2020-10-01 12:42:57 +00:00
Source a file into the environment before processing template files. This can be used multiple times.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
--
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
Used to indicate the end of options. You may optionally use this when filenames may start with two hyphens.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
Mo uses the following environment variables:
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
* MO_ALLOW_FUNCTION_ARGUMENTS - When set to a non-empty value, this allows functions referenced in templates to receive additional options and arguments. This puts the content from the template directly into an eval statement. Use with extreme care.
* MO_FUNCTION_ARGS - Arguments passed to the function
* MO_FAIL_ON_FUNCTION - If a function returns a non-zero status code, abort with an error.
* MO_FAIL_ON_UNSET - When set to a non-empty value, expansion of an unset env variable will be aborted with an error.
* MO_FALSE_IS_EMPTY - When set to a non-empty value, the string "false" will be treated as an empty value for the purposes of conditionals.
* MO_ORIGINAL_COMMAND - Used to find the `mo` program in order to generate a help message.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
Returns nothing.
2016-08-03 15:19:07 +00:00
2020-08-05 20:44:04 +00:00
`moCallFunction()`
------------------
Internal: Call a function.
* $1 - Variable for output
* $2 - Function to call
* $3 - Content to pass
* $4 - Additional arguments as a single string
This can be dangerous, especially if you are using tags like {{someFunction ; rm -rf / }}
Returns nothing.
`moFindEndTag()`
----------------
2015-10-02 15:04:39 +00:00
Internal: Scan content until the right end tag is found. Creates an array with the following members:
[0] = Content before end tag
[1] = End tag (complete tag)
[2] = Content after end tag
Everything using this function uses the "standalone tags" logic.
* $1 - Name of variable for the array
* $2 - Content
* $3 - Name of end tag
* $4 - If -z, do standalone tag processing before finishing
Returns nothing.
2020-08-05 20:44:04 +00:00
`moFindString()`
----------------
2015-10-02 15:04:39 +00:00
Internal: Find the first index of a substring. If not found, sets the index to -1.
* $1 - Destination variable for the index
* $2 - Haystack
* $3 - Needle
Returns nothing.
2020-08-05 20:44:04 +00:00
`moFullTagName()`
-----------------
2015-10-02 15:04:39 +00:00
Internal: Generate a dotted name based on current context and target name.
* $1 - Target variable to store results
* $2 - Context name
* $3 - Desired variable name
Returns nothing.
2020-08-05 20:44:04 +00:00
`moGetContent()`
----------------
2015-10-02 15:04:39 +00:00
Internal: Fetches the content to parse into a variable. Can be a list of partials for files or the content from stdin.
* $1 - Variable name to assign this content back as
2015-10-07 20:58:45 +00:00
* $2-@ - File names (optional)
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moIndentLines()`
-----------------
2015-10-02 15:04:39 +00:00
Internal: Indent a string, placing the indent at the beginning of every line that has any content.
* $1 - Name of destination variable to get an array of lines
* $2 - The indent string
* $3 - The string to reindent
Returns nothing.
2020-08-05 20:44:04 +00:00
`moIndirect()`
--------------
2015-10-02 15:04:39 +00:00
Internal: Send a variable up to the parent of the caller of this function.
* $1 - Variable name
* $2 - Value
Examples
callFunc () {
local "$1" & & moIndirect "$1" "the value"
}
2015-10-02 15:47:53 +00:00
callFunc dest
echo "$dest" # writes "the value"
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moIndirectArray()`
-------------------
2015-10-02 15:04:39 +00:00
Internal: Send an array as a variable up to caller of a function
* $1 - Variable name
2015-10-07 20:58:45 +00:00
* $2-@ - Array elements
2015-10-02 15:04:39 +00:00
Examples
callFunc () {
local myArray=(one two three)
local "$1" & & moIndirectArray "$1" "${myArray[@]}"
}
2015-10-02 15:47:53 +00:00
callFunc dest
echo "${dest[@]}" # writes "one two three"
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moIsArray()`
-------------
2015-10-02 15:04:39 +00:00
Internal: Determine if a given environment variable exists and if it is an array.
* $1 - Name of environment variable
2017-06-20 19:19:44 +00:00
Be extremely careful. Even if strict mode is enabled, it is not honored in newer versions of Bash. Any errors that crop up here will not be caught automatically.
2015-10-02 15:04:39 +00:00
Examples
var=(abc)
2020-08-05 20:44:04 +00:00
if moIsArray var; then
2015-10-02 15:04:39 +00:00
echo "This is an array"
2020-08-05 20:44:04 +00:00
echo "Make sure you don't accidentally use \$var"
2015-10-02 15:04:39 +00:00
fi
Returns 0 if the name is not empty, 1 otherwise.
2020-08-05 20:44:04 +00:00
`moIsFunction()`
----------------
2015-10-02 15:04:39 +00:00
Internal: Determine if the given name is a defined function.
* $1 - Function name to check
2017-06-20 19:19:44 +00:00
Be extremely careful. Even if strict mode is enabled, it is not honored in newer versions of Bash. Any errors that crop up here will not be caught automatically.
2015-10-02 15:04:39 +00:00
Examples
moo () {
echo "This is a function"
}
if moIsFunction moo; then
echo "moo is a defined function"
fi
Returns 0 if the name is a function, 1 otherwise.
2020-08-05 20:44:04 +00:00
`moIsStandalone()`
------------------
2015-10-02 15:04:39 +00:00
Internal: Determine if the tag is a standalone tag based on whitespace before and after the tag.
Passes back a string containing two numbers in the format "BEFORE AFTER" like "27 10". It indicates the number of bytes remaining in the "before" string (27) and the number of bytes to trim in the "after" string (10). Useful for string manipulation:
* $1 - Variable to set for passing data back
* $2 - Content before the tag
* $3 - Content after the tag
* $4 - true/false: is this the beginning of the content?
Examples
moIsStandalone RESULT "$before" "$after" false || return 0
RESULT_ARRAY=( $RESULT )
echo "${before:0:${RESULT_ARRAY[0]}}...${after:${RESULT_ARRAY[1]}}"
Returns nothing.
2020-08-05 20:44:04 +00:00
`moJoin()`
----------
2015-10-02 15:04:39 +00:00
Internal: Join / implode an array
* $1 - Variable name to receive the joined content
* $2 - Joiner
* $3-$* - Elements to join
Returns nothing.
2020-08-05 20:44:04 +00:00
`moLoadFile()`
--------------
2015-10-02 15:04:39 +00:00
Internal: Read a file into a variable.
* $1 - Variable name to receive the file's content
2020-08-05 20:44:04 +00:00
* $2 - Filename to load - if empty, defaults to /dev/stdin
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moLoop()`
----------
2015-10-02 15:04:39 +00:00
Internal: Process a chunk of content some number of times. Writes output to stdout.
* $1 - Content to parse repeatedly
* $2 - Tag prefix (context name)
2015-10-07 20:58:45 +00:00
* $3-@ - Names to insert into the parsed content
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moParse()`
-----------
2015-10-02 15:04:39 +00:00
Internal: Parse a block of text, writing the result to stdout.
* $1 - Block of text to change
* $2 - Current name (the variable NAME for what {{.}} means)
* $3 - true when no content before this, false otherwise
Returns nothing.
2020-08-05 20:44:04 +00:00
`moArgs`
--------
Split arguments from the tag name. Arguments are passed to functions.
`moPartial()`
-------------
2015-10-02 15:04:39 +00:00
Internal: Process a partial.
2017-06-20 19:19:44 +00:00
Indentation should be applied to the entire partial.
This sends back the "is beginning" flag because the newline after a standalone partial is consumed. That newline is very important in the middle of content. We send back this flag to reset the processing loop's `moIsBeginning` variable, so the software thinks we are back at the beginning of a file and standalone processing continues to work.
2015-10-02 15:04:39 +00:00
Prefix all variables.
2017-06-20 19:19:44 +00:00
* $1 - Name of destination variable. Element [0] is the content, [1] is the true/false flag indicating if we are at the beginning of content.
2015-10-02 15:04:39 +00:00
* $2 - Content before the tag that was not yet written
* $3 - Tag content
* $4 - Content after the tag
* $5 - true/false: is this the beginning of the content?
* $6 - Current context name
Returns nothing.
2020-08-05 20:44:04 +00:00
`moShow()`
----------
2015-10-02 15:04:39 +00:00
Internal: Show an environment variable or the output of a function to stdout.
Limit/prefix any variables used.
* $1 - Name of environment variable or function
* $2 - Current context
2020-08-05 20:44:04 +00:00
* $3 - Arguments string if $1 is a function
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moSplit()`
-----------
2015-10-02 15:04:39 +00:00
Internal: Split a larger string into an array.
* $1 - Destination variable
* $2 - String to split
* $3 - Starting delimiter
* $4 - Ending delimiter (optional)
Returns nothing.
2020-08-05 20:44:04 +00:00
`moStandaloneAllowed()`
-----------------------
2015-10-02 15:04:39 +00:00
Internal: Handle the content for a standalone tag. This means removing whitespace (not newlines) before a tag and whitespace and a newline after a tag. That is, assuming, that the line is otherwise empty.
* $1 - Name of destination "content" variable.
* $2 - Content before the tag that was not yet written
* $3 - Tag content (not used)
* $4 - Content after the tag
* $5 - true/false: is this the beginning of the content?
Returns nothing.
2020-08-05 20:44:04 +00:00
`moStandaloneDenied()`
----------------------
2015-10-02 15:04:39 +00:00
Internal: Handle the content for a tag that is never "standalone". No adjustments are made for newlines and whitespace.
* $1 - Name of destination "content" variable.
* $2 - Content before the tag that was not yet written
* $3 - Tag content (not used)
* $4 - Content after the tag
Returns nothing.
2020-08-05 20:44:04 +00:00
`moTest()`
----------
2015-10-02 15:04:39 +00:00
2016-08-03 15:19:07 +00:00
Internal: Determines if the named thing is a function or if it is a non-empty environment variable. When MO_FALSE_IS_EMPTY is set to a non-empty value, then "false" is also treated is an empty value.
2015-10-02 15:04:39 +00:00
Do not use variables without prefixes here if possible as this needs to check if any name exists in the environment
2016-08-03 15:19:07 +00:00
* $1 - Name of environment variable or function
* $2 - Current value (our context)
* MO_FALSE_IS_EMPTY - When set to a non-empty value, this will say the string value "false" is empty.
2015-10-02 15:04:39 +00:00
2016-08-03 15:19:07 +00:00
Returns 0 if the name is not empty, 1 otherwise. When MO_FALSE_IS_EMPTY is set, this returns 1 if the name is "false".
2015-10-02 15:04:39 +00:00
2020-08-05 20:44:04 +00:00
`moTestVarSet()`
----------------
2017-06-20 19:19:44 +00:00
Internal: Determine if a variable is assigned, even if it is assigned an empty value.
* $1 - Variable name to check.
Returns true (0) if the variable is set, 1 if the variable is unset.
2020-08-05 20:44:04 +00:00
`moTrimChars()`
---------------
2015-10-02 15:04:39 +00:00
Internal: Trim the leading whitespace only.
* $1 - Name of destination variable
* $2 - The string
* $3 - true/false - trim front?
* $4 - true/false - trim end?
2015-10-07 20:58:45 +00:00
* $5-@ - Characters to trim
2015-10-02 15:04:39 +00:00
Returns nothing.
2020-08-05 20:44:04 +00:00
`moTrimWhitespace()`
--------------------
2015-10-02 15:04:39 +00:00
Internal: Trim leading and trailing whitespace from a string.
* $1 - Name of variable to store trimmed string
* $2 - The string
Returns nothing.
2020-08-05 20:44:04 +00:00
`moUsage()`
-----------
2015-10-02 15:04:39 +00:00
Internal: Displays the usage for mo. Pulls this from the file that contained the `mo` function. Can only work when the right filename comes is the one argument, and that only happens when `mo` is called with `$0` set to this file.
* $1 - Filename that has the help message
Returns nothing.
2020-08-05 20:44:04 +00:00
`MO_ORIGINAL_COMMAND`
---------------------
2016-08-03 15:19:07 +00:00
Save the original command's path for usage later
2015-10-02 15:04:39 +00:00
[mo]: ./mo
2020-08-05 20:44:04 +00:00
[tomdoc.sh]: https://github.com/tests-always-included/tomdoc.sh