add documentation and examples showing how to override the short and long option names together

This commit is contained in:
Alex Flint 2020-08-06 16:41:45 -07:00
parent a68c3d0653
commit c47edd0324
2 changed files with 27 additions and 4 deletions

View File

@ -125,7 +125,7 @@ Workers: [1 99]
var args struct { var args struct {
Input string `arg:"positional"` Input string `arg:"positional"`
Output []string `arg:"positional"` Output []string `arg:"positional"`
Verbose bool `arg:"-v" help:"verbosity level"` Verbose bool `arg:"-v,--verbose" help:"verbosity level"`
Dataset string `help:"dataset to use"` Dataset string `help:"dataset to use"`
Optimize int `arg:"-O" help:"optimization level"` Optimize int `arg:"-O" help:"optimization level"`
} }
@ -240,6 +240,29 @@ $ ./example --version
someprogram 4.3.0 someprogram 4.3.0
``` ```
### Overriding option names
```go
var args struct {
Short string `arg:"-s"`
Long string `arg:"--custom-long-option"`
ShortAndLong string `arg:"-x,--my-option"`
}
arg.MustParse(&args)
```
```shell
$ ./example --help
Usage: [--short SHORT] [--custom-long-option CUSTOM-LONG-OPTION] [--my-option MY-OPTION]
Options:
--short SHORT, -s SHORT
--custom-long-option CUSTOM-LONG-OPTION
--my-option MY-OPTION, -x MY-OPTION
--help, -h display this help and exit
```
### Embedded structs ### Embedded structs
The fields of embedded structs are treated just like regular fields: The fields of embedded structs are treated just like regular fields:

View File

@ -112,7 +112,7 @@ func Example_helpText() {
Output []string `arg:"positional"` Output []string `arg:"positional"`
Verbose bool `arg:"-v" help:"verbosity level"` Verbose bool `arg:"-v" help:"verbosity level"`
Dataset string `help:"dataset to use"` Dataset string `help:"dataset to use"`
Optimize int `arg:"-O,help:optimization level"` Optimize int `arg:"-O,--optim" help:"optimization level"`
} }
// This is only necessary when running inside golang's runnable example harness // This is only necessary when running inside golang's runnable example harness
@ -121,7 +121,7 @@ func Example_helpText() {
MustParse(&args) MustParse(&args)
// output: // output:
// Usage: example [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]] // Usage: example [--verbose] [--dataset DATASET] [--optim OPTIM] INPUT [OUTPUT [OUTPUT ...]]
// //
// Positional arguments: // Positional arguments:
// INPUT // INPUT
@ -130,7 +130,7 @@ func Example_helpText() {
// Options: // Options:
// --verbose, -v verbosity level // --verbose, -v verbosity level
// --dataset DATASET dataset to use // --dataset DATASET dataset to use
// --optimize OPTIMIZE, -O OPTIMIZE // --optim OPTIM, -O OPTIM
// optimization level // optimization level
// --help, -h display this help and exit // --help, -h display this help and exit
} }