mo can now source a script before parsing templates

This commit is contained in:
Alexandre-Silva 2016-07-21 12:07:02 +01:00
parent a1e22f1814
commit f9aea51163
3 changed files with 28 additions and 2 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ diagnostic.test
tests/*.diff tests/*.diff
spec/ spec/
spec-runner/ spec-runner/
demo/sourcing.vars

18
demo/sourcing Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
#
# This sources a simple script with the env. variables needed for the template.
cd "$(dirname "$0")" # Go to the script's directory
cat <<EOF >sourcing.vars
export NAME="Alex"
export ARRAY=( AAA BBB CCC )
EOF
cat <<EOF | ../mo --source=sourcing.vars
Hello, my name is {{NAME}}.
And this is ARRAY's conntents:
{{#ARRAY}}
* {{.}}
{{/ARRAY}}
EOF

11
mo
View File

@ -21,7 +21,9 @@
# #
# $0 - Name of the mo file, used for getting the help message. # $0 - Name of the mo file, used for getting the help message.
# $* - Filenames to parse. Can use -h or --help as the only option # $* - Filenames to parse. Can use -h or --help as the only option
# in order to show a help message. # in order to show a help message. Additionaly, --source=file
# can be passed so that mo sources the file before parsing
# the filenames.
# #
# Returns nothing. # Returns nothing.
mo() ( mo() (
@ -30,13 +32,18 @@ mo() (
local moContent local moContent
IFS=$' \n\t' IFS=$' \n\t'
if [[ $# -gt 0 ]]; then if [[ $# -gt 0 ]]; then
case "$1" in case "$1" in
-h|--h|--he|--hel|--help) -h|--h|--he|--hel|--help)
moUsage "$0" moUsage "$0"
exit 0 exit 0
;; ;;
--source=*)
. "${1#--source=}"
shift 1
;;
esac esac
fi fi