diff --git a/mo b/mo index c12254a..8bbf038 100755 --- a/mo +++ b/mo @@ -8,6 +8,14 @@ #/ conditionally display content or iterate over the values of an array. #/ #/ Learn more about mustache templates at https://mustache.github.io/ +#/ +#/ Simple usage: +#/ +#/ mo [--false] [--help] [--source=FILE] filenames... +#/ +#/ --false - Treat the string "false" as empty for conditionals. +#/ --help - This message. +#/ --source=FILE - Load FILE into the environment before processing templates. # # Mo is under a MIT style licence with an additional non-advertising clause. # See LICENSE.md for the full text. @@ -20,6 +28,9 @@ # Public: Template parser function. Writes templates to stdout. # # $0 - Name of the mo file, used for getting the help message. +# --false - 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. # --help - Display a help message. # --source=FILE - Source a file into the environment before processint # template files. @@ -27,6 +38,14 @@ # use this when filenames may start with two hyphens. # $@ - Filenames to parse. # +# Mo uses the following environment variables: +# +# 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. +# # Returns nothing. mo() ( # This function executes in a subshell so IFS is reset. @@ -45,11 +64,15 @@ mo() ( files=("${files[@]}" "$arg") else case "$arg" in - -h|--h|--he|--hel|--help) + -h|--h|--he|--hel|--help|-\?) moUsage "$0" exit 0 ;; + --false) + MO_FALSE_IS_EMPTY=true + ;; + --source=*) f2source="${1#--source=}" @@ -775,15 +798,19 @@ moStandaloneDenied() { # Internal: Determines if the named thing is a function or if it is a -# non-empty environment variable. +# 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. # # Do not use variables without prefixes here if possible as this needs to # check if any name exists in the environment # -# $1 - Name of environment variable or function -# $2 - Current value (our context) +# $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. # -# Returns 0 if the name is not empty, 1 otherwise. +# 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". moTest() { # Test for functions moIsFunction "$1" && return 0 @@ -792,6 +819,10 @@ moTest() { # Arrays must have at least 1 element eval '[[ "${#'"$1"'[@]}" -gt 0 ]]' && return 0 else + # If MO_FALSE_IS_EMPTY is set, then return 1 if the value of + # the variable is "false". + [[ ! -z "${MO_FALSE_IS_EMPTY-}" ]] && [[ "${!1-}" == "false" ]] && return 1 + # Environment variables must not be empty [[ ! -z "${!1}" ]] && return 0 fi @@ -855,10 +886,13 @@ moTrimWhitespace() { # # Returns nothing. moUsage() { - grep '^#/' "$1" | cut -c 4- + grep '^#/' "${MO_ORIGINAL_COMMAND}" | cut -c 4- } +# Save the original command's path for usage later +MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE%/*}"; pwd)/${BASH_SOURCE##*/}" + # If sourced, load all functions. # If executed, perform the actions as expected. if [[ "$0" == "$BASH_SOURCE" ]] || ! [[ -n "$BASH_SOURCE" ]]; then diff --git a/run-tests b/run-tests index 69df382..25f43ef 100755 --- a/run-tests +++ b/run-tests @@ -8,6 +8,7 @@ FAIL=0 for TEST in tests/*.expected; do BASE="${TEST%.expected}" + MO_FALSE_IS_EMPTY= echo -n "$BASE ... " diff --git a/tests/false-is-empty-arg.expected b/tests/false-is-empty-arg.expected new file mode 100644 index 0000000..ddf6d4e --- /dev/null +++ b/tests/false-is-empty-arg.expected @@ -0,0 +1 @@ +The user j.doe exists. diff --git a/tests/false-is-empty-arg.sh b/tests/false-is-empty-arg.sh new file mode 100755 index 0000000..94d8365 --- /dev/null +++ b/tests/false-is-empty-arg.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +cd "${0%/*}" +USER=j.doe ADMIN=false ../mo --false false-is-empty-arg.template diff --git a/tests/false-is-empty-arg.template b/tests/false-is-empty-arg.template new file mode 100644 index 0000000..7d9e2b2 --- /dev/null +++ b/tests/false-is-empty-arg.template @@ -0,0 +1,4 @@ +The user {{USER}} exists. +{{#ADMIN}} +WRONG - should not be an admin. +{{/ADMIN}} diff --git a/tests/false-is-empty-env.env b/tests/false-is-empty-env.env new file mode 100644 index 0000000..90d226d --- /dev/null +++ b/tests/false-is-empty-env.env @@ -0,0 +1,2 @@ +MO_FALSE_IS_EMPTY=yeppers +someFalseValue=false diff --git a/tests/false-is-empty-env.expected b/tests/false-is-empty-env.expected new file mode 100644 index 0000000..3bbe354 --- /dev/null +++ b/tests/false-is-empty-env.expected @@ -0,0 +1 @@ +Works diff --git a/tests/false-is-empty-env.template b/tests/false-is-empty-env.template new file mode 100644 index 0000000..4fdd83f --- /dev/null +++ b/tests/false-is-empty-env.template @@ -0,0 +1,4 @@ +Works +{{#someFalseValue}} + Never shown! +{{/someFalseValue}} diff --git a/tests/help.expected b/tests/help.expected index c0cfa7f..95e8df5 100644 --- a/tests/help.expected +++ b/tests/help.expected @@ -6,3 +6,11 @@ environment variable. You can use {{#VARIABLE}}content{{/VARIABLE}} to conditionally display content or iterate over the values of an array. Learn more about mustache templates at https://mustache.github.io/ + +Simple usage: + + mo [--false] [--help] [--source=FILE] filenames... + +--false - Treat the string "false" as empty for conditionals. +--help - This message. +--source=FILE - Load FILE into the environment before processing templates.