Added the "dataname" tag

This commit is contained in:
Andrew Morozko 2019-11-29 22:33:16 +03:00
parent c0c7a3ba8a
commit 904e039267
3 changed files with 47 additions and 5 deletions

View File

@ -135,6 +135,42 @@ 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
func Example_helpDataname() {
// These are the args you would pass in on the command line
os.Args = split("./example --help")
var args struct {
Input string `arg:"positional,dataname:DATAFILE"`
Output []string `arg:"positional"`
Verbose bool `arg:"-v" help:"verbosity level"`
Dataset string `help:"dataset to use"`
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
osExit = func(int) {}
MustParse(&args)
// output:
// Usage: example [--verbose] [--dataset DATASET] [--optimize LEVEL] [--maxjobs N] DATAFILE [OUTPUT [OUTPUT ...]]
// Positional arguments:
// DATAFILE
// OUTPUT
// Options:
// --verbose, -v verbosity level
// --dataset DATASET dataset to use
// --optimize LEVEL, -O LEVEL
// optimization level
// --maxjobs N, -j N maximum number of simultanious jobs
// --help, -h display this help and exit
}
// This example shows the usage string generated by go-arg when using subcommands // This example shows the usage string generated by go-arg when using subcommands
func Example_helpTextWithSubcommand() { func Example_helpTextWithSubcommand() {
// These are the args you would pass in on the command line // These are the args you would pass in on the command line

View File

@ -56,6 +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
} }
// command represents a named subcommand, or the top-level command // command represents a named subcommand, or the top-level command
@ -305,6 +306,8 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
spec.required = true spec.required = true
case key == "positional": case key == "positional":
spec.positional = true spec.positional = true
case key == "dataname":
spec.dataname = value
case key == "separate": case key == "separate":
spec.separate = true spec.separate = true
case key == "help": // deprecated case key == "help": // deprecated
@ -342,6 +345,10 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
} }
} }
if spec.dataname == "" {
spec.dataname = 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
// wait until ParseValue because it means that a program with invalid argument // wait until ParseValue because it means that a program with invalid argument
// fields will always fail regardless of whether the arguments it received // fields will always fail regardless of whether the arguments it received

View File

@ -76,17 +76,16 @@ func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) {
for _, spec := range positionals { for _, spec := range positionals {
// prefix with a space // prefix with a space
fmt.Fprint(w, " ") fmt.Fprint(w, " ")
up := strings.ToUpper(spec.long)
if spec.multiple { if spec.multiple {
if !spec.required { if !spec.required {
fmt.Fprint(w, "[") fmt.Fprint(w, "[")
} }
fmt.Fprintf(w, "%s [%s ...]", up, up) fmt.Fprintf(w, "%s [%s ...]", spec.dataname, spec.dataname)
if !spec.required { if !spec.required {
fmt.Fprint(w, "]") fmt.Fprint(w, "]")
} }
} else { } else {
fmt.Fprint(w, up) fmt.Fprint(w, spec.dataname)
} }
} }
fmt.Fprint(w, "\n") fmt.Fprint(w, "\n")
@ -134,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, strings.ToUpper(spec.long), spec.help, "") printTwoCols(w, spec.dataname, spec.help, "")
} }
} }
@ -180,7 +179,7 @@ func synopsis(spec *spec, form string) string {
if spec.boolean { if spec.boolean {
return form return form
} }
return form + " " + strings.ToUpper(spec.long) return form + " " + spec.dataname
} }
func ptrTo(s string) *string { func ptrTo(s string) *string {