Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#49) Handle introspect correctly when top has required flags or args #50

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
test:
strategy:
matrix:
go: ["1.19", "1.20"]
go: ["1.20", "1.21"]

runs-on: ubuntu-latest
steps:
Expand Down
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
20 changes: 18 additions & 2 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"go/doc"
"go/doc/comment"
"io"
"strings"
"text/template"
Expand Down Expand Up @@ -33,7 +34,14 @@ func formatTwoColumns(w io.Writer, indent, padding, width int, rows [][2]string)

for _, row := range rows {
buf := bytes.NewBuffer(nil)
doc.ToText(buf, row[1], "", preIndent, width-s-padding-indent)
d := new(doc.Package).Parser().Parse(row[1])
pr := &comment.Printer{
TextPrefix: "",
TextCodePrefix: preIndent,
TextWidth: width - s - padding - indent,
}
buf.Write(pr.Text(d))

lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
fmt.Fprintf(w, "%s%-*s%*s", indentStr, s, row[0], padding, "")
if len(row[0]) >= max {
Expand Down Expand Up @@ -134,7 +142,15 @@ func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent
"Wrap": func(indent int, s string) string {
buf := bytes.NewBuffer(nil)
indentText := strings.Repeat(" ", indent)
doc.ToText(buf, s, indentText, " "+indentText, width-indent)

d := new(doc.Package).Parser().Parse(s)
pr := &comment.Printer{
TextPrefix: indentText,
TextCodePrefix: " " + indentText,
TextWidth: width - indent,
}
buf.Write(pr.Text(d))

return buf.String()
},
"FormatFlag": formatFlag,
Expand Down
Loading