Skip to content

FlagUsagesWrapped stops wrapping the rest of a usage string after a word wider than the description column #501

Description

@mahnerak

When a usage string contains a word too wide to fit the remaining space, wrapN returns the entire rest of the string unwrapped — not just the over-wide word. Everything after it lands on one long line.

flag.go, in wrapN:

w := strings.LastIndexAny(s[:i], " \t\n")
if w <= 0 {
      return s, ""   // <- gives up on the whole remainder, not just this word
}

Reproducer (similar to apptainer/apptainer#3668 )

package main

import (
      "fmt"

      "github.com/spf13/pflag"
)

func main() {
      fs := pflag.NewFlagSet("example", pflag.ContinueOnError)
      fs.String("mount", "", "a mount specification e.g. 'type=bind,source=/opt,destination=/hostopt'. The rethis description is never wrapped.")
      fmt.Print(fs.FlagUsagesWrapped(60))
}

Actual — the second line is 116 columns:

      --mount string   a mount specification e.g.
                       'type=bind,source=/opt,destination=/hostopt'. The rest of this description is never wrapped.

Expected — the unbreakable word overflows on a line of its own, and wrapping resumes:

      --mount string   a mount specification e.g.
                       'type=bind,source=/opt,destination=/hostopt'.
                       The rest of this description is
                       never wrapped.

I'll try to draft a quick PR, but before that would like @spf13 to confirm we agree on the "Expected" behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions