udpate readme
This commit is contained in:
parent
22c73471e6
commit
04e96c0c6b
14
README.md
14
README.md
|
@ -1,6 +1,8 @@
|
||||||
Argument parsing for Go.
|
# Argument parsing for Go
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
|
import "github.com/alexflint/go-arg"
|
||||||
|
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo string
|
Foo string
|
||||||
Bar bool
|
Bar bool
|
||||||
|
@ -16,7 +18,7 @@ hello
|
||||||
True
|
True
|
||||||
```
|
```
|
||||||
|
|
||||||
Setting defaults values:
|
### Default values
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
var args struct {
|
var args struct {
|
||||||
|
@ -27,7 +29,7 @@ args.Foo = "default value"
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
```
|
```
|
||||||
|
|
||||||
Marking options as required
|
### Marking options as required
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
var args struct {
|
var args struct {
|
||||||
|
@ -37,7 +39,7 @@ var args struct {
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
```
|
```
|
||||||
|
|
||||||
Positional argument:
|
### Positional argument
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
var args struct {
|
var args struct {
|
||||||
|
@ -56,7 +58,7 @@ Input: src.txt
|
||||||
Output: [x.out y.out z.out]
|
Output: [x.out y.out z.out]
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage strings:
|
### Usage strings
|
||||||
```bash
|
```bash
|
||||||
$ ./example -h
|
$ ./example -h
|
||||||
usage: [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--help] INPUT [OUTPUT [OUTPUT ...]]
|
usage: [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] [--help] INPUT [OUTPUT [OUTPUT ...]]
|
||||||
|
@ -73,7 +75,7 @@ options:
|
||||||
--help, -h print this help message
|
--help, -h print this help message
|
||||||
```
|
```
|
||||||
|
|
||||||
Options with multiple values:
|
### Options with multiple values
|
||||||
```
|
```
|
||||||
var args struct {
|
var args struct {
|
||||||
Database string
|
Database string
|
||||||
|
|
|
@ -4,11 +4,11 @@ import "github.com/alexflint/go-arg"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var args struct {
|
var args struct {
|
||||||
Input string `arg:"positional"`
|
Input string `arg:"positional"`
|
||||||
Output string `arg:"positional"`
|
Output []string `arg:"positional"`
|
||||||
Foo string `arg:"help:this argument is foo"`
|
Verbose bool `arg:"-v,help:verbosity level"`
|
||||||
VeryLongArgument int `arg:"help:this argument is very long"`
|
Dataset string `arg:"help:dataset to use"`
|
||||||
Bar float64 `arg:"-b"`
|
Optimize int `arg:"-O,help:optimization level"`
|
||||||
}
|
}
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
}
|
}
|
||||||
|
|
2
parse.go
2
parse.go
|
@ -38,7 +38,7 @@ func Parse(dest ...interface{}) error {
|
||||||
func ParseFrom(args []string, dest ...interface{}) error {
|
func ParseFrom(args []string, dest ...interface{}) error {
|
||||||
// Add the help option if one is not already defined
|
// Add the help option if one is not already defined
|
||||||
var internal struct {
|
var internal struct {
|
||||||
Help bool `arg:"-h"`
|
Help bool `arg:"-h,help:print this help message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the spec
|
// Parse the spec
|
||||||
|
|
2
usage.go
2
usage.go
|
@ -72,7 +72,7 @@ func writeUsage(w io.Writer, specs []*spec) {
|
||||||
for _, spec := range positionals {
|
for _, spec := range positionals {
|
||||||
up := strings.ToUpper(spec.long)
|
up := strings.ToUpper(spec.long)
|
||||||
if spec.multiple {
|
if spec.multiple {
|
||||||
fmt.Fprintf(w, "[%s [%s ...]]", up)
|
fmt.Fprintf(w, "[%s [%s ...]]", up, up)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprint(w, up)
|
fmt.Fprint(w, up)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue