Skip to content

Commit

Permalink
feat: set default values for options and arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
steverusso committed Sep 30, 2024
1 parent cf26f85 commit 42f866d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
47 changes: 43 additions & 4 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,19 @@ func (g *generator) genCmdUsageFunc(c *command) error {
for i, o := range c.Opts {
switch g.usgLayoutKind {
case "roomy":
var extra string
{
if v, ok := o.data.getConfig("default"); ok {
extra += "\n [default: " + v + "]"
}
if v, ok := o.data.getConfig("env"); ok {
extra += "\n [env: " + v + "]"
}
}
content := " " + o.usgNameAndArg() + "\n"
content += " " + wrapBlurb(o.data.Blurb, 6, g.usgTextWidth)
if v, ok := o.data.getConfig("env"); ok {
content += "\n\n [env: " + v + "]"
if extra != "" {
content += "\n" + extra
}
if i < len(c.Opts)-1 {
content += "\n"
Expand All @@ -193,6 +202,9 @@ func (g *generator) genCmdUsageFunc(c *command) error {
default:
paddedNameAndArg := fmt.Sprintf(" %-*s ", optNameColWidth, o.usgNameAndArg())
desc := o.data.Blurb
if v, ok := o.data.getConfig("default"); ok {
desc += " (default: " + v + ")"
}
if v, ok := o.data.getConfig("env"); ok {
desc += " [$" + v + "]"
}
Expand All @@ -212,10 +224,19 @@ func (g *generator) genCmdUsageFunc(c *command) error {
for i, a := range c.Args {
switch g.usgLayoutKind {
case "roomy":
var extra string
{
if v, ok := a.data.getConfig("default"); ok {
extra += "\n [default: " + v + "]"
}
if v, ok := a.data.getConfig("env"); ok {
extra += "\n [env: " + v + "]"
}
}
content := " " + a.UsgName() + "\n"
content += " " + wrapBlurb(a.data.Blurb, 6, g.usgTextWidth)
if v, ok := a.data.getConfig("env"); ok {
content += "\n\n [env: " + v + "]"
if extra != "" {
content += "\n" + extra
}
if i < len(c.Args)-1 {
content += "\n"
Expand All @@ -224,6 +245,9 @@ func (g *generator) genCmdUsageFunc(c *command) error {
default:
paddedName := fmt.Sprintf(" %-*s ", argNameColWidth, a.UsgName())
desc := a.data.Blurb
if v, ok := a.data.getConfig("default"); ok {
desc += " (default: " + v + ")"
}
if v, ok := a.data.getConfig("env"); ok {
desc += " [$" + v + "]"
}
Expand Down Expand Up @@ -276,6 +300,21 @@ func (g *generator) genCmdParseFunc(c *command) error {
return nil
}

func (c *command) Defaults() string {
var s string
for _, o := range c.Opts {
if defVal, ok := o.data.getConfig("default"); ok {
s += fmt.Sprintf("\tc.%s = %s\n", o.FieldName, defVal)
}
}
for _, a := range c.Args {
if defVal, ok := a.data.getConfig("default"); ok {
s += fmt.Sprintf("\tc.%s = %s\n", a.FieldName, defVal)
}
}
return s
}

func (c *command) Parents() string {
s := ""
for i := range c.parentNames {
Expand Down
2 changes: 2 additions & 0 deletions tmpls/parsefunc.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

func (c *{{ .TypeName }}) Parse(args []string) {
{{- with .Defaults }}
{{ . }}{{ end }}
p := clapCommand{
usage: c.UsageHelp,

Expand Down

0 comments on commit 42f866d

Please sign in to comment.