Skip to content

Commit

Permalink
Add description padding costomization
Browse files Browse the repository at this point in the history
  • Loading branch information
Gueckmooh committed May 29, 2021
1 parent b0d9075 commit 907e570
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
26 changes: 17 additions & 9 deletions argparse.go
Expand Up @@ -18,15 +18,16 @@ var disableHelp = false
// Command MUST NOT ever be created manually. Instead one should call NewCommand method of Parser or Command,
// which will setup appropriate fields and call methods that have to be called when creating new command.
type Command struct {
name string
description string
args []*arg
commands []*Command
parsed bool
happened bool
parent *Command
HelpFunc func(c *Command, msg interface{}) string
exitOnHelp bool
name string
description string
args []*arg
commands []*Command
parsed bool
happened bool
parent *Command
HelpFunc func(c *Command, msg interface{}) string
exitOnHelp bool
DescriptionPadding interface{}
}

// GetName exposes Command's name field
Expand Down Expand Up @@ -116,6 +117,7 @@ func NewParser(name string, description string) *Parser {
p.help("h", "help")
p.exitOnHelp = true
p.HelpFunc = (*Command).Usage
p.DescriptionPadding = nil

return p
}
Expand Down Expand Up @@ -539,6 +541,12 @@ func (o *Command) precedingCommands2Result(result string, chain []string, argume
usedHelp = true
}
}
// Set custom padding
customPadding, ok := o.DescriptionPadding.(int)
if ok {
// -1 because addTolastline inserts extra space
leftPadding = customPadding - 1
}
// Add program/Command description to the result
result = result + "\n\n" + strings.Repeat(" ", leftPadding)
result = addToLastLine(result, o.description, maxWidth, leftPadding, true)
Expand Down
27 changes: 26 additions & 1 deletion argparse_test.go
Expand Up @@ -2708,7 +2708,32 @@ Arguments:

parser := NewParser("command", "Program with multiline\ndescription.")

parser.String("s", "string", &Options{Required: true, Help: "String argument example\non several lines"})
parser.String("s", "string", &Options{Required: true, Help: "String argument example\non several lines"})

actual := parser.Help(nil)
if expected != actual {
t.Errorf("Expectations unmet. expected: %s, actual: %s", expected, actual)
}
}

func TestCommandHelpMultilineWithCustomPadding(t *testing.T) {
expected := `usage: command [-h|--help] -s|--string "<value>"
Program with multiline
description.
Arguments:
-h --help Print help information
-s --string String argument example
on several lines
`

parser := NewParser("command", "Program with multiline\ndescription.")

parser.String("s", "string", &Options{Required: true, Help: "String argument example\non several lines"})
parser.DescriptionPadding = 3

actual := parser.Help(nil)
if expected != actual {
Expand Down
22 changes: 22 additions & 0 deletions examples/custompadding/custompadding.go
@@ -0,0 +1,22 @@
package main

import (
"fmt"

"github.com/akamensky/argparse"
)

func main() {
// Create new parser object
parser := argparse.NewParser("help",
`Demonstrates multiline description and help messages.
With some very long lines like this one which is broken automatically by argparse.
And some shorter.`)
// Create string flag
parser.String("s", "string", &argparse.Options{Required: true, Help: "String argument example\non several lines"})
// Create string flag
parser.Int("i", "int", &argparse.Options{Required: true, Help: "Integer argument example"})
parser.DescriptionPadding = 3
// Use the help function
fmt.Print(parser.Help(nil))
}

0 comments on commit 907e570

Please sign in to comment.