Skip to content

Commit 6653ca5

Browse files
author
Sylvain Viart
committed
prepare release v0.6.4-with-no-mangle-double-dash
1 parent 8e8c077 commit 6653ca5

File tree

12 files changed

+211
-83
lines changed

12 files changed

+211
-83
lines changed

README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Status: working.
88

99
Most concepts are documented in the `docopt` (without S) manual - see [docopt.org](http://docopt.org/).
1010

11-
Many examples use associative arrays in bash 4.x, but there is legacy support for bash 3.2 on macOS (OS X) or legacy
11+
Many examples use associative arrays in Bash 4+, but there is legacy support for Bash 3.2 on macOS (OS X) or legacy
1212
GNU/Linux OS.
1313

1414
[make README.md]: # (./docopts --version | get_version "This is a bug fix release:")
@@ -68,18 +68,24 @@ code. Double-dash `--` will be kept.
6868
`<prefix>` + `_` + `Mangled_name`.
6969

7070
* `--Long-Option` ==> `prefix_Long_Option`
71+
* etc.
7172

72-
Which makes it easy to filter variable with `grep` or such or to avoid globals
73-
name collision.
73+
Note that prefixed invalid mangled names still raise an error, if they resolve to
74+
invalid bash identifier.
75+
76+
Prefix gobals variable makes it easy to filter variable with `grep` or such or to
77+
avoid globals name collision.
7478

7579
Handling `[-]` in global mode is not supported and raises an error when trying to mangle `-`.
76-
But works for any other modes including `-G`.
80+
But works for any other modes including `-G`. This behavior differs from python's version of
81+
`docopts` which would have skiped the positional `-` without error.
82+
7783
```
7884
./docopts -h 'Usage: dump [-]' : -
7985
docopts:error: Print_bash_global:Mangling not supported for: '-'
8086
```
8187

82-
Single-dash can be catch easily by reading it in `FILENAME` parameter:
88+
Single-dash can be catch easily by reading it into a `FILENAME` parameter:
8389

8490
```bash
8591
./docopts -h 'Usage: prog parse FILENAME' : parse -
@@ -96,10 +102,12 @@ if [[ $f == '-' ]] ; then
96102
fi
97103
```
98104

105+
A working example is provided in [examples/legacy_bash/cat-n_wrapper_example.sh](examples/legacy_bash/cat-n_wrapper_example.sh)
106+
99107
### Associative Array mode
100108

101109
Alternatively, `docopts` can be invoked with the `-A <name>` option, which
102-
stores the parsed arguments as fields of a Bash 4 associative array called
110+
stores the parsed arguments as fields of a Bash 4+ associative array called
103111
`<name>` instead. However, as Bash does not natively support nested arrays,
104112
they are faked for repeatable arguments with the following access syntax:
105113

@@ -171,7 +179,7 @@ Options:
171179
first one that does not begin with a dash will
172180
be treated as positional arguments.
173181
-H, --no-help Don't handle --help and --version specially.
174-
-A <name> Export the arguments as a Bash 4.x associative
182+
-A <name> Export the arguments as a Bash 4+ associative
175183
array called <name>.
176184
-G <prefix> Don't use associative array but output
177185
Bash 3.2 compatible GLOBAL variables assignment:
@@ -189,9 +197,9 @@ Options:
189197

190198
## COMPATIBILITY
191199

192-
Bash 4.x and higher is the main target.
200+
Bash 4+ and higher is the main target.
193201

194-
In order to use `docopts` with bash 3.2 (for macOS and old GNU/Linux versions) by avoiding bash >4.x associative arrays,
202+
In order to use `docopts` with Bash 3.2 (for macOS and old GNU/Linux versions) by avoiding bash >4.x associative arrays,
195203
you can:
196204

197205
* don't use the `-A` option
@@ -280,7 +288,7 @@ for arg in "${argv[@]}"; do
280288
done
281289
```
282290

283-
The next example shows how using the Bash 4.x associative array with `-A`:
291+
The next example shows how using the Bash 4+ associative array with `-A`:
284292

285293
```bash
286294
help="
@@ -310,7 +318,9 @@ done
310318
`docopts` was first developed by Lari Rasku <rasku@lavabit.com> and was written in Python based on the
311319
[docopt Python parser](https://github.com/docopt/docopt).
312320

313-
The current version is written in [go](https://golang.org/) and is 100% compatible with previous Python-based `docopts`.
321+
The current version is written in [go](https://golang.org/) and is almost 100% compatible with previous Python-based `docopts`.
322+
See section `Global mode` for incompatibly details and provided work around.
323+
314324
Please report any non working code with [issue](https://github.com/docopt/docopts/issues) and examples.
315325

316326
## Roadmap: A new shell API is proposed
@@ -392,7 +402,7 @@ Tested builds are built on:
392402
[make README.md]: # (go version)
393403

394404
```
395-
go version go1.14.1 linux/amd64
405+
go version go1.17.1 linux/amd64
396406
```
397407

398408
## Features
@@ -405,9 +415,9 @@ documentation](docs/README.md).
405415

406416
`docopts` doesn't need a python interpreter anymore, so it works on any legacy system too.
407417

408-
As of 2019-05-18
418+
As of 2021-09-15
409419

410-
* `docopts` is able to reproduce 100% of the python version.
420+
* `docopts` is able to reproduce almost 100% of the python version.
411421
* unit tests for Go are provided, so hack as you wish.
412422
* 100% of `language_agnostic_tester.py` tests pass (GNU/Linux 64bits).
413423
* `bats-core` unittests and functional testing are provided too.

TODO.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
## PR52
44

5-
65
* ~~add unit test for corner case (`mangle_name` removes option `[--]`)~~
76
* ~~document how to add unittest in Go~~
87
* ~~document behavior parsing `--` `[--]` in global mode~~
98
* ~~add examples of `[--]` usage in global mode~~
109
* ~~add `language_agnostic_tester.py` input test~~
1110
* ~~better error message from `docopts`~~ no so better
12-
* also document `delv` debugger usage in delevopper's doc
11+
* ~~also document `delve` debugger usage in delevopper's doc~~
1312
* ~~update comment about version 0.6.3 and python compatibility~~
14-
* test python've version behavior with our input
15-
* add example of usage for handling `-` (stdin argument)
16-
* add functional testcase in bats with corner case `--no-mangle` `-G` etc.
13+
* ~~add functional testcase in bats with corner case `--no-mangle` `-G` etc.~~
14+
* ~~test python's version behavior with our input~~
15+
* ~~add example of usage for handling `-` (stdin argument)~~
16+
* ~~remove 100% compatible with python~~
1717

1818
## better error handling
1919

common_input_test.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@
9797
"p=false",
9898
"unparsed_option=('one' '-p' '-auto-approve' 'two')",
9999
"double_dash=true"
100+
],
101+
"expect_global_prefix": [
102+
"ARGS___=true",
103+
"ARGS_o=false",
104+
"ARGS_p=false",
105+
"ARGS_unparsed_option=('one' '-p' '-auto-approve' 'two')",
106+
"ARGS_double_dash=true"
100107
]
101108
}
102109
]

deployment.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ releases:
2323
2424
features changes:
2525
- fix error refusing mangling double dash in global mode [#52]
26-
- still refuse to mangle single dash `[-]` in global mode
27-
- now can mangle single dash in `-G` mode
26+
- still refuse to mangle single dash `[-]` in global mode (not compatible with v0.6.1)
27+
- now can mangle single-dash and double-dash in `-G` mode
2828
2929
internal changes:
30+
- use Go 1.17.1 for compiling
3031
- `language_agnostic_tester.py` re-write for python3
3132
- sort argument key in `Print_bash_args()` `Print_bash_global()`
3233
- sort input keys in `docopts_test.go`
33-
- add PR52 test to `testcases.docopt`
34+
- add PR #52 test to `testcases.docopt`
3435
- completed developper's documentation
3536
- #52 ignore `[--]` double-dash option in `Print_bash_global()`
3637
- reformat of Go code with gofmt indent change Space ==> Tab
38+
- add `tests/functional_tests_docopts.bats`
3739
3840
v0.6.3-rc2:
3941
name: "docopts binary transitional v0.6.3-rc2"

docopts.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ var (
2626
)
2727

2828
var copyleft = `
29-
Copyright (C) 2013 Vladimir Keleshev, Lari Rasku.
3029
Copyleft (Ɔ) 2021 Sylvain Viart (golang version).
30+
Copyright (C) 2013 Vladimir Keleshev, Lari Rasku.
3131
License MIT <http://opensource.org/licenses/MIT>.
3232
This is free software: you are free to change and redistribute it.
3333
There is NO WARRANTY, to the extent permitted by law.
3434
`
3535

36-
// Version will be build by main
36+
// Version will be build by main() function
3737
var Docopts_Version string
3838

3939
var Usage string = `Shell interface for docopt, the CLI description language.
@@ -65,7 +65,7 @@ Options:
6565
first one that does not begin with a dash will
6666
be treated as positional arguments.
6767
-H, --no-help Don't handle --help and --version specially.
68-
-A <name> Export the arguments as a Bash 4.x associative
68+
-A <name> Export the arguments as a Bash 4+ associative
6969
array called <name>.
7070
-G <prefix> Don't use associative array but output
7171
Bash 3.2 compatible GLOBAL variables assignment:
@@ -112,7 +112,7 @@ type Docopts struct {
112112
Exit_function bool
113113
}
114114

115-
// output bash 4 compatible assoc array, suitable for eval.
115+
// output bash 4+ compatible assoc array, suitable for eval.
116116
func (d *Docopts) Print_bash_args(bash_assoc string, args docopt.Opts) {
117117
// Reuse python's fake nested Bash arrays for repeatable arguments with values.
118118
// The structure is:
@@ -197,7 +197,7 @@ func To_bash(v interface{}) string {
197197
return s
198198
}
199199

200-
// Performs output for bash Globals (not bash 4 assoc) Names are mangled to become
200+
// Performs output for bash Globals (not bash 4+ assoc) Names are mangled to become
201201
// suitable for bash eval.
202202
// If Docopts.Mangle_key is false: simply print left-hand side assignment verbatim.
203203
// used for --no-mangle

docopts_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,14 @@ func TestPrint_bash_global(t *testing.T) {
270270
t.Errorf("Print_bash_global doesn't return nil for err: %v\n", err)
271271
}
272272
res := out.(*bytes.Buffer).String()
273-
expect := rewrite_prefix("ARGS", table.Expect_global)
273+
274+
var expect string
275+
if len(table.Expect_global_prefix) > 0 {
276+
// if special case was provided
277+
expect = strings.Join(table.Expect_global_prefix[:], "\n") + "\n"
278+
} else {
279+
expect = rewrite_prefix("ARGS", table.Expect_global)
280+
}
274281
if res != expect {
275282
t.Errorf("with prefix: Print_bash_global for '%v'\ngot: '%v'\nwant: '%v'\n", table.Input, res, expect)
276283
}

docs/developer.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type TestString struct {
127127
Input map[string]interface{}
128128
Expect_args []string
129129
Expect_global []string
130+
Expect_global_prefix []string // optional
130131
}
131132
```
132133

@@ -161,6 +162,8 @@ add a new bloc of JSON object (dict / hash / map):
161162
* `input` correspond to the `map[string]interface{}` of docopt parsed options.
162163
* `expect_args` the text rows of the associative array code for bash4 that is outputed by `Print_bash_args()` matched in order.
163164
* `expect_global` the text definition of the bash global vars that is outputed by `Print_bash_global()` matched in order.
165+
* `expect_global_prefix` [optional] if present will be used for testing `Mangle_key` + `Global_prefix` instead of [`rewrite_prefix("ARGS",)`](../docopts_test.go)
166+
So in `expect_global_prefix` the prefix must be `ARGS` + `_`.
164167

165168

166169
### testcases.docopt (agnostic test universal to docopt parsing language)
@@ -185,3 +188,33 @@ $ prog -a
185188
```
186189
* followed with the exptected output in JSON format (single ligne) (no empty line between `prog` call and expected JSON)
187190
* `\n` newline separator if some other call are added for the same `Usage:` definition
191+
192+
## Golang debugger
193+
194+
Debugger is a must for any programming language. Go provide an extrenal debugger named [delve](https://github.com/go-delve/delve)
195+
196+
https://github.com/go-delve/delve/tree/master/Documentation
197+
198+
In order to debug `docopts` you obviously need to pass command line argument to our program:
199+
Our arguments start after the first `--` which is `delve` argument stopper.
200+
201+
```
202+
cd path/to/docopts/source
203+
dlv debug -- -h 'Usage: prog dump [--] <unparsed_arguments>...' : dump -- -some -auto-approve FILENAME
204+
```
205+
206+
The as usual in debugger:
207+
208+
```
209+
# put break point in main function
210+
b main.main
211+
continue
212+
n
213+
s # for steping into the current function
214+
# printing some value
215+
p some_varialbles
216+
```
217+
218+
The debugger will then magically bring you step after step to the bug!
219+
Enjoy! and promote dubugger in evrery programming language and every programming course. :wink:
220+

docs/release.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ Step to produce a release.
44

55
## 1. prepare the release
66

7-
copy the previous ./VERSION to ./tests/VERSION
7+
copy the previous `./VERSION` to `./tests/VERSION`
88

99
```
1010
cp ./VERSION ./tests/VERSION
1111
git add ./tests/VERSION
1212
```
1313

14-
Increment the ./VERSION number
14+
Increment the `./VERSION` number
1515

16-
edit ./deployment.yml
16+
edit `./deployment.yml`
1717

1818
- add the new tag name release matching ./VERSION
1919
- the description you want, changes etc.
@@ -30,6 +30,7 @@ make test
3030
Ensure README.md is commited and rebuild it too
3131

3232
```
33+
touch build_doc.sh
3334
make README.md
3435
```
3536

examples/legacy_bash/cat-n_wrapper_example.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Usage: cat-n_wrapper_example.sh [--count=N] FILE...
66
#
77
# Arguments:
8-
# FILE input file
8+
# FILE input file, if FILLE equal - stdin is used instead.
99
#
1010
# Options:
1111
# --count=N limit the number of line to display

0 commit comments

Comments
 (0)