Skip to content

Commit

Permalink
(#49) Handle introspect correctly when top has required flags or args
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Dec 19, 2023
1 parent b579221 commit 5fa1cae
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Application struct {
noInterspersed bool // can flags be interspersed with args (or must they come first)
defaultEnvars bool
completion bool
introspect bool
cheats map[string]string
cheatTags []string
helpFlagIsSet bool
Expand Down Expand Up @@ -89,7 +90,7 @@ func New(name, help string) *Application {
a.Flag("completion-bash", "Output possible completions for the given args.").Hidden().UnNegatableBoolVar(&a.completion)
a.Flag("completion-script-bash", "Generate completion script for bash.").Hidden().PreAction(a.generateBashCompletionScript).UnNegatableBool()
a.Flag("completion-script-zsh", "Generate completion script for ZSH.").Hidden().PreAction(a.generateZSHCompletionScript).UnNegatableBool()
a.Flag("fisk-introspect", "Introspect the application model").Hidden().Action(a.introspectAction).UnNegatableBool()
a.Flag("fisk-introspect", "Introspect the application model").Hidden().Action(a.introspectAction).UnNegatableBoolVar(&a.introspect)

return a
}
Expand Down Expand Up @@ -255,6 +256,9 @@ func (a *Application) Parse(args []string) (command string, err error) {
if a.completion {
a.generateBashCompletion(context)
a.terminate(0)
} else if a.introspect {
a.introspectAction(context)
a.terminate(0)
} else {
if parseErr != nil {
return "", parseErr
Expand Down
12 changes: 12 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,21 @@ var docFS embed.FS

func TestCheatFile(t *testing.T) {
c := newTestApp().CheatFile(docFS, "", "doc.go")

c.Command("x", "x").CheatFile(docFS, "y", "doc.go")
assert.Contains(t, c.cheats["test"], "Package fisk provides")
assert.Contains(t, c.cheats["y"], "Package fisk provides")

}

func TestFiskIntrospect(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.usageWriter = &buf
c.errorWriter = &buf
c.Flag("required", "required").Required().String()
c.MustParseWithUsage([]string{"--fisk-introspect"})
assert.NotContains(t, buf.String(), "required flag --required not provided")
}

func TestParseWithUsage(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/choria-io/fisk

go 1.19
go 1.20

require (
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.13.0
golang.org/x/text v0.14.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit 5fa1cae

Please sign in to comment.