Skip to content

Commit 48302b8

Browse files
committed
Improved docs
1 parent 3f1ab6b commit 48302b8

File tree

15 files changed

+119
-78
lines changed

15 files changed

+119
-78
lines changed

ChangeLog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1.1.0a (2015-09-01)
1+
1.1.0 (2015-09-06)
22
------------------
33

44
New features:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Following arguments are supported:
3636

3737
The utility has been inspired by Python's `argparse` and the `shflags` project.
3838

39-
**Read [the docs](doc/index.rst) for more info**
39+
**[Read the docs](http://argbash.readthedocs.org/en/latest/) for more info**
4040

4141
## Requirements
4242

bin/argbash.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
VERSION=1.1.0a
3+
VERSION=1.1.0
44
# DEFINE_SCRIPT_DIR()
55
# ARG_POSITIONAL_SINGLE([input],[The input template file])
66
# ARG_OPTIONAL_SINGLE([output],[o],[Name of the output file (pass '-' for stdout)],[-])
@@ -10,7 +10,7 @@ VERSION=1.1.0a
1010

1111
# ARGBASH_GO()
1212
# needed because of Argbash --> m4_ignore([
13-
### START OF CODE GENERATED BY ARGBASH v1.1.0a one line above ###
13+
### START OF CODE GENERATED BY ARGBASH v1.1.0 one line above ###
1414
# THE DEFAULTS INITIALIZATION --- POSITIONALS
1515
# THE DEFAULTS INITIALIZATION --- OPTIONALS
1616
_ARG_OUTPUT="-"

doc/_static/minimal-output-help.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This is a minimal demo of Argbash potential
66
Usage: ../resources/examples/minimal.sh <positional-arg> [--option <arg>] [--(no-)verbose] [--version] [--help]
7-
<positional-arg>: Positional arg
7+
<positional-arg>: Positional arg description
88
-o,--option: A option with short and long flags and default (default: 'b')
99
--verbose,--no-verbose: A boolean option with long flag (and implicit default: off) (default: 'off')
1010
-v,--version: Prints version

doc/example.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _sec_example:
2+
13
Minimal example
24
===============
35

@@ -14,7 +16,7 @@ Here, we can notice multiple notable things:
1416
#. Definitions of arguments are placed before the script body.
1517
From Bash point of view, they are commented out, so the "template" can be a syntactically valid script.
1618

17-
#. ...
19+
#. You access the values of argument ``foo-bar`` as ``$_ARG_FOO_BAR`` etc. (this is covered more in-depth in :ref:`parsing_results`).
1820

1921
So let's try the script in action! Let's see what happens when we pass the -h option:
2022

doc/guide.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
Declaring arguments
2-
===================
1+
Template writing guide
2+
======================
3+
4+
This section tells you how to write templates, the next one is about ``argbash.sh`` invocation.
35

46
Definitions
57
-----------
@@ -184,6 +186,8 @@ The macro doesn't take any parameters.
184186
185187
ARGBASH_GO
186188

189+
.. _parsing_results:
190+
187191
Using parsing results
188192
+++++++++++++++++++++
189193

@@ -198,3 +202,4 @@ The argument name is transliterated like this:
198202
#. Boolean arguments have values either ``on`` or ``off``.
199203

200204
If (a boolean argument) ``--verbose`` is passed, value of ``_ARG_VERBOSE`` is set to ``on``.
205+

doc/index.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ So, let's say that we would like a script that produces the following help messa
3838
.. literalinclude:: _static/minimal-output-help.txt
3939
:start-after: minimal.sh
4040

41-
Then, it means that we need following arguments
41+
Then, it means that we need following arguments:
4242

4343
* One mandatory positional argument.
4444
(In other words, an argument that must be passed and that is not preceeded by options such as ``--foo``, ``-f``.)
@@ -55,7 +55,7 @@ Therefore, we write this to the template:
5555
:language: bash
5656
:end-before: needed because of Argbash
5757

58-
The body of the script is trivial, but note that it is enclosed in square brackets.
58+
The body of the script (that follows the template) is trivial, but note that it is enclosed in a pair of square brackets.
5959
They are "hidden" in comments, but still, they have to be there.
6060

6161
.. literalinclude:: ../resources/examples/minimal.m4
@@ -78,6 +78,11 @@ Now we launch it and the output is good!
7878
Positional arg value: posi-tional
7979
Optional arg --option value: opt-ional
8080

81+
.. note::
82+
83+
If something still isn't totally clear, take look at the :ref:`sec_example` section.
84+
85+
.. _limitations:
8186

8287
Limitations
8388
+++++++++++

doc/install.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ The main Makefile
3636

3737
The ``Makefile`` in the ``resources`` folder can do many things:
3838

39+
.. _install:
40+
3941
Installation
4042
++++++++++++
4143

doc/usage.rst

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,93 @@
1-
Usage
2-
=====
1+
Argbash invocation
2+
==================
33

4-
Layout
5-
------
4+
So, you have a template and now it is time to (re)generate a shell script from it!
65

7-
Separate file for parsing
8-
+++++++++++++++++++++++++
6+
File layout
7+
-----------
8+
9+
You have three options here, they are sorted by the estimated preference:
10+
11+
#. One file with both parsing code and script body.
12+
13+
This is a both simple and functional approach.
14+
15+
#. Two files --- one for the parsing code and one for the script body, both taken care of by ``Argbash``.
916

10-
This is really easy.
11-
Just place the directives in the file behind comments and then run the ``argbash.sh`` with the ``--standalone`` argument.
17+
This is more suitable for people that prefer to keep things tidy, you can have the parsing code separate and included in the script at run-time.
18+
However, ``Argbash`` can assist you with that.
19+
20+
#. Same as the above, just without ``Argbash`` assistance.
21+
22+
You want to take this path if your script has a non-matching square brackets problem (see :ref:`limitations`).
23+
This is very unlikely.
24+
25+
.. note::
26+
27+
We assume that you have installed (see :ref:`install`) the ``argbash.sh``, so it is available as a command ``argbash``.
28+
If it is not the case, you just have to substitute ``argbash`` by direct invocation of ``bin/argbash.sh``.
1229

1330
Parsing code and script body together
1431
+++++++++++++++++++++++++++++++++++++
1532

16-
This requires some trivial adjustments to your script.
33+
Assuming that you have created a template file ``my-template.m4``, you simply run ``argbash`` over the script [*]_:
1734

18-
#. Add Argbash definitions to the script so they come before the script body.
19-
Let's say that the file is called ``my-template.m4`` (the extension [*]_ is not important in itself).
20-
21-
.. note::
35+
::
36+
37+
argbash my-template.m4 -o my-script.sh
2238

23-
This one is just a recommendation, but don't do othewise if you don't have deep understanding of what's going on.
39+
If you want to regenerate a new version of ``my-script.sh`` after you have modified its template section, you can run
2440

25-
Definitions are described in the Argbash API section, include them in a form as shell comments like this:
41+
::
42+
43+
argbash my-script.sh -o my-script.sh
2644

27-
::
45+
as the script can deal with input and output being the same file.
2846

29-
# ARG_POSITIONAL_SINGLE([filename])
30-
# ARG_OPTIONAL_SINGLE([unit], u, [What unit we accept (b for bytes, k for kilobytes, M for megabytes)], b)
31-
# ARG_VERSION([echo $0 v0.1])
32-
# ARG_OPTIONAL_BOOLEAN(verbose)
33-
# ARG_HELP
34-
# ARGBASH_GO
47+
.. [*] ``m4`` is the extension used for the ``M4`` language, but we use the ``m4sugar`` extension built on top of it).
3548
36-
#. Add this comment before the script body (it is a commented opening square bracket, the note is optional):
49+
Separate file for parsing with assistance
50+
+++++++++++++++++++++++++++++++++++++++++
3751

38-
::
52+
You have two files, let's say it is a ``my-parsing.m4`` and ``my-script.sh``.
53+
The ``my-parsing.m4`` file contains just the template section of ``my-script.sh``.
54+
Then, you add a very small template code to ``my-script.sh`` at the beginning:
3955

40-
# [ note: <--- this has to be here because of Argbash
56+
.. code-block:: bash
4157
42-
and another one (closing square bracket):
58+
# DEFINE_SCRIPT_DIR
59+
# INCLUDE_PARSING_CODE([my-parsing.sh])
60+
# ARGBASH_GO
61+
62+
# [ <-- needed because of Argbash
63+
64+
# HERE GOES THE SCRIPT BODY
65+
66+
# ] <-- needed because of Argbash
4367
44-
::
68+
i.e. you add thos three lines with definitions and you enclose the script in square brackets.
4569

46-
# ] note: <--- this has to be here because of Argbash
70+
Finally, you just make sure that ``my-script.sh`` and ``my-parsing.m4`` are next to each other and run
4771

48-
#. Run the ``argbash.sh`` over the script:
72+
::
4973

50-
::
51-
52-
bin/argbash.sh my-template.m4 -o my-script.sh
74+
argbash my-script.sh -o my-script.sh
5375

54-
Then, if you do some script development and you decide to add an option or remove one:
76+
which finds ``my-parsing.m4`` (it would find ``my-parsing.sh`` too) and generates new ``my-parsing.sh`` and ``my-script.sh`` that you can use right away.
5577

56-
4. Forget about the template, edit the script --- declarations are preserved in there.
57-
Obtain the updated version by re-running ``argbash.sh`` over ``my-script.sh``:
78+
Separate file for parsing
79+
+++++++++++++++++++++++++
5880

59-
::
60-
61-
bin/argbash.sh my-script.sh -o my-script-new.sh
81+
If you want/have to take care of including the parsing code yourself, just make sure you do it in the script:
6282

63-
If you compare the two, you should find out that it works quite well and that the ``my-script-new.sh`` does what it is supposed to do and that it is by no means cluttered.
83+
.. code-block:: bash
6484
65-
.. [*] ``m4`` is the extension used for the ``M4`` language, but we use the ``m4sugar`` extension built on top of it).
85+
source $(dirname $0)/my-parsing.sh
86+
87+
# HERE GOES THE SCRIPT BODY
88+
89+
Then, you just generate ``my-parsing.sh`` using ``--standalone`` option:
6690

91+
::
6792

93+
argbash my-parsing.m4 -o my-parsing.sh

resources/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ROOT = /
1212

1313
GENPARSE = ../bin/argbash.sh
1414
EXAMPLES = \
15+
examples/minimal.sh \
1516
examples/simple.sh \
1617
examples/simple-standalone.sh \
1718
$(NUL)
@@ -84,7 +85,7 @@ version:
8485
touch $(M4_SRC)
8586

8687
release: version $(GENPARSE) examples check
87-
sed -i "1s/([-0-9]+)/($$(date -I))/ $(CHANGELOG)
88+
sed -i "1s/([-0-9]+)/($$(date -I))/" $(CHANGELOG)
8889

8990
tag:
9091
git tag $(shell cat ../src/version) -s -m 'Version release' master

resources/examples/minimal.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
# ARG_POSITIONAL_SINGLE([positional-arg])
3+
# ARG_POSITIONAL_SINGLE([positional-arg], [Positional arg description])
44
# ARG_OPTIONAL_SINGLE([option], o, [A option with short and long flags and default], b)
55
# ARG_OPTIONAL_BOOLEAN([verbose], , [A boolean option with long flag (and implicit default: off)], )
66
# ARG_VERSION([echo $0 v0.1])

resources/examples/minimal.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
#!/bin/bash
22

3-
# ARG_POSITIONAL_SINGLE([positional-arg])
3+
# ARG_POSITIONAL_SINGLE([positional-arg],[Positional arg description])
44
# ARG_OPTIONAL_SINGLE([option],[o],[A option with short and long flags and default],[b])
55
# ARG_OPTIONAL_BOOLEAN([verbose],[],[A boolean option with long flag (and implicit default: off)],[])
66
# ARG_VERSION([echo $0 v0.1])
77
# ARG_HELP([This is a minimal demo of Argbash potential])
88
# ARGBASH_GO()
99
# needed because of Argbash --> m4_ignore([
10-
### START OF CODE GENERATED BY ARGBASH v1.0.0 one line above ###
11-
# THE DEFAULTS INITIALIZATION
12-
_ARG_OPTION=b
10+
### START OF CODE GENERATED BY ARGBASH v1.1.0 one line above ###
11+
# THE DEFAULTS INITIALIZATION --- POSITIONALS
12+
# THE DEFAULTS INITIALIZATION --- OPTIONALS
13+
_ARG_OPTION="b"
1314
_ARG_VERBOSE=off
1415

1516
# THE PRINT HELP FUNCION
1617
function print_help
1718
{
1819
echo "This is a minimal demo of Argbash potential"
1920
echo "Usage: $0 <positional-arg> [--option <arg>] [--(no-)verbose] [--version] [--help]"
20-
echo -e "\t<positional-arg>: Positional arg"
21-
echo -e "\t-o,--option: A option with short and long flags and default (default: 'b')"
22-
echo -e "\t--verbose,--no-verbose: A boolean option with long flag (and implicit default: off) (default: 'off')"
21+
echo -e "\t<positional-arg>: Positional arg description"
22+
echo -e "\t-o,--option: A option with short and long flags and default (default: '""b""')"
23+
echo -e "\t--verbose,--no-verbose: A boolean option with long flag (and implicit default: off) (default: '"off"')"
2324
echo -e "\t-v,--version: Prints version"
2425
echo -e "\t-h,--help: Prints help"
2526
}
@@ -47,15 +48,14 @@ do
4748
;;
4849
*)
4950
POSITIONALS+=("$1")
50-
# unknown option
5151
;;
5252
esac
5353
shift
5454
done
5555

5656
POSITIONAL_NAMES=('_ARG_POSITIONAL_ARG' )
5757
test ${#POSITIONALS[@]} -lt 1 && { ( echo "FATAL ERROR: Not enough positional arguments."; print_help ) >&2; exit 1; }
58-
test ${#POSITIONALS[@]} -gt 1 && { ( echo "FATAL ERROR: There were spurious positional arguments."; print_help ) >&2; exit 1; }
58+
test ${#POSITIONALS[@]} -gt 1 && { ( echo "FATAL ERROR: There were spurious positional arguments --- we expect at most 1."; print_help ) >&2; exit 1; }
5959
for (( ii = 0; ii < ${#POSITIONALS[@]}; ii++))
6060
do
6161
eval "${POSITIONAL_NAMES[$ii]}=\"${POSITIONALS[$ii]}\""

resources/examples/simple-standalone.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
# ARG_HELP()
88
# ARGBASH_GO()
99
# needed because of Argbash --> m4_ignore([
10-
### START OF CODE GENERATED BY ARGBASH v1.0.0 one line above ###
11-
# THE DEFAULTS INITIALIZATION
12-
_ARG_UNIT=b
10+
### START OF CODE GENERATED BY ARGBASH v1.1.0 one line above ###
11+
# THE DEFAULTS INITIALIZATION --- POSITIONALS
12+
# THE DEFAULTS INITIALIZATION --- OPTIONALS
13+
_ARG_UNIT="b"
1314
_ARG_VERBOSE=off
1415

1516
# THE PRINT HELP FUNCION
1617
function print_help
1718
{
1819
echo "Usage: $0 <filename> [--unit <arg>] [--version] [--(no-)verbose] [--help]"
19-
echo -e "\t<filename>: Positional arg"
20-
echo -e "\t-u,--unit: What unit we accept (b for bytes, k for kilobytes, M for megabytes) (default: 'b')"
20+
echo -e "\t<filename>: "
21+
echo -e "\t-u,--unit: What unit we accept (b for bytes, k for kilobytes, M for megabytes) (default: '""b""')"
2122
echo -e "\t-v,--version: Prints version"
22-
echo -e "\t--verbose,--no-verbose: (default: 'off')"
23+
echo -e "\t--verbose,--no-verbose: (default: '"off"')"
2324
echo -e "\t-h,--help: Prints help"
2425
}
2526
# THE PARSING ITSELF
@@ -46,15 +47,14 @@ do
4647
;;
4748
*)
4849
POSITIONALS+=("$1")
49-
# unknown option
5050
;;
5151
esac
5252
shift
5353
done
5454

5555
POSITIONAL_NAMES=('_ARG_FILENAME' )
5656
test ${#POSITIONALS[@]} -lt 1 && { ( echo "FATAL ERROR: Not enough positional arguments."; print_help ) >&2; exit 1; }
57-
test ${#POSITIONALS[@]} -gt 1 && { ( echo "FATAL ERROR: There were spurious positional arguments."; print_help ) >&2; exit 1; }
57+
test ${#POSITIONALS[@]} -gt 1 && { ( echo "FATAL ERROR: There were spurious positional arguments --- we expect at most 1."; print_help ) >&2; exit 1; }
5858
for (( ii = 0; ii < ${#POSITIONALS[@]}; ii++))
5959
do
6060
eval "${POSITIONAL_NAMES[$ii]}=\"${POSITIONALS[$ii]}\""

0 commit comments

Comments
 (0)