Handling double-hyphens

This commit is contained in:
Tyler Akins 2016-07-21 09:45:31 -05:00
parent b69406835f
commit ea76dc468b
5 changed files with 53 additions and 27 deletions

28
mo
View File

@ -20,22 +20,30 @@
# Public: Template parser function. Writes templates to stdout.
#
# $0 - Name of the mo file, used for getting the help message.
# $* - Filenames to parse. Can use -h or --help as the only option
# in order to show a help message. Additionaly, --source=file
# can be passed so that mo sources the file before parsing
# the filenames
# --help - Display a help message.
# --source=FILE - Source a file into the environment before processint
# template files.
# -- - Used to indicate the end of options. You may optionally
# use this when filenames may start with two hyphens.
# $@ - Filenames to parse.
#
# Returns nothing.
mo() (
# This function executes in a subshell so IFS is reset.
# Namespace this variable so we don't conflict with desired values.
local moContent f2source files
local moContent f2source files doubleHyphens
IFS=$' \n\t'
files=()
doubleHyphens=false
if [[ $# -gt 0 ]]; then
for arg in "$@"; do
if $doubleHyphens; then
# After we encounter two hyphens together, all the rest
# of the arguments are files.
files=("${files[@]}" "$arg")
else
case "$arg" in
-h|--h|--he|--hel|--help)
moUsage "$0"
@ -53,11 +61,17 @@ mo() (
fi
;;
--)
# Set a flag indicating we've encountered double hyphens
doubleHyphens=true
;;
*)
# Every arg that is not a flag or a option should be a file
files=("${files[@]}" "$arg")
;;
esac
fi
done
fi
@ -442,7 +456,7 @@ moLoadFile() {
# a dot to the content to preserve all newlines.
# TODO: remove cat and replace with read loop?
content=$(cat $2; echo '.')
content=$(cat -- $2; echo '.')
len=$((${#content} - 1))
content=${content:0:$len} # Remove last dot
@ -636,7 +650,7 @@ moPartial() {
# Execute in subshell to preserve current cwd and environment
(
# TODO: Remove dirname and use a function instead
cd "$(dirname "$moFilename")"
cd "$(dirname -- "$moFilename")"
moIndentLines moPartial "$moIndent" "$(
moLoadFile moPartial "${moFilename##*/}"

View File

@ -0,0 +1 @@
cat: --help: No such file or directory

5
tests/double-hyphen.sh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
# This should display a message indicating that the file --help
# could not be found. It should not display a help messsage.
cd "${0%/*}"
../mo -- --help 2>&1

View File

@ -0,0 +1 @@
cat: --something: No such file or directory

5
tests/invalid-option.sh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env bash
# This should display a message indicating that the file --something
# could not be found.
cd "${0%/*}"
../mo --something 2>&1