separate help into WriteUsage and WriteHelp
This commit is contained in:
parent
bcb41ba048
commit
60f2612c0c
28
README.md
28
README.md
|
@ -19,17 +19,6 @@ $ ./example --foo=hello --bar
|
||||||
hello true
|
hello true
|
||||||
```
|
```
|
||||||
|
|
||||||
### Default values
|
|
||||||
|
|
||||||
```go
|
|
||||||
var args struct {
|
|
||||||
Foo string
|
|
||||||
Bar bool
|
|
||||||
}
|
|
||||||
args.Foo = "default value"
|
|
||||||
arg.MustParse(&args)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Required arguments
|
### Required arguments
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -40,6 +29,12 @@ var args struct {
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ ./example
|
||||||
|
usage: example --foo FOO [--bar]
|
||||||
|
error: --foo is required
|
||||||
|
```
|
||||||
|
|
||||||
### Positional arguments
|
### Positional arguments
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -86,6 +81,17 @@ options:
|
||||||
--help, -h print this help message
|
--help, -h print this help message
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Default values
|
||||||
|
|
||||||
|
```go
|
||||||
|
var args struct {
|
||||||
|
Foo string
|
||||||
|
Bar bool
|
||||||
|
}
|
||||||
|
args.Foo = "default value"
|
||||||
|
arg.MustParse(&args)
|
||||||
|
```
|
||||||
|
|
||||||
### Arguments with multiple values
|
### Arguments with multiple values
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
|
|
4
parse.go
4
parse.go
|
@ -64,11 +64,11 @@ func MustParse(dest ...interface{}) {
|
||||||
p, err := NewParser(dest...)
|
p, err := NewParser(dest...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
err = p.Parse(os.Args[1:])
|
err = p.Parse(os.Args[1:])
|
||||||
if err == ErrHelp {
|
if err == ErrHelp {
|
||||||
p.WriteUsage(os.Stdout)
|
p.WriteHelp(os.Stdout)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
26
usage.go
26
usage.go
|
@ -11,9 +11,9 @@ import (
|
||||||
|
|
||||||
// Fail prints usage information to stdout and exits with non-zero status
|
// Fail prints usage information to stdout and exits with non-zero status
|
||||||
func (p *Parser) Fail(msg string) {
|
func (p *Parser) Fail(msg string) {
|
||||||
fmt.Println(msg)
|
|
||||||
p.WriteUsage(os.Stdout)
|
p.WriteUsage(os.Stdout)
|
||||||
os.Exit(1)
|
fmt.Println("error:", msg)
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteUsage writes usage information to the given writer
|
// WriteUsage writes usage information to the given writer
|
||||||
|
@ -29,7 +29,7 @@ func (p *Parser) WriteUsage(w io.Writer) {
|
||||||
|
|
||||||
fmt.Fprintf(w, "usage: %s ", filepath.Base(os.Args[0]))
|
fmt.Fprintf(w, "usage: %s ", filepath.Base(os.Args[0]))
|
||||||
|
|
||||||
// write the option component of the one-line usage message
|
// write the option component of the usage message
|
||||||
for _, spec := range options {
|
for _, spec := range options {
|
||||||
if !spec.required {
|
if !spec.required {
|
||||||
fmt.Fprint(w, "[")
|
fmt.Fprint(w, "[")
|
||||||
|
@ -41,7 +41,7 @@ func (p *Parser) WriteUsage(w io.Writer) {
|
||||||
fmt.Fprint(w, " ")
|
fmt.Fprint(w, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// write the positional component of the one-line usage message
|
// write the positional component of the usage message
|
||||||
for _, spec := range positionals {
|
for _, spec := range positionals {
|
||||||
up := strings.ToUpper(spec.long)
|
up := strings.ToUpper(spec.long)
|
||||||
if spec.multiple {
|
if spec.multiple {
|
||||||
|
@ -52,6 +52,20 @@ func (p *Parser) WriteUsage(w io.Writer) {
|
||||||
fmt.Fprint(w, " ")
|
fmt.Fprint(w, " ")
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, "\n")
|
fmt.Fprint(w, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteHelp writes the usage string followed by the full help string for each option
|
||||||
|
func (p *Parser) WriteHelp(w io.Writer) {
|
||||||
|
var positionals, options []*spec
|
||||||
|
for _, spec := range p.spec {
|
||||||
|
if spec.positional {
|
||||||
|
positionals = append(positionals, spec)
|
||||||
|
} else {
|
||||||
|
options = append(options, spec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.WriteUsage(w)
|
||||||
|
|
||||||
// write the list of positionals
|
// write the list of positionals
|
||||||
if len(positionals) > 0 {
|
if len(positionals) > 0 {
|
||||||
|
@ -66,9 +80,9 @@ func (p *Parser) WriteUsage(w io.Writer) {
|
||||||
fmt.Fprint(w, "\noptions:\n")
|
fmt.Fprint(w, "\noptions:\n")
|
||||||
const colWidth = 25
|
const colWidth = 25
|
||||||
for _, spec := range options {
|
for _, spec := range options {
|
||||||
left := fmt.Sprint(synopsis(spec, "--"+spec.long))
|
left := " " + synopsis(spec, "--"+spec.long)
|
||||||
if spec.short != "" {
|
if spec.short != "" {
|
||||||
left += ", " + fmt.Sprint(synopsis(spec, "-"+spec.short))
|
left += ", " + synopsis(spec, "-"+spec.short)
|
||||||
}
|
}
|
||||||
fmt.Print(left)
|
fmt.Print(left)
|
||||||
if spec.help != "" {
|
if spec.help != "" {
|
||||||
|
|
Loading…
Reference in New Issue