Skip to content

Commit a438dae

Browse files
authored
doc: added usage examples to README.md (#25)
1 parent 2fd4761 commit a438dae

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

README.md

+108
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
A library to parse command line arguments for Golang application.
44

55
- [Import this package](#import)
6+
- [Usage examples](#usage)
67
- [Supporting Go versions](#support-go-version)
78
- [License](#license)
89

@@ -13,6 +14,113 @@ A library to parse command line arguments for Golang application.
1314
import "github.com/sttk-go/cliargs"
1415
```
1516

17+
18+
<a name="usage"></a>
19+
## Usage examplee
20+
21+
This library provides 3 functions to parse command line arguments, and provides a function to print help a text.
22+
23+
### 1. Parse CLI arguments without configurations
24+
25+
```
26+
// os.Args[1:] = [--foo-bar=A -a --baz -bc=3 qux -c=4 quux]
27+
28+
args, err := cliargs.Parse()
29+
args.HasOpt("a") // true
30+
args.HasOpt("b") // true
31+
args.HasOpt("c") // true
32+
args.HasOpt("foo-bar") // true
33+
args.HasOpt("baz") // true
34+
args.OptParam("c") // 3
35+
args.OptParams("c") // [3 4]
36+
args.OptParam("foo-bar") // A
37+
args.OptParams("foo-bar") // [A]
38+
args.CmdParams() // [qux quux]
39+
```
40+
41+
### 2. Parse CLI arguments with configurations
42+
43+
```
44+
osArgs := []string{"--foo-bar", "qux", "--baz", "1", "-z=2", "-X", "quux"}
45+
optCfgs := []cliargs.OptCfg{
46+
cliargs.OptCfg{Name:"foo-bar", Desc:"This is description of foo-bar."},
47+
cliargs.OptCfg{
48+
Name:"baz", Aliases:[]string{"z"}, HasParam:true, IsArray: true,
49+
Desc:"This is description of baz.", AtParam:"<text>",
50+
},
51+
cliargs.OptCfg{Name:"*"},
52+
}
53+
54+
args, err := cliargs.Parseith(osArgs, optCfgs)
55+
args.HasOpt("foo-bar") // true
56+
args.HasOpt("baz") // true
57+
args.HasOpt("X") // true, due to "*" config
58+
args.OptParam("baz") // 1
59+
args.OptParams("baz") // [1 2]
60+
args.CmdParams() // [qux quux]
61+
```
62+
63+
#### Print a help text of the above `optCfgs`
64+
65+
```
66+
wrapOpts := WrapOpts{}
67+
usage := "This is the usage description."
68+
cliargs.PrintHelp(optCfgs, wrapOpts)
69+
```
70+
(stdout)
71+
```
72+
This is the usage description.
73+
--foo-bar, -f This is description of foo-bar.
74+
--baz, -b <text> This is description of baz.
75+
```
76+
77+
78+
### 3. Parse CLI arguments for an option store with struct tags
79+
80+
```
81+
type Options struct {
82+
FooBar bool `opt:foo-bar,f" optdesc:"This is FooBar.`
83+
Baz int `opt:baz,b=99" optdesc:"This is Baz." optparam:"<num>"`
84+
Qux string `opt:"=XXX" optdesc:"This is Qux." optparam:"<text>"`
85+
Quux []string `opt:"quux=[A,B,C]" optdesc:"This is Quux."`
86+
Corge []int
87+
}
88+
options := Options{}
89+
90+
os.Args := []string{
91+
"--foo-bar", "c1", "-b", "12", "--Qux", "ABC", "c2",
92+
"--Corge", "20", "--Corge=21",
93+
}
94+
95+
cmdParams, optCfgs, err := cliargs.ParseFor(osArgs, &options)
96+
cmdParams // [c1 c2]
97+
options.FooBar // true
98+
options.Baz // 12
99+
options.Qux // ABC
100+
options.Quux // [A B C]
101+
options.Corge // [20 21]
102+
```
103+
104+
#### Print a help text of the above `optCfgs`
105+
106+
```
107+
wrapOpts := WrapOpts{}
108+
usage := "This is the usage description.\n\nOPTIONS:"
109+
cliargs.PrintHelp(optCfgs, wrapOpts)
110+
```
111+
(stdout)
112+
```
113+
This is the usage description.
114+
115+
OPTIONS:
116+
--foo-bar, -f This is FooBar.
117+
--baz, -b <num> This is Baz.
118+
--Qux <text> This is Qux.
119+
--quux This is Quux.
120+
--Corge
121+
```
122+
123+
16124
<a name="support-go-versions"></a>
17125
## Supporting Go versions
18126

0 commit comments

Comments
 (0)