Skip to content

Commit d0da121

Browse files
authored
Merge pull request #50 from choria-io/49
(#49) Handle introspect correctly when top has required flags or args
2 parents b579221 + edda7b9 commit d0da121

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

Diff for: .github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
test:
66
strategy:
77
matrix:
8-
go: ["1.19", "1.20"]
8+
go: ["1.20", "1.21"]
99

1010
runs-on: ubuntu-latest
1111
steps:

Diff for: app.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Application struct {
4444
noInterspersed bool // can flags be interspersed with args (or must they come first)
4545
defaultEnvars bool
4646
completion bool
47+
introspect bool
4748
cheats map[string]string
4849
cheatTags []string
4950
helpFlagIsSet bool
@@ -89,7 +90,7 @@ func New(name, help string) *Application {
8990
a.Flag("completion-bash", "Output possible completions for the given args.").Hidden().UnNegatableBoolVar(&a.completion)
9091
a.Flag("completion-script-bash", "Generate completion script for bash.").Hidden().PreAction(a.generateBashCompletionScript).UnNegatableBool()
9192
a.Flag("completion-script-zsh", "Generate completion script for ZSH.").Hidden().PreAction(a.generateZSHCompletionScript).UnNegatableBool()
92-
a.Flag("fisk-introspect", "Introspect the application model").Hidden().Action(a.introspectAction).UnNegatableBool()
93+
a.Flag("fisk-introspect", "Introspect the application model").Hidden().Action(a.introspectAction).UnNegatableBoolVar(&a.introspect)
9394

9495
return a
9596
}
@@ -255,6 +256,9 @@ func (a *Application) Parse(args []string) (command string, err error) {
255256
if a.completion {
256257
a.generateBashCompletion(context)
257258
a.terminate(0)
259+
} else if a.introspect {
260+
a.introspectAction(context)
261+
a.terminate(0)
258262
} else {
259263
if parseErr != nil {
260264
return "", parseErr

Diff for: app_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,21 @@ var docFS embed.FS
584584

585585
func TestCheatFile(t *testing.T) {
586586
c := newTestApp().CheatFile(docFS, "", "doc.go")
587+
587588
c.Command("x", "x").CheatFile(docFS, "y", "doc.go")
588589
assert.Contains(t, c.cheats["test"], "Package fisk provides")
589590
assert.Contains(t, c.cheats["y"], "Package fisk provides")
591+
592+
}
593+
594+
func TestFiskIntrospect(t *testing.T) {
595+
var buf bytes.Buffer
596+
c := newTestApp()
597+
c.usageWriter = &buf
598+
c.errorWriter = &buf
599+
c.Flag("required", "required").Required().String()
600+
c.MustParseWithUsage([]string{"--fisk-introspect"})
601+
assert.NotContains(t, buf.String(), "required flag --required not provided")
590602
}
591603

592604
func TestParseWithUsage(t *testing.T) {

Diff for: go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/choria-io/fisk
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/stretchr/testify v1.8.4
7-
golang.org/x/text v0.13.0
7+
golang.org/x/text v0.14.0
88
)
99

1010
require (

Diff for: go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
55
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
66
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
8-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
7+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
8+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
99
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1010
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1111
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

Diff for: usage.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"fmt"
77
"go/doc"
8+
"go/doc/comment"
89
"io"
910
"strings"
1011
"text/template"
@@ -33,7 +34,14 @@ func formatTwoColumns(w io.Writer, indent, padding, width int, rows [][2]string)
3334

3435
for _, row := range rows {
3536
buf := bytes.NewBuffer(nil)
36-
doc.ToText(buf, row[1], "", preIndent, width-s-padding-indent)
37+
d := new(doc.Package).Parser().Parse(row[1])
38+
pr := &comment.Printer{
39+
TextPrefix: "",
40+
TextCodePrefix: preIndent,
41+
TextWidth: width - s - padding - indent,
42+
}
43+
buf.Write(pr.Text(d))
44+
3745
lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
3846
fmt.Fprintf(w, "%s%-*s%*s", indentStr, s, row[0], padding, "")
3947
if len(row[0]) >= max {
@@ -134,7 +142,15 @@ func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent
134142
"Wrap": func(indent int, s string) string {
135143
buf := bytes.NewBuffer(nil)
136144
indentText := strings.Repeat(" ", indent)
137-
doc.ToText(buf, s, indentText, " "+indentText, width-indent)
145+
146+
d := new(doc.Package).Parser().Parse(s)
147+
pr := &comment.Printer{
148+
TextPrefix: indentText,
149+
TextCodePrefix: " " + indentText,
150+
TextWidth: width - indent,
151+
}
152+
buf.Write(pr.Text(d))
153+
138154
return buf.String()
139155
},
140156
"FormatFlag": formatFlag,

0 commit comments

Comments
 (0)