Various changes

This commit is contained in:
Andrew Morozko 2019-11-30 00:22:21 +03:00
parent 904e039267
commit c3a019cdb8
5 changed files with 68 additions and 32 deletions

View File

@ -148,6 +148,32 @@ var args struct {
arg.MustParse(&args) arg.MustParse(&args)
``` ```
### Custom placeholders
```go
var args struct {
Input string `arg:"positional" placeholder:"SRC"`
Output []string `arg:"positional" placeholder:"DST"`
Optimize int `arg:"-O" help:"optimization level" placeholder:"LEVEL"`
MaxJobs int `arg:"-j" help:"maximum number of simultaneous jobs" placeholder:"N"`
}
arg.MustParse(&args)
```
```shell
$ ./example -h
Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]]
Positional arguments:
SRC
DST
Options:
--optimize LEVEL, -O LEVEL
optimization level
--maxjobs N, -j N maximum number of simultaneous jobs
--help, -h display this help and exit
```
### Default values (before v1.2) ### Default values (before v1.2)
```go ```go

View File

@ -135,18 +135,16 @@ func Example_helpText() {
// --help, -h display this help and exit // --help, -h display this help and exit
} }
// This example shows the usage string generated by go-arg with custom datanames // This example shows the usage string generated by go-arg with customized placeholders
func Example_helpDataname() { func Example_helpDataname() {
// These are the args you would pass in on the command line // These are the args you would pass in on the command line
os.Args = split("./example --help") os.Args = split("./example --help")
var args struct { var args struct {
Input string `arg:"positional,dataname:DATAFILE"` Input string `arg:"positional" placeholder:"SRC"`
Output []string `arg:"positional"` Output []string `arg:"positional" placeholder:"DST"`
Verbose bool `arg:"-v" help:"verbosity level"` Optimize int `arg:"-O" help:"optimization level" placeholder:"LEVEL"`
Dataset string `help:"dataset to use"` MaxJobs int `arg:"-j" help:"maximum number of simultaneous jobs" placeholder:"N"`
Optimize int `arg:"-O,help:optimization level,dataname:LEVEL"`
MaxJobs int `arg:"-j,help:maximum number of simultaneous jobs,dataname:N"`
} }
// This is only necessary when running inside golang's runnable example harness // This is only necessary when running inside golang's runnable example harness
@ -156,18 +154,16 @@ func Example_helpDataname() {
// output: // output:
// Usage: example [--verbose] [--dataset DATASET] [--optimize LEVEL] [--maxjobs N] DATAFILE [OUTPUT [OUTPUT ...]] // Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]]
// Positional arguments: // Positional arguments:
// DATAFILE // SRC
// OUTPUT // DST
// Options: // Options:
// --verbose, -v verbosity level
// --dataset DATASET dataset to use
// --optimize LEVEL, -O LEVEL // --optimize LEVEL, -O LEVEL
// optimization level // optimization level
// --maxjobs N, -j N maximum number of simultanious jobs // --maxjobs N, -j N maximum number of simultaneous jobs
// --help, -h display this help and exit // --help, -h display this help and exit
} }

View File

@ -56,7 +56,7 @@ type spec struct {
env string env string
boolean bool boolean bool
defaultVal string // default value for this option defaultVal string // default value for this option
dataname string // name of the data in help placeholder string // name of the data in help
} }
// command represents a named subcommand, or the top-level command // command represents a named subcommand, or the top-level command
@ -345,8 +345,11 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
} }
} }
if spec.dataname == "" { placeholder, hasPlaceholder := field.Tag.Lookup("placeholder")
spec.dataname = strings.ToUpper(spec.long) if hasPlaceholder {
spec.placeholder = placeholder
} else {
spec.placeholder = strings.ToUpper(spec.long)
} }
// Check whether this field is supported. It's good to do this here rather than // Check whether this field is supported. It's good to do this here rather than

View File

@ -220,6 +220,17 @@ func TestLongFlag(t *testing.T) {
assert.Equal(t, "xyz", args.Foo) assert.Equal(t, "xyz", args.Foo)
} }
func TestPlaceholder(t *testing.T) {
var args struct {
Input string `arg:"positional" placeholder:"SRC"`
Output []string `arg:"positional" placeholder:"DST"`
Optimize int `arg:"-O" placeholder:"LEVEL"`
MaxJobs int `arg:"-j" placeholder:"N"`
}
err := parse("-O 5 --maxjobs 2 src dest1 dest2", &args)
assert.NoError(t, err)
}
func TestCaseSensitive(t *testing.T) { func TestCaseSensitive(t *testing.T) {
var args struct { var args struct {
Lower bool `arg:"-v"` Lower bool `arg:"-v"`

View File

@ -80,12 +80,12 @@ func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) {
if !spec.required { if !spec.required {
fmt.Fprint(w, "[") fmt.Fprint(w, "[")
} }
fmt.Fprintf(w, "%s [%s ...]", spec.dataname, spec.dataname) fmt.Fprintf(w, "%s [%s ...]", spec.placeholder, spec.placeholder)
if !spec.required { if !spec.required {
fmt.Fprint(w, "]") fmt.Fprint(w, "]")
} }
} else { } else {
fmt.Fprint(w, spec.dataname) fmt.Fprint(w, spec.placeholder)
} }
} }
fmt.Fprint(w, "\n") fmt.Fprint(w, "\n")
@ -133,7 +133,7 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) {
if len(positionals) > 0 { if len(positionals) > 0 {
fmt.Fprint(w, "\nPositional arguments:\n") fmt.Fprint(w, "\nPositional arguments:\n")
for _, spec := range positionals { for _, spec := range positionals {
printTwoCols(w, spec.dataname, spec.help, "") printTwoCols(w, spec.placeholder, spec.help, "")
} }
} }
@ -179,7 +179,7 @@ func synopsis(spec *spec, form string) string {
if spec.boolean { if spec.boolean {
return form return form
} }
return form + " " + spec.dataname return form + " " + spec.placeholder
} }
func ptrTo(s string) *string { func ptrTo(s string) *string {