Skip to content

Commit

Permalink
Multiline descriptions and help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Gueckmooh committed May 28, 2021
1 parent 231d348 commit ad76fcf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/multiline/multiline.go
@@ -0,0 +1,21 @@
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"})
// Use the help function
fmt.Print(parser.Help(nil))
}
15 changes: 15 additions & 0 deletions extras.go
Expand Up @@ -8,6 +8,21 @@ func getLastLine(input string) string {
}

func addToLastLine(base string, add string, width int, padding int, canSplit bool) string {
// Split the multiline string to add
if strings.Contains(add, "\n") {
lines := strings.Split(add, "\n")
firstLine := true
for _, v := range lines {
if firstLine {
base = addToLastLine(base, v, width, padding, true)
firstLine = false
} else {
base = base + "\n" + strings.Repeat(" ", padding)
base = addToLastLine(base, v, width, padding, true)
}
}
return base
}
// If last line has less than 10% space left, do not try to fill in by splitting else just try to split
hasTen := (width - len(getLastLine(base))) > width/10
if len(getLastLine(base)+" "+add) >= width {
Expand Down

0 comments on commit ad76fcf

Please sign in to comment.