Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2.12.0 (unreleased)
-------------------

Bugfixes:

* `argbash` warns if the template lacks the guard lines past `ARGBASH_GO`, as square brackets in the user code get stripped in that case (#205).

2.11.0 (2025-05-23)
-------------------

Expand Down
19 changes: 19 additions & 0 deletions bin/argbash
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,24 @@ get_parsing_code()
}


# The user content past ARGBASH_GO has to be enclosed in guard lines that contain square brackets,
# otherwise m4 strips square brackets from it.
# $1: The input file
warn_if_guards_are_missing()
{
test "$_arg_strip" = none || return 0
case "$_arg_type" in *script) ;; *) return 0 ;; esac
awk '
/^#[[:space:]]*ARGBASH_(GO|PREPARE)([^A-Za-z0-9_]|$)/ { seen_go = 1; if ($0 ~ /\[/) seen_guard = 1 }
seen_go && /^#[[:space:]]*\[/ { seen_guard = 1 } # ]] <-- needed because of Argbash
END { exit !(seen_go && !seen_guard) }
' "$1" || return 0
printf '%s\n' \
"Warning: The ARGBASH_GO (or ARGBASH_PREPARE) line is not followed by the '# [ <-- needed because of Argbash' guard line (with the matching '# ] <-- needed because of Argbash' line at the end of the file)." \
"Square brackets in the code past that line will be stripped, and the script can't be regenerated correctly. See the 'Template layout' section of the documentation." >&2
}


# $1: The output file
# $2: The output type string
set_output_permission()
Expand Down Expand Up @@ -490,6 +508,7 @@ test "$_arg_library" = off && test -n "$parsing_code" && ($0 --strip user-conten
settle_wrapped_fname "$infile"

assert_m4_files_are_readable
warn_if_guards_are_missing "$infile"
output="$(do_stuff "$infile" "$outfname" "$_arg_type")" || die "" "$?"
if test "$_arg_check_typos" = on
then
Expand Down
19 changes: 19 additions & 0 deletions src/argbash.m4
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,24 @@ get_parsing_code()
}


# The user content past ARGBASH_GO has to be enclosed in guard lines that contain square brackets,
# otherwise m4 strips square brackets from it.
# $1: The input file
warn_if_guards_are_missing()
{
test "$_arg_strip" = none || return 0
case "$_arg_type" in *script) ;; *) return 0 ;; esac
awk '
/^#[[:space:]]*ARGBASH_(GO|PREPARE)([^A-Za-z0-9_]|$)/ { seen_go = 1; if ($0 ~ /\[/) seen_guard = 1 }
seen_go && /^#[[:space:]]*\[/ { seen_guard = 1 } # ]] <-- needed because of Argbash
END { exit !(seen_go && !seen_guard) }
' "$1" || return 0
printf '%s\n' \
"Warning: The ARGBASH_GO (or ARGBASH_PREPARE) line is not followed by the '# [ <-- needed because of Argbash' guard line (with the matching '# ] <-- needed because of Argbash' line at the end of the file)." \
"Square brackets in the code past that line will be stripped, and the script can't be regenerated correctly. See the 'Template layout' section of the documentation." >&2
}


# $1: The output file
# $2: The output type string
set_output_permission()
Expand Down Expand Up @@ -259,6 +277,7 @@ test "$_arg_library" = off && test -n "$parsing_code" && ($0 --strip user-conten
settle_wrapped_fname "$infile"

assert_m4_files_are_readable
warn_if_guards_are_missing "$infile"
output="$(do_stuff "$infile" "$outfname" "$_arg_type")" || die "" "$?"
if test "$_arg_check_typos" = on
then
Expand Down
10 changes: 10 additions & 0 deletions tests/regressiontests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ TESTS += \
test-infinity-nodefaults \
test-infinity-mixed \
test-leftovers \
test-missing-guards \
test-delim-space \
test-delim-equals \
test-delim-both \
Expand Down Expand Up @@ -600,6 +601,15 @@ gen-test-badcall-multi: $(TESTDIR)/gen-test-badcall-multi.m4 $(ARGBASH_BIN)
ERROR="num of args" $(REVERSE) $(ARGBASH_EXEC) $< > /dev/null
ERROR="actual number of" $(REVERSE) $(ARGBASH_EXEC) $< > /dev/null

test-missing-guards: $(TESTDIR)/test-simple.sh $(TESTDIR)/gen-test-missing-guards.m4
$(ARGBASH_EXEC) $(TESTDIR)/gen-test-missing-guards.m4 -o - 2>&1 > /dev/null | grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/gen-test-missing-guards.m4 --strip user-content -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/test-simple.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/../../src/argbash-init.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) --type posix-script $(TESTDIR)/gen-test-missing-guards.m4 -o - 2>&1 > /dev/null | grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) --type posix-script --strip all $(TESTDIR)/gen-test-missing-guards.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/gen-test-misspelled.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
test -z "$(SHELLCHECK)" || $(SHELLCHECK) "$(TESTDIR)/test-simple.sh"
basic-docopt: $(TESTDIR)/basic.txt
grep -q "\[<pos-opt>\]" $<
grep -q "\[--opt_arg OPT_ARG\]" $<
Expand Down
10 changes: 10 additions & 0 deletions tests/regressiontests/gen-test-missing-guards.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# A template that lacks the guard lines past ARGBASH_GO - argbash should warn about it.
# shellcheck disable=SC2154

# ARG_OPTIONAL_SINGLE([opt-arg], [o], [An optional argument])
# ARG_HELP([A template without the guard lines])
# ARGBASH_GO

echo "OPT_S=$_arg_opt_arg,"
12 changes: 12 additions & 0 deletions tests/regressiontests/make/tests/tests-base.m4
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,15 @@ ADD_GENTEST_BASH([misspelled], [ARG_FOOBAR], [ARGBASH_GOO])
dnl We have to escape \[ -> \@<:@ for grep
ADD_GENTEST_BASH([unmatched_bracket], [unmatched square bracket on line 3], [[# ARG_OPTIONAL_BOOLEAN(\[long\], l, \@<:@)]])
ADD_GENTEST_BASH([badcall-multi], [3rd argument], [num of args], [actual number of])


dnl Templates that lack the guard lines past ARGBASH_GO are reported.
ADD_TEST_BASH([test-missing-guards], [[
$(ARGBASH_EXEC) $(TESTDIR)/gen-test-missing-guards.m4 -o - 2>&1 > /dev/null | grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/gen-test-missing-guards.m4 --strip user-content -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/test-simple.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/../../src/argbash-init.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) --type posix-script $(TESTDIR)/gen-test-missing-guards.m4 -o - 2>&1 > /dev/null | grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) --type posix-script --strip all $(TESTDIR)/gen-test-missing-guards.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
$(ARGBASH_EXEC) $(TESTDIR)/gen-test-misspelled.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
]], [$(TESTDIR)/gen-test-missing-guards.m4], [$(TESTDIR)/test-simple.sh])