Skip to content

Commit

Permalink
Merge pull request #75 from mattwiller/expose_subcommand_context
Browse files Browse the repository at this point in the history
Expose terminal subcommand
  • Loading branch information
Eric Greer committed Apr 23, 2022
2 parents 5fe7abb + 50aa5da commit 50d0cd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flaggy_test.go
Expand Up @@ -108,6 +108,9 @@ func TestComplexNesting(t *testing.T) {
t.Log("testE", testE)
t.FailNow()
}
if subcommandName := flaggy.DefaultParser.TrailingSubcommand().Name; subcommandName != "scD" {
t.Fatal("Used subcommand was incorrect:", subcommandName)
}

}

Expand Down Expand Up @@ -177,5 +180,8 @@ func TestParsePositionalsA(t *testing.T) {
if parser.TrailingArguments[1] != "trailingB" {
t.Fatal("Trailing argumentB was incorrect:", parser.TrailingArguments[1])
}
if subcommandName := parser.TrailingSubcommand().Name; subcommandName != "subcommand" {
t.Fatal("Used subcommand was incorrect:", subcommandName)
}

}
5 changes: 5 additions & 0 deletions parser.go
Expand Up @@ -26,6 +26,11 @@ type Parser struct {
subcommandContext *Subcommand // points to the most specific subcommand being used
}

TrailingSubcommand returns the last and most specific subcommand invoked.
func (p *Parser) TrailingSubcommand() *Subcommand {
return p.subcommandContext
}

// NewParser creates a new ArgumentParser ready to parse inputs
func NewParser(name string) *Parser {
// this can not be done inline because of struct embedding
Expand Down

0 comments on commit 50d0cd8

Please sign in to comment.