Skip to content

Commit

Permalink
Merge pull request #32 from choria-io/31
Browse files Browse the repository at this point in the history
(#31) Attempt to intelligently show global flags
  • Loading branch information
ripienaar authored Apr 21, 2023
2 parents 9809553 + afe431c commit f95ab53
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Application struct {
completion bool
cheats map[string]string
cheatTags []string
helpFlagIsSet bool

// Help flag. Exposed for user customisation.
HelpFlag *FlagClause
Expand Down Expand Up @@ -75,7 +76,7 @@ func New(name, help string) *Application {
a.flagGroup = newFlagGroup()
a.argGroup = newArgGroup()
a.cmdGroup = newCmdGroup(a)
a.HelpFlag = a.Flag("help", "Show context-sensitive help")
a.HelpFlag = a.Flag("help", "Show context-sensitive help").IsSetByUser(&a.helpFlagIsSet)
a.HelpFlag.UnNegatableBool()

a.Flag("help-long", "Generate long help.").Hidden().PreAction(a.generateLongHelp).UnNegatableBool()
Expand Down
9 changes: 6 additions & 3 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@ Commands:
Flags:
{{.Context.SelectedCommand.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{else -}}
{{if .Context.Flags|VisibleFlags -}}
{{end -}}
{{if GlobalFlags .Context|VisibleFlags -}}
{{if .HelpFlagIsSet -}}
Global Flags:
{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{ GlobalFlags .Context|FlagsToTwoColumns|FormatTwoColumns}}
{{else -}}
Pass --help to see global flags applicable to this command.
{{end -}}
{{end -}}
`
Expand Down
40 changes: 35 additions & 5 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func formatTwoColumns(w io.Writer, indent, padding, width int, rows [][2]string)
func (a *Application) Usage(args []string) {
context, err := a.parseContext(true, args)
a.FatalIfError(err, "")

if err := a.UsageForContextWithTemplate(context, 2, a.usageTemplate); err != nil {
panic(err)
}
Expand Down Expand Up @@ -111,9 +112,10 @@ type templateParseContext struct {
}

type templateContext struct {
App *ApplicationModel
Width int
Context *templateParseContext
App *ApplicationModel
HelpFlagIsSet bool
Width int
Context *templateParseContext
}

// UsageForContext displays usage information from a ParseContext (obtained from
Expand Down Expand Up @@ -162,6 +164,33 @@ func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent
}
return rows
},
"GlobalFlags": func(c *templateParseContext) []*FlagModel {
if c.SelectedCommand == nil {
return c.Flags
}

var sflags []string
for _, f := range c.SelectedCommand.Flags {
sflags = append(sflags, f.Name)
}

var globals []*FlagModel
for _, f := range c.Flags {
var known bool
for _, sf := range sflags {
if f.Name == sf {
known = true
break
}
}

if !known {
globals = append(globals, f)
}
}

return globals
},
"RequiredFlags": func(f []*FlagModel) []*FlagModel {
requiredFlags := []*FlagModel{}
for _, flag := range f {
Expand Down Expand Up @@ -251,8 +280,9 @@ func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent
selectedCommand = context.SelectedCommand.Model()
}
ctx := templateContext{
App: a.Model(),
Width: width,
App: a.Model(),
Width: width,
HelpFlagIsSet: a.helpFlagIsSet,
Context: &templateParseContext{
SelectedCommand: selectedCommand,
FlagGroupModel: context.flags.Model(),
Expand Down

0 comments on commit f95ab53

Please sign in to comment.