From b739fd8c21d7d60bf623fbc69189102c75963f39 Mon Sep 17 00:00:00 2001 From: Anand Date: Sun, 12 Dec 2021 12:56:46 +0530 Subject: [PATCH 1/3] ref issue #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 --- argparse.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/argparse.go b/argparse.go index 1ecac29..f313806 100644 --- a/argparse.go +++ b/argparse.go @@ -20,6 +20,7 @@ var disableHelp = false type Command struct { name string description string + authorinfo string args []*arg commands []*Command parsed bool @@ -91,6 +92,8 @@ type Parser struct { // Options.Help - A help message to be displayed in Usage output. Can be of any length as the message will be // formatted to fit max screen width of 100 characters. // +// Options.Path - A field describing the argument. For example for a filename argument. +// // Options.Default - A default value for an argument. This value will be assigned to the argument at the end of parsing // in case if this argument was not supplied on command line. File default value is a string which it will be open with // provided options. In case if provided value type does not match expected, the error will be returned on run-time. @@ -98,17 +101,19 @@ type Options struct { Required bool Validate func(args []string) error Help string + Path string Default interface{} } // NewParser creates new Parser object that will allow to add arguments for parsing // It takes program name and description which will be used as part of Usage output // Returns pointer to Parser object -func NewParser(name string, description string) *Parser { +func NewParser(name, description, authorinfo string) *Parser { p := &Parser{} p.name = name p.description = description + p.authorinfo = authorinfo p.args = make([]*arg, 0) p.commands = make([]*Command, 0) @@ -133,6 +138,7 @@ func (o *Command) NewCommand(name string, description string) *Command { c.description = description c.parsed = false c.parent = o + if !disableHelp { c.help("h", "help") c.exitOnHelp = true @@ -582,9 +588,11 @@ func subCommands2Result(result string, commands []Command, maxWidth int) string func arguments2Result(result string, arguments []*arg, maxWidth int) string { usedHelp := false if len(arguments) > 0 { - argContent := "Arguments:\n\n" + argContent := "Options:\n\n" // Get biggest padding var argPadding int + var argPathPadding int + // Find biggest padding for _, argument := range arguments { if argument.opts.Help == DisableDescription { @@ -593,6 +601,9 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string { if len(argument.lname)+9 > argPadding { argPadding = len(argument.lname) + 9 } + if len(argument.opts.Path) > argPathPadding { + argPathPadding = len(argument.opts.Path) + } } // Now add args with padding for _, argument := range arguments { @@ -602,6 +613,7 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string { if argument.lname == "help" && usedHelp { } else { arg := " " + if argument.sname != "" { arg = arg + "-" + argument.sname + " " } else { @@ -609,8 +621,16 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string { } arg = arg + "--" + argument.lname arg = arg + strings.Repeat(" ", argPadding-len(arg)) - if argument.opts != nil && argument.opts.Help != "" { - arg = addToLastLine(arg, argument.getHelpMessage(), maxWidth, argPadding, true) + + if argument.opts != nil { + if argument.opts.Path == "" { + arg = addToLastLine(arg, strings.Repeat(" ", argPathPadding), maxWidth, argPathPadding, true) + } else { + arg = addToLastLine(arg, argument.opts.Path+strings.Repeat(" ", argPathPadding-len(argument.opts.Path)), maxWidth, argPathPadding, true) + } + if argument.opts.Help != "" { + arg = addToLastLine(arg, argument.getHelpMessage(), maxWidth, argPadding, true) + } } argContent = argContent + arg + "\n" } @@ -668,6 +688,11 @@ func (o *Command) Usage(msg interface{}) string { // Add list of arguments to the result result = arguments2Result(result, arguments, maxWidth) + // If this is the last command in the chain, add author info + if len(o.GetCommands()) == 0 { + result += o.authorinfo + } + return result } From e8523825f627c0e0aee8a92703bdfe0db269ca83 Mon Sep 17 00:00:00 2001 From: Anand Date: Sun, 12 Dec 2021 13:02:33 +0530 Subject: [PATCH 2/3] ref issue #94 (original argparse) - Added new Path field in Options for metavar and authorinfo to allow author information + copyright to be printed in usage --- argparse.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/argparse.go b/argparse.go index 1ecac29..f313806 100644 --- a/argparse.go +++ b/argparse.go @@ -20,6 +20,7 @@ var disableHelp = false type Command struct { name string description string + authorinfo string args []*arg commands []*Command parsed bool @@ -91,6 +92,8 @@ type Parser struct { // Options.Help - A help message to be displayed in Usage output. Can be of any length as the message will be // formatted to fit max screen width of 100 characters. // +// Options.Path - A field describing the argument. For example for a filename argument. +// // Options.Default - A default value for an argument. This value will be assigned to the argument at the end of parsing // in case if this argument was not supplied on command line. File default value is a string which it will be open with // provided options. In case if provided value type does not match expected, the error will be returned on run-time. @@ -98,17 +101,19 @@ type Options struct { Required bool Validate func(args []string) error Help string + Path string Default interface{} } // NewParser creates new Parser object that will allow to add arguments for parsing // It takes program name and description which will be used as part of Usage output // Returns pointer to Parser object -func NewParser(name string, description string) *Parser { +func NewParser(name, description, authorinfo string) *Parser { p := &Parser{} p.name = name p.description = description + p.authorinfo = authorinfo p.args = make([]*arg, 0) p.commands = make([]*Command, 0) @@ -133,6 +138,7 @@ func (o *Command) NewCommand(name string, description string) *Command { c.description = description c.parsed = false c.parent = o + if !disableHelp { c.help("h", "help") c.exitOnHelp = true @@ -582,9 +588,11 @@ func subCommands2Result(result string, commands []Command, maxWidth int) string func arguments2Result(result string, arguments []*arg, maxWidth int) string { usedHelp := false if len(arguments) > 0 { - argContent := "Arguments:\n\n" + argContent := "Options:\n\n" // Get biggest padding var argPadding int + var argPathPadding int + // Find biggest padding for _, argument := range arguments { if argument.opts.Help == DisableDescription { @@ -593,6 +601,9 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string { if len(argument.lname)+9 > argPadding { argPadding = len(argument.lname) + 9 } + if len(argument.opts.Path) > argPathPadding { + argPathPadding = len(argument.opts.Path) + } } // Now add args with padding for _, argument := range arguments { @@ -602,6 +613,7 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string { if argument.lname == "help" && usedHelp { } else { arg := " " + if argument.sname != "" { arg = arg + "-" + argument.sname + " " } else { @@ -609,8 +621,16 @@ func arguments2Result(result string, arguments []*arg, maxWidth int) string { } arg = arg + "--" + argument.lname arg = arg + strings.Repeat(" ", argPadding-len(arg)) - if argument.opts != nil && argument.opts.Help != "" { - arg = addToLastLine(arg, argument.getHelpMessage(), maxWidth, argPadding, true) + + if argument.opts != nil { + if argument.opts.Path == "" { + arg = addToLastLine(arg, strings.Repeat(" ", argPathPadding), maxWidth, argPathPadding, true) + } else { + arg = addToLastLine(arg, argument.opts.Path+strings.Repeat(" ", argPathPadding-len(argument.opts.Path)), maxWidth, argPathPadding, true) + } + if argument.opts.Help != "" { + arg = addToLastLine(arg, argument.getHelpMessage(), maxWidth, argPadding, true) + } } argContent = argContent + arg + "\n" } @@ -668,6 +688,11 @@ func (o *Command) Usage(msg interface{}) string { // Add list of arguments to the result result = arguments2Result(result, arguments, maxWidth) + // If this is the last command in the chain, add author info + if len(o.GetCommands()) == 0 { + result += o.authorinfo + } + return result } From f01fe192359b1c2b37f2406c69f975243705b83f Mon Sep 17 00:00:00 2001 From: Anand Date: Sun, 12 Dec 2021 13:08:37 +0530 Subject: [PATCH 3/3] ref issue #94 - Updated module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 189632f..faf69ca 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/akamensky/argparse +module github.com/pythonhacker/argparse go 1.13