Skip to content

Commit

Permalink
Refactored argbash-init, fixed omitted defaults of bool options.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejak committed Dec 6, 2017
1 parent 98e89ff commit 4004d64
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 73 deletions.
3 changes: 2 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

Bugfixes:

* Fixed argbash-init corner cases (wrong output filename, ).
* Fixed argbash-init corner cases (wrong output filename).
* Corrected argbash-init hint mode.
* Allowed argbash to wrap scripts in files with no extension.
* Fixed compatibility with the `-e` (i.e. "strict") mode (fixed #30).

Expand Down
71 changes: 46 additions & 25 deletions bin/argbash-init
Original file line number Diff line number Diff line change
Expand Up @@ -203,44 +203,60 @@ _translit_var()
}


do_hints_pos()
optional_argument_without_hints()
{
_help="[<$1's help message goes here>]"
test "$_arg_hints" = on && _default="[<$1's default goes here (optional)>]"
echo "# ARG_OPTIONAL_SINGLE([$1])"
}


do_hits_opt()
optional_argument_with_hints()
{
do_hints_pos "$1"
if test "$_arg_hints" = on
then
_short_opt="[<short option character goes here (optional)>]"
fi
echo "# ARG_OPTIONAL_SINGLE([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional)>])"
}


optional_argument()
{
"${FUNCNAME}_$2" "$1"
_variables+=("printf 'Value of --%s: %s\n' '$1' \"$(_translit_var "$1")\"")
}


boolean_argument_with_hints()
{
echo "# ARG_OPTIONAL_BOOLEAN([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional), off by default>])"
}


do_opt()
boolean_argument_without_hints()
{
do_hits_opt "$1"
echo "# ARG_OPTIONAL_SINGLE([$1], $_short_opt, $_help)"
_variables+=('echo "Value of --'$1': '$(_translit_var "$1")'"')
echo "# ARG_OPTIONAL_BOOLEAN([$1])"
}


do_opt_bool()
boolean_argument()
{
do_hits_opt "$1"
echo "# ARG_OPTIONAL_BOOLEAN([$1], $_short_opt, $_help)"
_variables+=('echo "'$1' is '$(_translit_var "$1")'"')
"${FUNCNAME}_$2" "$1"
_variables+=("printf \"'%s' is %s\n\" '$1' \"$(_translit_var "$1")\"")
}


do_pos()
positional_argument_with_hints()
{
do_hints_pos "$1"
echo "# ARG_POSITIONAL_SINGLE([$1], $_help, $_default)"
_variables+=('echo "Value of '$1': '$(_translit_var "$1")'"')
echo "# ARG_POSITIONAL_SINGLE([$1], [<help message (optional)>], [<default (optional)])"
}


positional_argument_without_hints()
{
echo "# ARG_POSITIONAL_SINGLE([$1])"
}


positional_argument()
{
"${FUNCNAME}_$2" "$1"
_variables+=("printf \"Value of '%s': %s\n\" '$1' \"$(_translit_var "$1")\"")
}


Expand All @@ -263,13 +279,18 @@ do_header()

do_args()
{
test "$_arg_hints" = on && echo "# Rearrange the order of options below according to what you would like to see in the help message."
local _mode="without_hints"
if test "$_arg_hints" = on
then
echo "# Rearrange the order of options below according to what you would like to see in the help message."
_mode="with_hints"
fi
for name in "${_arg_opt[@]}"
do do_opt "$name"; done
do optional_argument "$name" "$_mode"; done
for name in "${_arg_opt_bool[@]}"
do do_opt_bool "$name"; done
do boolean_argument "$name" "$_mode"; done
for name in "${_arg_pos[@]}"
do do_pos "$name"; done
do positional_argument "$name" "$_mode"; done
}


Expand Down
71 changes: 46 additions & 25 deletions src/argbash-init.m4
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,60 @@ _translit_var()
}


do_hints_pos()
optional_argument_without_hints()
{
_help="[<$1's help message goes here>]"
test "$_arg_hints" = on && _default="[<$1's default goes here (optional)>]"
echo "# ARG_OPTIONAL_SINGLE([$1])"
}


do_hits_opt()
optional_argument_with_hints()
{
do_hints_pos "$1"
if test "$_arg_hints" = on
then
_short_opt="[<short option character goes here (optional)>]"
fi
echo "# ARG_OPTIONAL_SINGLE([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional)>])"
}


optional_argument()
{
"${FUNCNAME}_$2" "$1"
_variables+=("printf 'Value of --%s: %s\n' '$1' \"$(_translit_var "$1")\"")
}


boolean_argument_with_hints()
{
echo "# ARG_OPTIONAL_BOOLEAN([$1], [<short option character (optional)>], [<help message (optional)>], [<default (optional), off by default>])"
}


do_opt()
boolean_argument_without_hints()
{
do_hits_opt "$1"
echo "# ARG_OPTIONAL_SINGLE([$1], $_short_opt, $_help)"
_variables+=('echo "Value of --'$1': '$(_translit_var "$1")'"')
echo "# ARG_OPTIONAL_BOOLEAN([$1])"
}


do_opt_bool()
boolean_argument()
{
do_hits_opt "$1"
echo "# ARG_OPTIONAL_BOOLEAN([$1], $_short_opt, $_help)"
_variables+=('echo "'$1' is '$(_translit_var "$1")'"')
"${FUNCNAME}_$2" "$1"
_variables+=("printf \"'%s' is %s\n\" '$1' \"$(_translit_var "$1")\"")
}


do_pos()
positional_argument_with_hints()
{
do_hints_pos "$1"
echo "# ARG_POSITIONAL_SINGLE([$1], $_help, $_default)"
_variables+=('echo "Value of '$1': '$(_translit_var "$1")'"')
echo "# ARG_POSITIONAL_SINGLE([$1], [<help message (optional)>], [<default (optional)])"
}


positional_argument_without_hints()
{
echo "# ARG_POSITIONAL_SINGLE([$1])"
}


positional_argument()
{
"${FUNCNAME}_$2" "$1"
_variables+=("printf \"Value of '%s': %s\n\" '$1' \"$(_translit_var "$1")\"")
}


Expand All @@ -86,13 +102,18 @@ do_header()

do_args()
{
test "$_arg_hints" = on && echo "# Rearrange the order of options below according to what you would like to see in the help message."
local _mode="without_hints"
if test "$_arg_hints" = on
then
echo "# Rearrange the order of options below according to what you would like to see in the help message."
_mode="with_hints"
fi
for name in "${_arg_opt[@]}"
do do_opt "$name"; done
do optional_argument "$name" "$_mode"; done
for name in "${_arg_opt_bool[@]}"
do do_opt_bool "$name"; done
do boolean_argument "$name" "$_mode"; done
for name in "${_arg_pos[@]}"
do do_pos "$name"; done
do positional_argument "$name" "$_mode"; done
}


Expand Down
28 changes: 14 additions & 14 deletions tests/regressiontests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -388,30 +388,30 @@ test-delim-both: $(TESTDIR)/test-delim-both.sh

test-init_simple: $(TESTDIR)/test-init_simple.sh
ERROR="[Nn]ot enough" $(REVERSE) $<
$< foo | grep -q "pos: foo"
$< foo | grep -q "'pos': foo"
$< foo --opt bar | grep -q " --opt: bar"
$< foo --opt bar | grep -q "boo is off"
$< foo --opt bar | grep -q "'boo' is off"
$< foo --opt bar --opt2 baz | grep -q " --opt: bar"
$< foo --opt bar --opt2 baz | grep -q " --opt2: baz"
$< foo --opt bar --opt2 baz --boo | grep -q "boo is on"
$< foo --opt bar --opt2 baz --boo | grep -q "'boo' is on"

test-init_simple-s: $(TESTDIR)/test-init_simple-s.sh
ERROR="[Nn]ot enough" $(REVERSE) $<
$< foo | grep -q "pos: foo"
$< foo | grep -q "'pos': foo"
$< foo --opt bar | grep -q " --opt: bar"
$< foo --opt bar | grep -q "boo is off"
$< foo --opt bar | grep -q "'boo' is off"
$< foo --opt bar --opt2 baz | grep -q " --opt: bar"
$< foo --opt bar --opt2 baz | grep -q " --opt2: baz"
$< foo --opt bar --opt2 baz --boo | grep -q "boo is on"
$< foo --opt bar --opt2 baz --boo | grep -q "'boo' is on"

test-init_simple-ss: $(TESTDIR)/test-init_simple-ss.sh $(TESTDIR)/test-init_simple-ss-parsing.sh
ERROR="[Nn]ot enough" $(REVERSE) $<
$< foo | grep -q "pos: foo"
$< foo | grep -q "'pos': foo"
$< foo --opt bar | grep -q " --opt: bar"
$< foo --opt bar | grep -q "boo is off"
$< foo --opt bar | grep -q "'boo' is off"
$< foo --opt bar --opt2 baz | grep -q " --opt: bar"
$< foo --opt bar --opt2 baz | grep -q " --opt2: baz"
$< foo --opt bar --opt2 baz --boo | grep -q "boo is on"
$< foo --opt bar --opt2 baz --boo | grep -q "'boo' is on"

$(TESTDIR)/test-init_simple.m4: $(ARGBASH_INIT)
$< --pos pos --opt opt2 --opt opt --opt-bool boo $@
Expand All @@ -423,21 +423,21 @@ $(TESTDIR)/regenerate-test-init_simple-s-update.m4:
touch $@

$(TESTDIR)/test-init_simple-s-update.m4: $(ARGBASH_INIT) $(TESTDIR)/regenerate-test-init_simple-s-update.m4
$< --opt ordnung -s $@
$< --opt ordnung -s $@ > /dev/null
sed -i 's/^echo .*//' $@
echo 'test "$$_arg_ordnung" = yes || exit 1' >> $@

test-init_simple-s-update: $(TESTDIR)/test-init_simple-s-update.sh
@# Regenerate everyting during the next test run
touch $(TESTDIR)/regenerate-test-init_simple-s-update.m4
$< --ordnung yes
$< --ordnung yes > /dev/null
$(REVERSE) $<
ERROR="unexpected argument" $(REVERSE) $< -o yes
sed -i 's/\[ordnung\],\[\]/&[o]/' $(TESTDIR)/test-init_simple-s-update-parsing.sh
sed -i 's/\[ordnung\]/&,[o]/' $(TESTDIR)/test-init_simple-s-update-parsing.sh
$(ARGBASH_BIN) $< > /dev/null
$< --ordnung yes
$< --ordnung yes > /dev/null
$(REVERSE) $<
$< -o yes
$< -o yes > /dev/null

$(TESTDIR)/test-init_simple-ss.sh: $(ARGBASH_INIT)
$< --pos pos --opt opt2 --opt opt --opt-bool boo -s -s $@
Expand Down
16 changes: 8 additions & 8 deletions tests/regressiontests/make/tests/tests-init.m4
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
m4_pushdef([tbody], [[[
ERROR="[Nn]ot enough" $(REVERSE) $<
$< foo | grep -q "pos: foo"
$< foo | grep -q "'pos': foo"
$< foo --opt bar | grep -q " --opt: bar"
$< foo --opt bar | grep -q "boo is off"
$< foo --opt bar | grep -q "'boo' is off"
$< foo --opt bar --opt2 baz | grep -q " --opt: bar"
$< foo --opt bar --opt2 baz | grep -q " --opt2: baz"
$< foo --opt bar --opt2 baz --boo | grep -q "boo is on"
$< foo --opt bar --opt2 baz --boo | grep -q "'boo' is on"
]]])


Expand Down Expand Up @@ -41,7 +41,7 @@ ADD_RULE([$(TESTDIR)/regenerate-test-init_simple-s-update.m4], [],

dnl Take out all echos (argbash-init puts them there) so that we don't have to discard stdout.
ADD_RULE([$(TESTDIR)/test-init_simple-s-update.m4], [$(ARGBASH_INIT) $(TESTDIR)/regenerate-test-init_simple-s-update.m4],
[$< --opt ordnung -s $@
[$< --opt ordnung -s $@ > /dev/null
sed -i 's/^echo .*//' $@
echo 'test "$$_arg_ordnung" = yes || exit 1' >> $@
])
Expand All @@ -53,14 +53,14 @@ dnl 3. The script is regenerated and this time, we expect that the parsing stuff
ADD_TEST([test-init_simple-s-update], [[
@# Regenerate everyting during the next test run
touch $(TESTDIR)/regenerate-test-init_simple-s-update.m4
$< --ordnung yes
$< --ordnung yes > /dev/null
$(REVERSE) $<
ERROR="unexpected argument" $(REVERSE) $< -o yes
sed -i 's/\[ordnung\],\[\]/&[o]/' $(TESTDIR)/test-init_simple-s-update-parsing.sh
sed -i 's/\[ordnung\]/&,[o]/' $(TESTDIR)/test-init_simple-s-update-parsing.sh
$(ARGBASH_BIN) $< > /dev/null
$< --ordnung yes
$< --ordnung yes > /dev/null
$(REVERSE) $<
$< -o yes
$< -o yes > /dev/null
]])

ADD_SCRIPT([test-init_simple-ss-parsing])
Expand Down

0 comments on commit 4004d64

Please sign in to comment.