Skip to content

Commit

Permalink
Group type validator fix.
Browse files Browse the repository at this point in the history
Validator was misbehaving when one of values could be blank.
Refs #26.
  • Loading branch information
matejak committed Aug 27, 2017
1 parent ed01bfd commit d5a21d2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/argument_value_types.m4
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dnl TODO: Integrate with help (and not only with the help synopsis)
argbash_api([ARG_TYPE_GROUP_SET], [m4_do(
[[$0($@)]],
[m4_foreach([_val], [$4], [m4_do(
[m4_list_append([_LIST_$1_QUOTED], m4_quote(_sh_quote(m4_quote(_val))))],
[m4_list_append([_LIST_$1_QUOTED], m4_quote(_sh_quote_also_blanks(m4_quote(_val))))],
[m4_list_append([_LIST_$1], m4_quote(_val))],
)])],
[_define_validator([$1], m4_expand([_MK_VALIDATE_GROUP_FUNCTION([$1], [$5])]),
Expand Down
16 changes: 15 additions & 1 deletion src/stuff.m4
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ dnl TODO: Sort out the argbash_api macro
dnl TODO: Test for parsing library hidden in a subdirectory / having an absolute path(?)
dnl - check out the INCLUDE_PARSING_CODE macro
dnl - check out argbash script that has to be able to find it
dnl TODO: Add normalize-args utility --- given described script's command-line, simplify it.
dnl
dnl vvvvvvvvvvvvvvv
dnl TODO: Optimize the _CHECK_PASSED_VALUE_AGAINST_BLACKLIST calls
dnl TODO: Support custom error messages
dnl TODO: Make positional args check optional - make it a function(n_positionals, n_expected, what is expected, msg[when less args], [msg when more args]
dnl TODO: Introduce alternative REPEATED/INCREMENTAL version of macros (add and replace mode with respect to defaults)
dnl TODO: Make ARG_TYPE_GROUP_SET fool-proof (check the validity of elements of the 3rd argument)
dnl
dnl WIP vvvvvvvvvvvvvvv
dnl TODO: Define parsing code as a function so one can call it on its own. Implement DIY mode
dnl TODO: DIsplay the options stacking mode in the help message.
dnl
dnl Arg groups:
dnl name is used both in help and internally as an ID
Expand Down Expand Up @@ -97,7 +100,7 @@ m4_define([_varname], [m4_do(

dnl
dnl Encloses string into "" if its first char is not ' or "
dnl The string si also []-quoted
dnl The string is also []-quoted
dnl Property: Quoting a blank input results in blank result
dnl to AVOID it, pass string like ""ls -l or "ls" -l
dnl $1: String to quote
Expand All @@ -110,6 +113,17 @@ m4_define([_sh_quote], [m4_do(
)])


dnl
dnl Same as _sh_quote, except Quoting a blank input results in pair of quotes
dnl $1: String to quote
m4_define([_sh_quote_also_blanks], [m4_do(
[m4_if(
m4_index([$1], [']), 0, [[$1]],
m4_index([$1], ["]), 0, [[$1]],
[["$1"]])],
)])


dnl
dnl $1: Argument name
dnl $2: The variable name that would hold the argument value
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ test-int: $(TESTDIR)/test-int.sh

test-group: $(TESTDIR)/test-group.sh
$< foo | grep -q "ACT=foo"
$< '' | grep -q "ACT="
$< foo,baz | grep -q "ACT=foo,baz,"
$< "bar bar" | grep -q "ACT=bar bar,"
ERROR="allowed" $(REVERSE) $< fuuuu
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/make/tests/tests-types.m4
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ADD_TEST([test-int], [[

ADD_TEST([test-group], [[
$< foo | grep -q "ACT=foo"
$< '' | grep -q "ACT="
$< foo,baz | grep -q "ACT=foo,baz,"
$< "bar bar" | grep -q "ACT=bar bar,"
ERROR="allowed" $(REVERSE) $< fuuuu
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/test-group.m4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# ARG_POSITIONAL_SINGLE([act-ion], [], [foo])
# ARG_TYPE_GROUP_SET([act], [ACTION], [act-ion], [foo,baz,bar bar,[foo,baz]])
# ARG_TYPE_GROUP_SET([act], [ACTION], [act-ion], [foo,baz,bar bar,[foo,baz],])
# ARG_HELP([Testing program])
# ARGBASH_GO
#[
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/check-list.m4
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ assert_equals(m4_list_nth([FOO], 2), [BOMB])

m4_list_append([EMPTY], [])
m4_list_append([EMPTY], [second])
dnl The first element is the empty string

assert_equals(m4_list_nth([EMPTY], 1), [])
assert_equals(m4_list_nth([EMPTY], 2), [second])
assert_equals(m4_list_len([EMPTY]), 2)
m4_ignore([
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/check-utils.m4
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
m4_include([list.m4])
m4_include([stuff.m4])
m4_include([test-support.m4])

m4_list_append([FOO], [one])
Expand Down Expand Up @@ -33,3 +34,13 @@ assert_equals(m4_lists_foreach([FOO],[fu],[fu:]),

assert_equals(m4_lists_foreach([FOO,BAR,BAZ],[fu,ba,za],[fu: ba-za@]),
[one: 1-foo@two: 2-bar@])

assert_equals(_sh_quote(), [])
assert_equals(_sh_quote(x), ["x"])
assert_equals(_sh_quote("x"), ["x"])
assert_equals(_sh_quote('x'), ['x'])

assert_equals(_sh_quote_also_blanks(), [""])
assert_equals(_sh_quote_also_blanks(x), ["x"])
assert_equals(_sh_quote_also_blanks("x"), ["x"])
assert_equals(_sh_quote_also_blanks('x'), ['x'])

0 comments on commit d5a21d2

Please sign in to comment.