mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 16:27:52 +00:00
Handling double-hyphens
This commit is contained in:
parent
b69406835f
commit
ea76dc468b
68
mo
68
mo
@ -19,45 +19,59 @@
|
|||||||
|
|
||||||
# Public: Template parser function. Writes templates to stdout.
|
# Public: Template parser function. Writes templates to stdout.
|
||||||
#
|
#
|
||||||
# $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
|
# --help - Display a help message.
|
||||||
# in order to show a help message. Additionaly, --source=file
|
# --source=FILE - Source a file into the environment before processint
|
||||||
# can be passed so that mo sources the file before parsing
|
# template files.
|
||||||
# the filenames
|
# -- - Used to indicate the end of options. You may optionally
|
||||||
|
# use this when filenames may start with two hyphens.
|
||||||
|
# $@ - Filenames to parse.
|
||||||
#
|
#
|
||||||
# Returns nothing.
|
# Returns nothing.
|
||||||
mo() (
|
mo() (
|
||||||
# This function executes in a subshell so IFS is reset.
|
# This function executes in a subshell so IFS is reset.
|
||||||
# Namespace this variable so we don't conflict with desired values.
|
# Namespace this variable so we don't conflict with desired values.
|
||||||
local moContent f2source files
|
local moContent f2source files doubleHyphens
|
||||||
|
|
||||||
IFS=$' \n\t'
|
IFS=$' \n\t'
|
||||||
files=()
|
files=()
|
||||||
|
doubleHyphens=false
|
||||||
|
|
||||||
if [[ $# -gt 0 ]]; then
|
if [[ $# -gt 0 ]]; then
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
case "$arg" in
|
if $doubleHyphens; then
|
||||||
-h|--h|--he|--hel|--help)
|
# After we encounter two hyphens together, all the rest
|
||||||
moUsage "$0"
|
# of the arguments are files.
|
||||||
exit 0
|
files=("${files[@]}" "$arg")
|
||||||
;;
|
else
|
||||||
|
case "$arg" in
|
||||||
|
-h|--h|--he|--hel|--help)
|
||||||
|
moUsage "$0"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
|
||||||
--source=*)
|
--source=*)
|
||||||
f2source="${1#--source=}"
|
f2source="${1#--source=}"
|
||||||
|
|
||||||
if [[ -f "$f2source" ]]; then
|
if [[ -f "$f2source" ]]; then
|
||||||
. "$f2source"
|
. "$f2source"
|
||||||
else
|
else
|
||||||
echo "No such file: $f2source" >&2
|
echo "No such file: $f2source" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
--)
|
||||||
# Every arg that is not a flag or a option should be a file
|
# Set a flag indicating we've encountered double hyphens
|
||||||
files=("${files[@]}" "$arg")
|
doubleHyphens=true
|
||||||
;;
|
;;
|
||||||
esac
|
|
||||||
|
*)
|
||||||
|
# Every arg that is not a flag or a option should be a file
|
||||||
|
files=("${files[@]}" "$arg")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -442,7 +456,7 @@ moLoadFile() {
|
|||||||
# a dot to the content to preserve all newlines.
|
# a dot to the content to preserve all newlines.
|
||||||
# TODO: remove cat and replace with read loop?
|
# TODO: remove cat and replace with read loop?
|
||||||
|
|
||||||
content=$(cat $2; echo '.')
|
content=$(cat -- $2; echo '.')
|
||||||
len=$((${#content} - 1))
|
len=$((${#content} - 1))
|
||||||
content=${content:0:$len} # Remove last dot
|
content=${content:0:$len} # Remove last dot
|
||||||
|
|
||||||
@ -636,7 +650,7 @@ moPartial() {
|
|||||||
# Execute in subshell to preserve current cwd and environment
|
# Execute in subshell to preserve current cwd and environment
|
||||||
(
|
(
|
||||||
# TODO: Remove dirname and use a function instead
|
# TODO: Remove dirname and use a function instead
|
||||||
cd "$(dirname "$moFilename")"
|
cd "$(dirname -- "$moFilename")"
|
||||||
moIndentLines moPartial "$moIndent" "$(
|
moIndentLines moPartial "$moIndent" "$(
|
||||||
moLoadFile moPartial "${moFilename##*/}"
|
moLoadFile moPartial "${moFilename##*/}"
|
||||||
|
|
||||||
|
1
tests/double-hyphen.expected
Normal file
1
tests/double-hyphen.expected
Normal file
@ -0,0 +1 @@
|
|||||||
|
cat: --help: No such file or directory
|
5
tests/double-hyphen.sh
Executable file
5
tests/double-hyphen.sh
Executable 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
|
1
tests/invalid-option.expected
Normal file
1
tests/invalid-option.expected
Normal file
@ -0,0 +1 @@
|
|||||||
|
cat: --something: No such file or directory
|
5
tests/invalid-option.sh
Executable file
5
tests/invalid-option.sh
Executable 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
|
Loading…
Reference in New Issue
Block a user