Skip to content

Commit

Permalink
pr52 add more document + introduce output for -G
Browse files Browse the repository at this point in the history
-G mode now support both mangling `-` and `--`
respectively:

```
ARGS__=true
ARGS___=true
```
  • Loading branch information
Sylvain Viart committed Sep 14, 2021
1 parent 6ca39df commit 8e8c077
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
11 changes: 1 addition & 10 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
# Change history

This file didn't exist before v0.6.4-with-no-mangle-double-dash
See deployment.yml

## v0.6.4-with-no-mangle-double-dash

* `language_agnostic_tester.py` write for python3
* sort argument key in `Print_bash_args()` `Print_bash_global()`
* sort input keys in `docopts_test.go`
* add PR52 test to `testcases.docopt`
* complete developper's documentation
* #52 ignore `[--]` double-dash option in `Print_bash_global()`
* let reformat Go code with gofmt indent change Space ==> Tab
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Most concepts are documented in the `docopt` (without S) manual - see [docopt.or
Many examples use associative arrays in bash 4.x, but there is legacy support for bash 3.2 on macOS (OS X) or legacy
GNU/Linux OS.

[make README.md]: # (./docopts --version | get_version "This is a transitional release:")
[make README.md]: # (./docopts --version | get_version "This is a bug fix release:")

```
This is a transitional release: v0.6.3-rc2
This is a bug fix release: v0.6.4-with-no-mangle-double-dash
```

This release will be maintained for compatibility, only fixes will be provided. The 0.6.3 version is fully compatible with
the previous version of `docopts`.
This release will be maintained for compatibility, only fixes will be provided. The 0.6.4 version is fully compatible with
the previous version of `docopts`. Except for `-` handling in global mode, which produces an error.

## SYNOPSIS

Expand Down Expand Up @@ -72,9 +72,31 @@ code. Double-dash `--` will be kept.
Which makes it easy to filter variable with `grep` or such or to avoid globals
name collision.

Handling `[-]` in global mode is not supported and raises an error when trying to mangle `-`.
But works for any other modes including `-G`.
```
./docopts -h 'Usage: dump [-]' : -
docopts:error: Print_bash_global:Mangling not supported for: '-'
```

### Associative Array mode
Single-dash can be catch easily by reading it in `FILENAME` parameter:

```bash
./docopts -h 'Usage: prog parse FILENAME' : parse -
parse=true
FILENAME='-'
```

then in your code:

```bash
f="$FILENAME"
if [[ $f == '-' ]] ; then
f=/dev/stdin
fi
```

### Associative Array mode

Alternatively, `docopts` can be invoked with the `-A <name>` option, which
stores the parsed arguments as fields of a Bash 4 associative array called
Expand All @@ -88,7 +110,8 @@ they are faked for repeatable arguments with the following access syntax:
${args[ARG,1]} # the second argument to ARG, etc.
```

Associative mode don't skipp double-dash `--` it is part of the keys.
Associative mode don't skipp double-dash `--` it will be part of the keys
as boolean value present or not.

### How arguments are associated to variables

Expand Down
16 changes: 5 additions & 11 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@

* ~~add unit test for corner case (`mangle_name` removes option `[--]`)~~
* ~~document how to add unittest in Go~~
* document behavior parsing `--` `[--]` in global mode
* add examples of `[--]` usage
```
$ ./docopts -h 'Usage: prog double-dash [-p] [-o] [--] <unparsed_option>...' : double-dash -- one -p -auto-approve two
o=false
unparsed_option=('one' '-p' '-auto-approve' 'two')
double_dash=true
p=false
```
* ~~document behavior parsing `--` `[--]` in global mode~~
* ~~add examples of `[--]` usage in global mode~~
* ~~add `language_agnostic_tester.py` input test~~
* better error message from `docopts`
* ~~better error message from `docopts`~~ no so better
* also document `delv` debugger usage in delevopper's doc
* update comment about version 0.6.3 and python compatibility
* ~~update comment about version 0.6.3 and python compatibility~~
* test python've version behavior with our input
* add example of usage for handling `-` (stdin argument)
* add functional testcase in bats with corner case `--no-mangle` `-G` etc.

## better error handling

Expand Down
19 changes: 19 additions & 0 deletions deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ releases:
# yaml keys must match the git tag
##################################

v0.6.4-with-no-mangle-double-dash:
name: bug fix v0.6.4-with-no-mangle-double-dash
description: |
This is a bug fix release.
features changes:
- fix error refusing mangling double dash in global mode [#52]
- still refuse to mangle single dash `[-]` in global mode
- now can mangle single dash in `-G` mode
internal changes:
- `language_agnostic_tester.py` re-write for python3
- sort argument key in `Print_bash_args()` `Print_bash_global()`
- sort input keys in `docopts_test.go`
- add PR52 test to `testcases.docopt`
- completed developper's documentation
- #52 ignore `[--]` double-dash option in `Print_bash_global()`
- reformat of Go code with gofmt indent change Space ==> Tab
v0.6.3-rc2:
name: "docopts binary transitional v0.6.3-rc2"
description: |
Expand Down
5 changes: 3 additions & 2 deletions docopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (d *Docopts) Print_bash_global(args docopt.Opts) error {
// so value is an interface{}
for _, key := range Sort_args_keys(args) {
if d.Mangle_key {
if key == "--" {
if key == "--" && d.Global_prefix == "" {
// skip double-dash that can't be mangled #52
// so double-dash is not printed for bash
// but still parsed by docopts
Expand Down Expand Up @@ -251,7 +251,7 @@ func (d *Docopts) Print_bash_global(args docopt.Opts) error {
func (d *Docopts) Name_mangle(elem string) (string, error) {
var v string

if elem == "-" || elem == "--" {
if d.Global_prefix == "" && (elem == "-" || elem == "--") {
return "", fmt.Errorf("Mangling not supported for: '%s'", elem)
}

Expand All @@ -262,6 +262,7 @@ func (d *Docopts) Name_mangle(elem string) (string, error) {
} else if Match(`^--.+$`, elem) {
v = elem[2:]
} else {
// also this case for '-' when d.Global_prefix != ""
v = elem
}

Expand Down
18 changes: 13 additions & 5 deletions examples/without_docopts_sh_lib/cat-n_wrapper_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ cat -n all files
Usage: cat-n_wrapper_example.sh [--count=N] FILE...
Arguments:
FILE input file
FILE input file, - is converted to stdin
Options:
--count=N limit the number of line to display
Examples:
./cat-n_wrapper.sh --count=3 cat-n_wrapper.sh quick_example.sh
./cat-n_wrapper_example.sh --count=3 cat-n_wrapper.sh quick_example.sh
# read from standard input with -
./cat-n_wrapper_example.sh --count=3 - < cat-n_wrapper_example.sh
EOU
}


# no PATH changes required if docopts binary is in the PATH already
PATH=../..:$PATH
help=$(usage)
version='0.1'
version='0.2'

parsed=$(docopts -A args -h "$help" -V $version : "$@")
#echo "$parsed"
Expand All @@ -36,10 +37,17 @@ cat_limit() {
}

# current docopts multiple argument wrapper
n=${args[FILE,#]}

# use intermediate variable FILE_DASH to help vim syntax highlighting
# you can put FILE,# as assoc key too.
FILE_DASH='FILE,#'
n=${args[$FILE_DASH]}
for i in $(seq 0 $(($n - 1)))
do
f="${args[FILE,$i]}"
if [[ $f == '-' ]] ; then
f=/dev/stdin
fi
echo "----- $f -------"
cat_limit "$f"
done

0 comments on commit 8e8c077

Please sign in to comment.