Skip to content

Commit 936811b

Browse files
authored
add a boolean flag to message2string which indicates if the result string is ready to be returned + add tests (akamensky#60)
1 parent 6876ef2 commit 936811b

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

argparse.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,11 @@ func (o *Command) Selector(short string, long string, options []string, opts *Op
441441
return &result
442442
}
443443

444-
// message2String - puts msg in result string
444+
// message2String puts msg in result string
445+
// done boolean indicates if result is ready to be returned
445446
// Accepts an interface that can be error, string or fmt.Stringer that will be prepended to a message.
446447
// All other interface types will be ignored
447-
func message2String(msg interface{}) string {
448+
func message2String(msg interface{}) (string, bool) {
448449
var result string
449450
if msg != nil {
450451
switch msg.(type) {
@@ -453,7 +454,7 @@ func message2String(msg interface{}) string {
453454
if msg.(subCommandError).cmd != nil {
454455
result += msg.(subCommandError).cmd.Usage(nil)
455456
}
456-
return result
457+
return result, true
457458
case error:
458459
result = fmt.Sprintf("%s\n", msg.(error).Error())
459460
case string:
@@ -462,7 +463,7 @@ func message2String(msg interface{}) string {
462463
result = fmt.Sprintf("%s\n", msg.(fmt.Stringer).String())
463464
}
464465
}
465-
return result
466+
return result, false
466467
}
467468

468469
// getPrecedingCommands - collects info on command chain from root to current (o *Command) and all arguments in this chain
@@ -611,16 +612,18 @@ func (o *Command) Usage(msg interface{}) string {
611612
}
612613
}
613614

614-
var result string
615615
// Stay classy
616616
maxWidth := 80
617617
// List of arguments from all preceding commands
618618
arguments := make([]*arg, 0)
619619
// Line of commands until root
620620
var chain []string
621621

622-
// Put message in resultz
623-
result = message2String(msg)
622+
// Put message in result
623+
result, done := message2String(msg)
624+
if done {
625+
return result
626+
}
624627

625628
//collect info about Preceding Commands into chain and arguments
626629
o.getPrecedingCommands(&chain, &arguments)

argparse_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,36 @@ func TestUsageHidden1(t *testing.T) {
17021702
}
17031703
}
17041704

1705+
func TestUsageSubCommand(t *testing.T) {
1706+
expected := `[sub]Command required
1707+
usage: zooprog <Command> [-h|--help]
1708+
1709+
Program that walks us through the zoo
1710+
1711+
Commands:
1712+
1713+
dog We are going to see dog
1714+
1715+
Arguments:
1716+
1717+
-h --help Print help information
1718+
1719+
`
1720+
1721+
parser := NewParser("zooprog", "Program that walks us through the zoo")
1722+
1723+
// dog command
1724+
parser.
1725+
NewCommand("dog", "We are going to see dog"). // adds command to parser
1726+
NewCommand("speak", "Make the dog speak") // adds subcommand to previous command
1727+
1728+
err := newSubCommandError(&parser.Command)
1729+
actual := parser.Usage(err)
1730+
if expected != actual {
1731+
t.Errorf("Expectations unmet. expected: %s, actual: %s", expected, actual)
1732+
}
1733+
}
1734+
17051735
func TestStringMissingArgFail(t *testing.T) {
17061736
testArgs := []string{"progname", "-s"}
17071737

0 commit comments

Comments
 (0)