Skip to content

Commit

Permalink
Merge e1e0cdd into 6876ef2
Browse files Browse the repository at this point in the history
  • Loading branch information
thegreyd committed Mar 1, 2020
2 parents 6876ef2 + e1e0cdd commit 13bbb4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
17 changes: 10 additions & 7 deletions argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,11 @@ func (o *Command) Selector(short string, long string, options []string, opts *Op
return &result
}

// message2String - puts msg in result string
// message2String puts msg in result string
// done boolean indicates if result is ready to be returned
// Accepts an interface that can be error, string or fmt.Stringer that will be prepended to a message.
// All other interface types will be ignored
func message2String(msg interface{}) string {
func message2String(msg interface{}) (string, bool) {
var result string
if msg != nil {
switch msg.(type) {
Expand All @@ -453,7 +454,7 @@ func message2String(msg interface{}) string {
if msg.(subCommandError).cmd != nil {
result += msg.(subCommandError).cmd.Usage(nil)
}
return result
return result, true
case error:
result = fmt.Sprintf("%s\n", msg.(error).Error())
case string:
Expand All @@ -462,7 +463,7 @@ func message2String(msg interface{}) string {
result = fmt.Sprintf("%s\n", msg.(fmt.Stringer).String())
}
}
return result
return result, false
}

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

var result string
// Stay classy
maxWidth := 80
// List of arguments from all preceding commands
arguments := make([]*arg, 0)
// Line of commands until root
var chain []string

// Put message in resultz
result = message2String(msg)
// Put message in result
result, done := message2String(msg)
if done {
return result
}

//collect info about Preceding Commands into chain and arguments
o.getPrecedingCommands(&chain, &arguments)
Expand Down
30 changes: 30 additions & 0 deletions argparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,36 @@ func TestUsageHidden1(t *testing.T) {
}
}

func TestUsageSubCommand(t *testing.T) {
expected := `[sub]Command required
usage: zooprog <Command> [-h|--help]
Program that walks us through the zoo
Commands:
dog We are going to see dog
Arguments:
-h --help Print help information
`

parser := NewParser("zooprog", "Program that walks us through the zoo")

// dog command
parser.
NewCommand("dog", "We are going to see dog"). // adds command to parser
NewCommand("speak", "Make the dog speak") // adds subcommand to previous command

err := newSubCommandError(&parser.Command)
actual := parser.Usage(err)
if expected != actual {
t.Errorf("Expectations unmet. expected: %s, actual: %s", expected, actual)
}
}

func TestStringMissingArgFail(t *testing.T) {
testArgs := []string{"progname", "-s"}

Expand Down

0 comments on commit 13bbb4a

Please sign in to comment.