Skip to content

Commit b739fd8

Browse files
author
Anand
committed
ref issue akamensky#9 (of original argparse) - Adds a new Path field for metavar and provides a way to add author information at the end of the usage string
1 parent 6bd6f07 commit b739fd8

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

argparse.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var disableHelp = false
2020
type Command struct {
2121
name string
2222
description string
23+
authorinfo string
2324
args []*arg
2425
commands []*Command
2526
parsed bool
@@ -91,24 +92,28 @@ type Parser struct {
9192
// Options.Help - A help message to be displayed in Usage output. Can be of any length as the message will be
9293
// formatted to fit max screen width of 100 characters.
9394
//
95+
// Options.Path - A <meta> field describing the argument. For example <path> for a filename argument.
96+
//
9497
// Options.Default - A default value for an argument. This value will be assigned to the argument at the end of parsing
9598
// in case if this argument was not supplied on command line. File default value is a string which it will be open with
9699
// provided options. In case if provided value type does not match expected, the error will be returned on run-time.
97100
type Options struct {
98101
Required bool
99102
Validate func(args []string) error
100103
Help string
104+
Path string
101105
Default interface{}
102106
}
103107

104108
// NewParser creates new Parser object that will allow to add arguments for parsing
105109
// It takes program name and description which will be used as part of Usage output
106110
// Returns pointer to Parser object
107-
func NewParser(name string, description string) *Parser {
111+
func NewParser(name, description, authorinfo string) *Parser {
108112
p := &Parser{}
109113

110114
p.name = name
111115
p.description = description
116+
p.authorinfo = authorinfo
112117

113118
p.args = make([]*arg, 0)
114119
p.commands = make([]*Command, 0)
@@ -133,6 +138,7 @@ func (o *Command) NewCommand(name string, description string) *Command {
133138
c.description = description
134139
c.parsed = false
135140
c.parent = o
141+
136142
if !disableHelp {
137143
c.help("h", "help")
138144
c.exitOnHelp = true
@@ -582,9 +588,11 @@ func subCommands2Result(result string, commands []Command, maxWidth int) string
582588
func arguments2Result(result string, arguments []*arg, maxWidth int) string {
583589
usedHelp := false
584590
if len(arguments) > 0 {
585-
argContent := "Arguments:\n\n"
591+
argContent := "Options:\n\n"
586592
// Get biggest padding
587593
var argPadding int
594+
var argPathPadding int
595+
588596
// Find biggest padding
589597
for _, argument := range arguments {
590598
if argument.opts.Help == DisableDescription {
@@ -593,6 +601,9 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string {
593601
if len(argument.lname)+9 > argPadding {
594602
argPadding = len(argument.lname) + 9
595603
}
604+
if len(argument.opts.Path) > argPathPadding {
605+
argPathPadding = len(argument.opts.Path)
606+
}
596607
}
597608
// Now add args with padding
598609
for _, argument := range arguments {
@@ -602,15 +613,24 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string {
602613
if argument.lname == "help" && usedHelp {
603614
} else {
604615
arg := " "
616+
605617
if argument.sname != "" {
606618
arg = arg + "-" + argument.sname + " "
607619
} else {
608620
arg = arg + " "
609621
}
610622
arg = arg + "--" + argument.lname
611623
arg = arg + strings.Repeat(" ", argPadding-len(arg))
612-
if argument.opts != nil && argument.opts.Help != "" {
613-
arg = addToLastLine(arg, argument.getHelpMessage(), maxWidth, argPadding, true)
624+
625+
if argument.opts != nil {
626+
if argument.opts.Path == "" {
627+
arg = addToLastLine(arg, strings.Repeat(" ", argPathPadding), maxWidth, argPathPadding, true)
628+
} else {
629+
arg = addToLastLine(arg, argument.opts.Path+strings.Repeat(" ", argPathPadding-len(argument.opts.Path)), maxWidth, argPathPadding, true)
630+
}
631+
if argument.opts.Help != "" {
632+
arg = addToLastLine(arg, argument.getHelpMessage(), maxWidth, argPadding, true)
633+
}
614634
}
615635
argContent = argContent + arg + "\n"
616636
}
@@ -668,6 +688,11 @@ func (o *Command) Usage(msg interface{}) string {
668688
// Add list of arguments to the result
669689
result = arguments2Result(result, arguments, maxWidth)
670690

691+
// If this is the last command in the chain, add author info
692+
if len(o.GetCommands()) == 0 {
693+
result += o.authorinfo
694+
}
695+
671696
return result
672697
}
673698

0 commit comments

Comments
 (0)