From 637dac02546cc235ce50396ca9cea8672234364b Mon Sep 17 00:00:00 2001 From: Matt Willer Date: Tue, 31 Aug 2021 20:13:21 -0700 Subject: [PATCH 1/2] Expose terminal subcommand For use cases where only the final subcommand in the sequence needs to be run (e.g. `git remote add` just runs the add subcommand of remote), it would be useful to identify that subcommand directly. Since that pointer is already helpfully stored in the parser, simply exposing it via a method worked nicely. --- flaggy_test.go | 6 ++++++ parser.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/flaggy_test.go b/flaggy_test.go index 1de99fd..d78bdd3 100644 --- a/flaggy_test.go +++ b/flaggy_test.go @@ -108,6 +108,9 @@ func TestComplexNesting(t *testing.T) { t.Log("testE", testE) t.FailNow() } + if subcommandName := flaggy.DefaultParser.TerminalSubcommand().Name; subcommandName != "scD" { + t.Fatal("Used subcommand was incorrect:", subcommandName) + } } @@ -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.TerminalSubcommand().Name; subcommandName != "subcommand" { + t.Fatal("Used subcommand was incorrect:", subcommandName) + } } diff --git a/parser.go b/parser.go index 495e7c2..55184a6 100644 --- a/parser.go +++ b/parser.go @@ -26,6 +26,10 @@ type Parser struct { subcommandContext *Subcommand // points to the most specific subcommand being used } +func (p *Parser) TerminalSubcommand() *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 From 50aa5da22b65562441edd418b5d9622f9a1a5447 Mon Sep 17 00:00:00 2001 From: Matt Willer Date: Fri, 22 Apr 2022 19:36:44 -0700 Subject: [PATCH 2/2] Change name to TrailingSubcommand Co-authored-by: Eric Greer --- flaggy_test.go | 4 ++-- parser.go | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/flaggy_test.go b/flaggy_test.go index d78bdd3..7cfbd02 100644 --- a/flaggy_test.go +++ b/flaggy_test.go @@ -108,7 +108,7 @@ func TestComplexNesting(t *testing.T) { t.Log("testE", testE) t.FailNow() } - if subcommandName := flaggy.DefaultParser.TerminalSubcommand().Name; subcommandName != "scD" { + if subcommandName := flaggy.DefaultParser.TrailingSubcommand().Name; subcommandName != "scD" { t.Fatal("Used subcommand was incorrect:", subcommandName) } @@ -180,7 +180,7 @@ func TestParsePositionalsA(t *testing.T) { if parser.TrailingArguments[1] != "trailingB" { t.Fatal("Trailing argumentB was incorrect:", parser.TrailingArguments[1]) } - if subcommandName := parser.TerminalSubcommand().Name; subcommandName != "subcommand" { + if subcommandName := parser.TrailingSubcommand().Name; subcommandName != "subcommand" { t.Fatal("Used subcommand was incorrect:", subcommandName) } diff --git a/parser.go b/parser.go index 55184a6..5465ad4 100644 --- a/parser.go +++ b/parser.go @@ -26,7 +26,8 @@ type Parser struct { subcommandContext *Subcommand // points to the most specific subcommand being used } -func (p *Parser) TerminalSubcommand() *Subcommand { +TrailingSubcommand returns the last and most specific subcommand invoked. +func (p *Parser) TrailingSubcommand() *Subcommand { return p.subcommandContext }