summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2019-12-01 01:22:05 -0800
committerGitHub <[email protected]>2019-12-01 01:22:05 -0800
commitced05bfe8a0f966d6ed09af656f5410bc5f4ed7c (patch)
tree75a1540bde1cf3b46b45854e5043ca2100638888
parentc0c7a3ba8a1854cd85e65cca4f0e2028698a0738 (diff)
parent9d4521ce8be4871b3ba85cb1bfa96f4b074f505b (diff)
Merge pull request #96 from Andrew-Morozko/master
Added the "placeholder" tag
-rw-r--r--README.md28
-rw-r--r--example_test.go32
-rw-r--r--parse.go32
-rw-r--r--parse_test.go11
-rw-r--r--usage.go9
5 files changed, 95 insertions, 17 deletions
diff --git a/README.md b/README.md
index 1f02559..037d8d9 100644
--- a/README.md
+++ b/README.md
@@ -335,6 +335,34 @@ $ ./example
main.NameDotName{Head:"file", Tail:"txt"}
```
+### Custom placeholders
+
+Use the `placeholder` tag to control which placeholder text is used in the usage text.
+
+```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
+```
+
### Description strings
```go
diff --git a/example_test.go b/example_test.go
index f71fbeb..ea123e4 100644
--- a/example_test.go
+++ b/example_test.go
@@ -135,6 +135,38 @@ func Example_helpText() {
// --help, -h display this help and exit
}
+// This example shows the usage string generated by go-arg with customized placeholders
+func Example_helpPlaceholder() {
+ // These are the args you would pass in on the command line
+ os.Args = split("./example --help")
+
+ 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"`
+ }
+
+ // This is only necessary when running inside golang's runnable example harness
+ osExit = func(int) {}
+
+ MustParse(&args)
+
+ // output:
+
+ // 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
+}
+
// This example shows the usage string generated by go-arg when using subcommands
func Example_helpTextWithSubcommand() {
// These are the args you would pass in on the command line
diff --git a/parse.go b/parse.go
index bc156df..3fcf08a 100644
--- a/parse.go
+++ b/parse.go
@@ -44,18 +44,19 @@ func (p path) Child(child string) path {
// spec represents a command line option
type spec struct {
- dest path
- typ reflect.Type
- long string
- short string
- multiple bool
- required bool
- positional bool
- separate bool
- help string
- env string
- boolean bool
- defaultVal string // default value for this option
+ dest path
+ typ reflect.Type
+ long string
+ short string
+ multiple bool
+ required bool
+ positional bool
+ separate bool
+ help string
+ env string
+ boolean bool
+ defaultVal string // default value for this option
+ placeholder string // name of the data in help
}
// command represents a named subcommand, or the top-level command
@@ -342,6 +343,13 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
}
}
+ placeholder, hasPlaceholder := field.Tag.Lookup("placeholder")
+ 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
// wait until ParseValue because it means that a program with invalid argument
// fields will always fail regardless of whether the arguments it received
diff --git a/parse_test.go b/parse_test.go
index 47e9ccd..f75d1a7 100644
--- a/parse_test.go
+++ b/parse_test.go
@@ -220,6 +220,17 @@ func TestLongFlag(t *testing.T) {
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) {
var args struct {
Lower bool `arg:"-v"`
diff --git a/usage.go b/usage.go
index 43db703..57935fd 100644
--- a/usage.go
+++ b/usage.go
@@ -76,17 +76,16 @@ func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) {
for _, spec := range positionals {
// prefix with a space
fmt.Fprint(w, " ")
- up := strings.ToUpper(spec.long)
if spec.multiple {
if !spec.required {
fmt.Fprint(w, "[")
}
- fmt.Fprintf(w, "%s [%s ...]", up, up)
+ fmt.Fprintf(w, "%s [%s ...]", spec.placeholder, spec.placeholder)
if !spec.required {
fmt.Fprint(w, "]")
}
} else {
- fmt.Fprint(w, up)
+ fmt.Fprint(w, spec.placeholder)
}
}
fmt.Fprint(w, "\n")
@@ -134,7 +133,7 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) {
if len(positionals) > 0 {
fmt.Fprint(w, "\nPositional arguments:\n")
for _, spec := range positionals {
- printTwoCols(w, strings.ToUpper(spec.long), spec.help, "")
+ printTwoCols(w, spec.placeholder, spec.help, "")
}
}
@@ -180,7 +179,7 @@ func synopsis(spec *spec, form string) string {
if spec.boolean {
return form
}
- return form + " " + strings.ToUpper(spec.long)
+ return form + " " + spec.placeholder
}
func ptrTo(s string) *string {