@@ -20,6 +20,7 @@ var disableHelp = false
20
20
type Command struct {
21
21
name string
22
22
description string
23
+ authorinfo string
23
24
args []* arg
24
25
commands []* Command
25
26
parsed bool
@@ -91,24 +92,28 @@ type Parser struct {
91
92
// Options.Help - A help message to be displayed in Usage output. Can be of any length as the message will be
92
93
// formatted to fit max screen width of 100 characters.
93
94
//
95
+ // Options.Path - A <meta> field describing the argument. For example <path> for a filename argument.
96
+ //
94
97
// Options.Default - A default value for an argument. This value will be assigned to the argument at the end of parsing
95
98
// in case if this argument was not supplied on command line. File default value is a string which it will be open with
96
99
// provided options. In case if provided value type does not match expected, the error will be returned on run-time.
97
100
type Options struct {
98
101
Required bool
99
102
Validate func (args []string ) error
100
103
Help string
104
+ Path string
101
105
Default interface {}
102
106
}
103
107
104
108
// NewParser creates new Parser object that will allow to add arguments for parsing
105
109
// It takes program name and description which will be used as part of Usage output
106
110
// Returns pointer to Parser object
107
- func NewParser (name string , description string ) * Parser {
111
+ func NewParser (name , description , authorinfo string ) * Parser {
108
112
p := & Parser {}
109
113
110
114
p .name = name
111
115
p .description = description
116
+ p .authorinfo = authorinfo
112
117
113
118
p .args = make ([]* arg , 0 )
114
119
p .commands = make ([]* Command , 0 )
@@ -133,6 +138,7 @@ func (o *Command) NewCommand(name string, description string) *Command {
133
138
c .description = description
134
139
c .parsed = false
135
140
c .parent = o
141
+
136
142
if ! disableHelp {
137
143
c .help ("h" , "help" )
138
144
c .exitOnHelp = true
@@ -582,9 +588,11 @@ func subCommands2Result(result string, commands []Command, maxWidth int) string
582
588
func arguments2Result (result string , arguments []* arg , maxWidth int ) string {
583
589
usedHelp := false
584
590
if len (arguments ) > 0 {
585
- argContent := "Arguments :\n \n "
591
+ argContent := "Options :\n \n "
586
592
// Get biggest padding
587
593
var argPadding int
594
+ var argPathPadding int
595
+
588
596
// Find biggest padding
589
597
for _ , argument := range arguments {
590
598
if argument .opts .Help == DisableDescription {
@@ -593,6 +601,9 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string {
593
601
if len (argument .lname )+ 9 > argPadding {
594
602
argPadding = len (argument .lname ) + 9
595
603
}
604
+ if len (argument .opts .Path ) > argPathPadding {
605
+ argPathPadding = len (argument .opts .Path )
606
+ }
596
607
}
597
608
// Now add args with padding
598
609
for _ , argument := range arguments {
@@ -602,15 +613,24 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string {
602
613
if argument .lname == "help" && usedHelp {
603
614
} else {
604
615
arg := " "
616
+
605
617
if argument .sname != "" {
606
618
arg = arg + "-" + argument .sname + " "
607
619
} else {
608
620
arg = arg + " "
609
621
}
610
622
arg = arg + "--" + argument .lname
611
623
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
+ }
614
634
}
615
635
argContent = argContent + arg + "\n "
616
636
}
@@ -668,6 +688,11 @@ func (o *Command) Usage(msg interface{}) string {
668
688
// Add list of arguments to the result
669
689
result = arguments2Result (result , arguments , maxWidth )
670
690
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
+
671
696
return result
672
697
}
673
698
0 commit comments