Skip to content

Commit e4ff594

Browse files
author
Sylvain Viart
committed
Merge branch 'docopts-go'
2 parents b441420 + 07b2a0a commit e4ff594

19 files changed

+380
-19
lines changed

.travis.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
language: go
2+
os:
3+
- linux
4+
- osx
25
go:
36
- "1.10.1"
47
before_install:
8+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
9+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install bash; fi
10+
- bash --version ; type bash
511
- go get github.com/docopt/docopt-go
12+
# get our official repos to
13+
- go get github.com/docopt/docopts
614
branches:
715
only:
816
- docopts-go
17+
- master
918
notifications:
1019
email:
1120
on_success: never
1221
script:
13-
- go build docopts.go
14-
- go test -v .
15-
- python language_agnostic_tester.py ./testee.sh
22+
- make test

PROGRESS.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# current work in PROGRRESS
22

3-
finish examples/
4-
make repository cleanup
3+
old style loop bash examples
54

65
# next
76

8-
## CI
9-
integration with automated tests
7+
## release and binary
8+
9+
publish release and pre-build binaries
1010

1111
## provide test on old environment
1212

1313
docker?
1414
32bist
1515
bash 3
16+

TODO.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ See [API_proposal.md](API_proposal.md)
1212

1313
## build and publish binary
1414

15-
reuse build.sh to build golang binary and pubilsh it as a new release too.
15+
Reuse build.sh to build golang binary and pubilsh it as a new release too.
1616

1717
## generate bash completion from usage
1818

@@ -22,7 +22,7 @@ docopts -h "$help" --generate-completion
2222

2323
## embed test routine (validation)?
2424

25-
may we cat interract with the caller to eval some validation…
25+
May we can interract with the caller to eval some validation…
2626
It is needed? Is it our goal?
2727

2828
```
@@ -38,3 +38,6 @@ if docopts test -- num:gt:1:--count file_exists:INFILE
3838
## config file parse config to option format
3939

4040
À la nslcd… ?
41+
42+
* json merge
43+
* toml merge (ini)

docopts.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
# vim: set et sw=4 ts=4 sts=4:
33
#
44
# docopts helper for bash
@@ -125,7 +125,7 @@ docopt_print_ARGS() {
125125
# inspired by:
126126
# https://stackoverflow.com/questions/6660010/bash-how-to-assign-an-associative-array-to-another-variable-name-e-g-rename-t#8881121
127127
declare -A myassoc
128-
eval $(typeset -A -p $tab|sed "s/ $assoc=/ myassoc=/")
128+
eval $(typeset -A -p $assoc|sed "s/ $assoc=/ myassoc=/")
129129

130130
# loop on keys
131131
echo "docopt_print_ARGS => $assoc"

examples/legacy_bash/sshdiff.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
#
3+
# Show file differences between 2 hosts.
4+
# Usage: sshdiff.sh [-h] [-s] <host1> <host2> <file> [<lines_context>]
5+
#
6+
# If not specified, <lines_context> defaults to 3.
7+
#
8+
# Use colordiff if available.
9+
#
10+
# Options:
11+
# -h display this help and exit
12+
# -s use sort instead of cat to show remote <file>
13+
#
14+
# Examples:
15+
# sshdiff.sh server1 hostname2 /etc/hostname
16+
17+
PATH=../..:$PATH
18+
source docopts.sh --auto "$@"
19+
20+
docopt_print_ARGS
21+
22+

examples/naval_fate.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
# --drifting Drifting mine.
1818
#
1919

20-
VERSION='Naval Fate 2.0'
2120

2221
# if docopts is in PATH, not needed.
2322
# Note: docopts.sh is also found in PATH
2423
PATH=..:$PATH
25-
# auto parse the header above, See: docopt_get_help_string
26-
source docopts.sh --auto "$@"
24+
25+
VERSION='Naval Fate 2.0'
26+
source docopts.sh
27+
# no vesion support in docopt_auto_parse() so we call docopts directly
28+
usage=$(docopt_get_help_string "$0")
29+
eval "$(docopts -A ARGS -V "$VERSION" -h "$usage" : "$@")"
2730

2831
docopt_print_ARGS
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
usage() {
4+
cat << EOU
5+
Usage: arguments_example.sh [-vqrh] [FILE] ...
6+
arguments_example.sh (--left | --right) CORRECTION FILE
7+
8+
Process FILE and optionally apply correction to either left-hand side or
9+
right-hand side.
10+
11+
Arguments:
12+
FILE optional input file
13+
CORRECTION correction angle, needs FILE, --left or --right to be present
14+
15+
Options:
16+
-h --help
17+
-v verbose mode
18+
-q quiet mode
19+
-r make report
20+
--left use left-hand side
21+
--right use right-hand side
22+
EOU
23+
}
24+
25+
# not needed if docopts already in PATH
26+
PATH=../..:$PATH
27+
eval "$(docopts -A ARGS -h "$(usage)" : "$@")"
28+
29+
# main code
30+
# on assoc array '!' before nane gike hash keys
31+
for a in ${!ARGS[@]} ; do
32+
echo "$a = ${ARGS[$a]}"
33+
done
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
usage() {
3+
cat << EOU
4+
Not a serious example.
5+
6+
Usage:
7+
calculator_example.sh <value> ( ( + | - | * | / ) <value> )...
8+
calculator_example.sh <function> <value> [( , <value> )]...
9+
calculator_example.sh (-h | --help)
10+
11+
Examples:
12+
calculator_example.sh 1 + 2 + 3 + 4 + 5
13+
calculator_example.sh 1 + 2 '*' 3 / 4 - 5 # note quotes around '*'
14+
calculator_example.sh sum 10 , 20 , 30 , 40
15+
16+
Options:
17+
-h, --help
18+
19+
Example:
20+
./calculator_example.sh 30 + 23 - 22
21+
EOU
22+
}
23+
24+
# not needed if docopts is in PATH
25+
PATH=../..:$PATH
26+
version='0.1'
27+
parsed=$(docopts -A args -h "$(usage)" -V $version : "$@")
28+
echo "$parsed"
29+
echo "================================================================================"
30+
eval "$parsed"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
usage() {
3+
cat << EOU
4+
5+
cat -n all files
6+
7+
Usage: cat-n_wrapper_example.sh [--count=N] FILE...
8+
9+
Arguments:
10+
FILE input file
11+
12+
Options:
13+
--count=N limit the number of line to display
14+
15+
Examples:
16+
./cat-n_wrapper.sh --count=3 cat-n_wrapper.sh quick_example.sh
17+
EOU
18+
}
19+
20+
21+
# no PATH changes required if docopts binary is in the PATH already
22+
PATH=../..:$PATH
23+
help=$(usage)
24+
version='0.1'
25+
26+
parsed=$(docopts -A args -h "$help" -V $version : "$@")
27+
#echo "$parsed"
28+
eval "$parsed"
29+
30+
cat_limit() {
31+
if [[ -z "${args[--count]}" ]] ; then
32+
cat -n "$1"
33+
else
34+
cat -n "$1" | head -"${args[--count]}"
35+
fi
36+
}
37+
38+
# current docopts multiple argument wrapper
39+
n=${args[FILE,#]}
40+
for i in $(seq 0 $(($n - 1)))
41+
do
42+
f="${args[FILE,$i]}"
43+
echo "----- $f -------"
44+
cat_limit "$f"
45+
done
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
usage() {
4+
cat << EOU
5+
Usage: counted_example.sh --help
6+
counted_example.sh -v...
7+
counted_example.sh go [go]
8+
counted_example.sh (--path=<path>)...
9+
counted_example.sh <file> <file>
10+
11+
Try: counted_example.sh -vvvvvvvvvv
12+
counted_example.sh go go
13+
counted_example.sh --path ./here --path ./there
14+
counted_example.sh this.txt that.txt
15+
EOU
16+
}
17+
18+
# if docopts is in PATH, not needed.
19+
PATH=../..:$PATH
20+
eval "$(docopts -A ARGS -h "$(usage)" : "$@")"
21+
22+
# docopt_auto_parse use ARGS bash 4 global assoc array
23+
# main code
24+
# on assoc array '!' before nane gike hash keys
25+
for a in ${!ARGS[@]} ; do
26+
echo "$a = ${ARGS[$a]}"
27+
done

0 commit comments

Comments
 (0)