Skip to content

Commit 45e67fd

Browse files
committed
fixed help screen: added envvars list
1 parent 04d8890 commit 45e67fd

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

cli/flag.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,18 @@ func (f *Flag) GetDottedNamePath() string {
380380
func (f *Flag) ensureXref() {
381381
//
382382
}
383+
384+
func (c *Flag) EnvVarsHelpString(trans func(ss string, clr color.Color) string, clr, clrDefault color.Color) (hs, plain string) {
385+
if len(c.envVars) != 0 {
386+
var envVars []string
387+
for _, v := range c.envVars {
388+
if v != "" {
389+
envVars = append(envVars, v)
390+
}
391+
}
392+
dep := strings.Join(envVars, ",")
393+
plain = fmt.Sprintf("[Env: %s]", dep)
394+
hs = trans(fmt.Sprintf("[Env: <font color=%v>%s</font>]", color.ToColorString(clr), dep), clrDefault)
395+
}
396+
return
397+
}

cli/worker/printer.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
516516
tg := ff.ToggleGroupLeadHelpString()
517517
def, defPlain := ff.DefaultValueHelpString(trans, CurrentDefaultValueColor, CurrentDescColor)
518518
dep, depPlain := ff.DeprecatedHelpString(trans, CurrentDeprecatedColor, CurrentDescColor)
519+
env, envPlain := ff.EnvVarsHelpString(trans, CurrentEnvVarsColor, CurrentDescColor)
519520

520521
if (ff.Hidden() && *verboseCount > 0) || (ff.VendorHidden() && *verboseCount >= 3) { //nolint:revive
521522
s.WriteBgColor(sb, color.BgDim)
@@ -547,7 +548,7 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
547548
if right != "" {
548549
s.WriteColor(sb, CurrentDescColor)
549550

550-
_, l, l1st := len(right), len(right)+len(defPlain)+len(depPlain), len(tg)
551+
_, l, l1st := len(right), len(right)+len(defPlain)+len(depPlain)+len(envPlain), len(tg)
551552
// aa := []string{}
552553
if l+l1st >= rCols {
553554
var prt string
@@ -584,12 +585,28 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
584585
printed += 4
585586
}
586587

588+
if env != "" && printed >= 0 {
589+
if split {
590+
envlen := len(envPlain)
591+
printed += envlen
592+
if printed >= rCols {
593+
printleftpad(split)
594+
printed = envlen
595+
}
596+
}
597+
if sb.String()[sb.Len()-1] != ' ' {
598+
_, _ = sb.WriteString(" ")
599+
}
600+
_, _ = sb.WriteString(env)
601+
}
602+
587603
if def != "" && printed >= 0 {
588604
if split {
589-
deflen := len(is.StripEscapes(def))
605+
deflen := len(defPlain) // len(is.StripEscapes(def))
590606
printed += deflen
591607
if printed >= rCols {
592608
printleftpad(split)
609+
printed = deflen
593610
}
594611
}
595612
if sb.String()[sb.Len()-1] != ' ' {
@@ -600,10 +617,11 @@ func (s *helpPrinter) printFlag(ctx context.Context, sb *strings.Builder, verbos
600617

601618
if dep != "" {
602619
if split {
603-
deplen := len(is.StripEscapes(dep))
620+
deplen := len(depPlain) // len(is.StripEscapes(dep))
604621
printed += deplen
605622
if printed >= rCols {
606623
printleftpad(split)
624+
printed = deplen
607625
}
608626
}
609627
if sb.String()[sb.Len()-1] != ' ' {
@@ -657,6 +675,8 @@ var (
657675
// CurrentDeprecatedColor the print color for deprecated opt line
658676
CurrentDeprecatedColor = color.FgDarkGray
659677

678+
CurrentEnvVarsColor = color.FgLightGray
679+
660680
// CurrentDescColor the print color for description line
661681
CurrentDescColor = color.FgDarkGray
662682
// CurrentDefaultValueColor the print color for default value line

0 commit comments

Comments
 (0)