Skip to content

Commit 512bb04

Browse files
committed
Add a new 'empty' output mode
This output mode doesn't contain any Argbash-generated code. It is suitable for economical storage of Argbash-powered script if one believes to have reliable access to Argbash.
1 parent 8e19a11 commit 512bb04

File tree

10 files changed

+94
-22
lines changed

10 files changed

+94
-22
lines changed

bin/argbash

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# ARG_OPTIONAL_REPEATED([search],[I],[Directories to search for the wrapped scripts (directory of the template will be added to the end of the list)],["."])
1818
# ARG_OPTIONAL_SINGLE([debug],[],[(developer option) Tell autom4te to trace a macro])
1919
# ARG_TYPE_GROUP_SET([content],[content],[strip],[none,user-content,all])
20-
# ARG_TYPE_GROUP_SET([type],[type],[type],[bash-script,posix-script,manpage,manpage-defs,completion,docopt])
20+
# ARG_TYPE_GROUP_SET([type],[type],[type],[bash-script,posix-script,manpage,manpage-defs,completion,docopt,excised])
2121
# ARG_DEFAULTS_POS([])
2222
# ARG_HELP([Argbash is an argument parser generator for Bash.])
2323
# ARG_VERSION_AUTO([_ARGBASH_VERSION])
@@ -52,12 +52,12 @@ content()
5252

5353
type()
5454
{
55-
local _allowed=("bash-script" "posix-script" "manpage" "manpage-defs" "completion" "docopt") _seeking="$1"
55+
local _allowed=("bash-script" "posix-script" "manpage" "manpage-defs" "completion" "docopt" "excised") _seeking="$1"
5656
for element in "${_allowed[@]}"
5757
do
5858
test "$element" = "$_seeking" && echo "$element" && return 0
5959
done
60-
die "Value '$_seeking' (of argument '$2') doesn't match the list of allowed values: 'bash-script', 'posix-script', 'manpage', 'manpage-defs', 'completion' and 'docopt'" 4
60+
die "Value '$_seeking' (of argument '$2') doesn't match the list of allowed values: 'bash-script', 'posix-script', 'manpage', 'manpage-defs', 'completion', 'docopt' and 'excised'" 4
6161
}
6262

6363

@@ -90,7 +90,7 @@ print_help()
9090
printf '\t%s\n' "<input>: The input template file (pass '-' for stdin)"
9191
printf '\t%s\n' "-o, --output: Name of the output file (pass '-' for stdout) (default: '-')"
9292
printf '\t%s\n' "-i, --in-place, --no-in-place: Update a bash script in-place (off by default)"
93-
printf '\t%s\n' "-t, --type: Output type to generate. Can be one of: 'bash-script', 'posix-script', 'manpage', 'manpage-defs', 'completion' and 'docopt' (default: 'bash-script')"
93+
printf '\t%s\n' "-t, --type: Output type to generate. Can be one of: 'bash-script', 'posix-script', 'manpage', 'manpage-defs', 'completion', 'docopt' and 'excised' (default: 'bash-script')"
9494
printf '\t%s\n' "--library, --no-library: Whether the input file if the pure parsing library (off by default)"
9595
printf '\t%s\n' "--strip: Determines what to have in the output. Can be one of: 'none', 'user-content' and 'all' (default: 'none')"
9696
printf '\t%s\n' "--check-typos, --no-check-typos: Whether to check for possible argbash macro typos (on by default)"

doc/usage.rst

+17
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Bash completion completion all
248248
docopt help message docopt all
249249
manpage template manpage all
250250
manpage template definitions manpage-defs all
251+
No generated code excised all
251252
============================ ======================= ==========================
252253

253254

@@ -328,6 +329,7 @@ Typically, you generate docopt output to the standard output from the generated
328329
You can't pass the script which sources the parsing code as Argbash input, as it doesn't contain any Argbash macros.
329330
If you pass the parsing code script/template as Argbash input, the output will have wrong basename, as Argbash will use the basename of the parsing code file, not of the real script.
330331

332+
331333
.. _manpage_output:
332334

333335
Manpage output
@@ -351,6 +353,21 @@ So given a argbash-powered script or m4 file, your manpage workflow will typical
351353
$ man ./my-script.1
352354
353355
356+
.. _excise_output:
357+
358+
Output without generated code
359+
+++++++++++++++++++++++++++++
360+
361+
Argbash can also generte output that doesn't contain parsing code --- only user code and the Argbash template.
362+
If the input contained parsing code, that one gets *excised*.
363+
There are following examples when the excised output makes sense when dealing with Argbash-powered scripts:
364+
365+
- Space saving for e.g. copy-pasting:
366+
Don't involve generated code earlier than you need to, as it does make the script larger, and that can be an annoyance.
367+
- Normalization of scripts for storage:
368+
Store only definitions that are less likely to change, and generate implementation of parsing using the latest greatest version of Argbash just in time.
369+
370+
354371
.. _api_change:
355372

356373
API changes support

resources/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ M4_SRC += \
1919
../src/output-bash-script.m4 \
2020
../src/output-completion.m4 \
2121
../src/output-docopt.m4 \
22+
../src/output-excised.m4 \
2223
../src/output-manpage.m4 \
2324
../src/output-manpage-defs.m4 \
2425
../src/output-posix-script.m4 \

src/argbash.m4

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# ARG_OPTIONAL_REPEATED([search], I, [Directories to search for the wrapped scripts (directory of the template will be added to the end of the list)], ["."])
1818
# ARG_OPTIONAL_SINGLE([debug],, [(developer option) Tell autom4te to trace a macro])
1919
# ARG_TYPE_GROUP_SET([content], [content], [strip], [none,user-content,all])
20-
# ARG_TYPE_GROUP_SET([type], [type], [type], [bash-script,posix-script,manpage,manpage-defs,completion,docopt])
20+
# ARG_TYPE_GROUP_SET([type], [type], [type], [bash-script,posix-script,manpage,manpage-defs,completion,docopt,excised])
2121
# ARG_DEFAULTS_POS()
2222
# ARG_HELP([Argbash is an argument parser generator for Bash.])
2323
# ARG_VERSION_AUTO([_ARGBASH_VERSION])

src/output-excised.m4

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dnl
2+
dnl $1: The macro call (the caller is supposed to pass [$0($@)])
3+
dnl What is also part of the API: The line
4+
dnl ### START OF CODE GENERATED BY Argbash vx.y.z one line above ###
5+
m4_define([ARGBASH_GO_BASE], [m4_do(
6+
[[$1]],
7+
)])

tests/regressiontests/Makefile

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ ARGBASH_INIT_EXEC ?= $(ARGBASH_INIT)
1515
$(word 2,$^) --type posix-script -o $@ $<
1616
sed -i "s|#!/usr/bin/env bash|#!$(shell which dash)|" $@
1717

18+
%-excised.m4: %.sh $(ARGBASH_BIN)
19+
$(word 2,$^) --type excised -o $@ $<
20+
1821
%.sh: %.m4 $(ARGBASH_BIN)
1922
$(word 2,$^) $< -o $@
2023

@@ -63,7 +66,6 @@ SCRIPTS = \
6366
$(TESTDIR)/test-getopt-space.sh \
6467
$(NUL)
6568

66-
# bash tests
6769
TESTS += \
6870
stability \
6971
basic \
@@ -111,12 +113,17 @@ TESTS += \
111113
test-getopt-equals \
112114
$(NUL)
113115

114-
# docopt tests
115116
TESTS += \
116117
basic-docopt \
117118
test-onlyopt-docopt \
118119
$(NUL)
119120

121+
122+
TESTS += \
123+
test-ls_like-excised \
124+
$(NUL)
125+
126+
120127
TESTS_GEN += \
121128
gen-test-pos \
122129
gen-test-opt \
@@ -183,6 +190,7 @@ SCRIPTS += \
183190
$(TESTDIR)/test-getopt-equals.sh \
184191
$(TESTDIR)/basic.txt \
185192
$(TESTDIR)/test-onlyopt.txt \
193+
$(TESTDIR)/test-ls_like-excised.m4 \
186194
$(NUL)
187195

188196
ifneq "$(shell which dash 2> /dev/null)" ""
@@ -600,6 +608,9 @@ test-onlyopt-docopt: $(TESTDIR)/test-onlyopt.txt
600608
grep -q "\[--opt-repeated OPT-REPEATED\]\.\.\." $<
601609
grep -q "\[--incrx\]\.\.\." $<
602610

611+
test-ls_like-excised: $(TESTDIR)/test-ls_like-excised.m4
612+
diff -q $(TESTDIR)/test-ls_like.m4 $<
613+
603614
test-delim-space: $(TESTDIR)/test-delim-space.sh
604615
ERROR="unexpected argument '--opt=something'" $(REVERSE) $< --opt=something
605616
$< --opt something | grep -q 'OPT_S=something,'

tests/regressiontests/make/Makefile.m4

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ m4_define([include_test], [m4_do(
1111

1212
m4_include([tests/tests-base.m4])
1313
m4_include([tests/tests-docopt.m4])
14+
m4_include([tests/tests-excise.m4])
1415
m4_include([tests/tests-delimiters.m4])
1516
m4_include([tests/tests-init.m4])
1617
m4_include([tests/tests-env.m4])
@@ -36,6 +37,9 @@ ARGBASH_INIT_EXEC ?= $(ARGBASH_INIT)
3637
$(word 2,$^) --type posix-script -o $@ $<
3738
[sed -i "s|#!/usr/bin/env bash|#!]$(shell which dash)|" $@
3839

40+
%-excised.m4: %.sh $(ARGBASH_BIN)
41+
$(word 2,$^) --type excised -o $@ $<
42+
3943
%.sh: %.m4 $(ARGBASH_BIN)
4044
$(word 2,$^) $< -o $@
4145

@@ -53,26 +57,31 @@ SCRIPTS = \
5357
], m4_set_list([_TEST_SCRIPTS])) \
5458
$(NUL)
5559

56-
# bash tests
5760
TESTS += \
5861
m4_join([ \
5962
], m4_set_list([BASH_TESTS])) \
6063
$(NUL)
6164

62-
# docopt tests
6365
TESTS += \
6466
m4_join([ \
6567
], m4_set_list([DOCOPT_TESTS])) \
6668
$(NUL)
6769

70+
71+
TESTS += \
72+
m4_join([ \
73+
], m4_set_list([EXCISE_OUTPUT_TESTS])) \
74+
$(NUL)
75+
76+
6877
TESTS_GEN += \
6978
m4_join([ \
7079
], m4_set_list([_TEST_GEN_BASH])) \
7180
$(NUL)
7281

7382
SCRIPTS += \
7483
m4_join([ \
75-
], m4_set_list([_TEST_BASH_SCRIPTS]),m4_set_list([_TEST_DOCOPT_SCRIPTS])) \
84+
], m4_set_list([_TEST_BASH_SCRIPTS]),m4_set_list([_TEST_DOCOPT_SCRIPTS]),m4_set_list([_TEST_EXCISE_OUTPUT_SCRIPTS])) \
7685
$(NUL)
7786

7887
ifneq "$(shell which dash 2> /dev/null)" ""

tests/regressiontests/make/make.m4

+29-4
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,41 @@ dnl
4949
dnl Remarks:
5050
dnl No leading/trailing newlines, around the test body as it already has those.
5151
m4_define([ADD_DOCOPT_TEST], [m4_do(
52-
[m4_pushdef([_script], m4_default_quoted([$4], [$(TESTDIR)/$1.txt]))],
52+
[m4_pushdef([_artifact], m4_default_quoted([$4], [$(TESTDIR)/$1.txt]))],
5353
[m4_pushdef([_testname], [[$1-docopt]])],
5454
[m4_set_add([DOCOPT_TESTS], _testname)],
55-
[m4_set_add([_TEST_DOCOPT_SCRIPTS], m4_quote(_script))],
55+
[m4_set_add([_TEST_DOCOPT_SCRIPTS], m4_quote(_artifact))],
5656
[m4_divert_text([STDOUT3], [m4_do(
57-
_testname[: _script[]m4_ifnblank([$3], [ $3])],
57+
_testname[: _artifact[]m4_ifnblank([$3], [ $3])],
5858
[$2],
5959
)])],
6060
[m4_popdef([_testname])],
61-
[m4_popdef([_script])],
61+
[m4_popdef([_artifact])],
62+
)])
63+
64+
65+
dnl
66+
dnl $1: The test name
67+
dnl $2: The test body (see also: TEST_BODY)
68+
dnl
69+
dnl Remarks:
70+
dnl No leading/trailing newlines, around the test body as it already has those.
71+
m4_define([ADD_EXCISE_OUTPUT_TEST], [m4_do(
72+
[m4_pushdef([_original_source], [$(TESTDIR)/$1.m4])],
73+
[m4_pushdef([_artifact], [$(TESTDIR)/$1-excised.m4])],
74+
[m4_pushdef([_testname], [[$1-excised]])],
75+
[m4_set_add([EXCISE_OUTPUT_TESTS], _testname)],
76+
[m4_set_add([_TEST_EXCISE_OUTPUT_SCRIPTS], m4_quote(_artifact))],
77+
[m4_divert_text([STDOUT3], [m4_do(
78+
[_testname: _artifact],
79+
[
80+
diff -q _original_source $<],
81+
[$2],
82+
[
83+
],
84+
)])],
85+
[m4_popdef([_testname])],
86+
[m4_popdef([_artifact])],
6287
)])
6388

6489

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ADD_EXCISE_OUTPUT_TEST([test-ls_like], [])
2+

tests/regressiontests/test-ls_like.m4

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env -S bash -e
22

3-
# ARG_POSITIONAL_SINGLE([arg], [], [foo])
4-
# ARG_OPTIONAL_SINGLE([width], w, [])
5-
# ARG_OPTIONAL_BOOLEAN([time], t, [])
6-
# ARG_OPTIONAL_BOOLEAN([long], l, [)
7-
# ARG_OPTIONAL_INCREMENTAL([verbose], v, [])
3+
# ARG_POSITIONAL_SINGLE([arg],[],[foo])
4+
# ARG_OPTIONAL_SINGLE([width],[w],[])
5+
# ARG_OPTIONAL_BOOLEAN([time],[t],[])
6+
# ARG_OPTIONAL_BOOLEAN([long],[l],[])
7+
# ARG_OPTIONAL_INCREMENTAL([verbose],[v],[])
88
# ARG_HELP([Testing program])
9-
# ARGBASH_GO
10-
#[
9+
# ARGBASH_GO()
10+
# [ <-- needed because of Argbash
1111

1212
# Now we take the parsed data and assign them no nice-looking variable names,
1313
# sometimes after a basic validation
1414
echo "ARG=$_arg_arg,WIDTH=$_arg_width,TIME=$_arg_time,LONG=$_arg_long,VERBOSE=$_arg_verbose,"
1515

16-
#]
16+
# ] <-- needed because of Argbash

0 commit comments

Comments
 (0)