Skip to content

Commit

Permalink
cli.Short and cli.Long now strip leading and trailing whitespace (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jan 3, 2025
1 parent c15c4f3 commit 387756b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[default.extend-words]
byt = "byt" # Useful name as byte is a keyword in go
8 changes: 8 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,16 @@ tasks:

- sh: command -v nilaway
msg: nilaway not installed, see https://github.com/uber-go/nilaway

- sh: command -v betteralign
msg: requires betteralign, run `go install github.com/dkorunic/betteralign/cmd/betteralign@latest`

- sh: command -v typos
msg: requires typos-cli, run `brew install typos-cli`
cmds:
- betteralign -test_files -apply ./...
- golangci-lint run --fix
- typos
- nilaway ./...

docs:
Expand Down
12 changes: 11 additions & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func TestHelp(t *testing.T) {
wantErr: false,
},
{
name: "default.short",
name: "default short",
options: []cli.Option{
cli.OverrideArgs([]string{"-h"}),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
Expand Down Expand Up @@ -463,6 +463,16 @@ func TestHelp(t *testing.T) {
},
wantErr: false,
},
{
name: "full description strip whitespace",
options: []cli.Option{
cli.OverrideArgs([]string{"--help"}),
cli.Short(" \t\n A cool CLI to do things \n "),
cli.Long(" \t\n\n A longer, probably multiline description \t\n\n "),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
wantErr: false,
},
{
name: "with no description",
options: []cli.Option{
Expand Down
4 changes: 2 additions & 2 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ type signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}

// unsigned is the same as constraints.Unsigned but we don't hve to depend
// unsigned is the same as constraints.Unsigned but we don't have to depend
// on golang/x/exp.
type unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
Expand Down Expand Up @@ -456,7 +456,7 @@ func cast[T2 any, T1 any](v *T1) *T2 {

// validateFlagName ensures a flag name is valid, returning an error if it's not.
//
// Flags names must be all lower case ASCII letters, a hypen separator is allowed e.g. "set-default"
// Flags names must be all lower case ASCII letters, a hyphen separator is allowed e.g. "set-default"
// but this must be in between letters, not leading or trailing.
func validateFlagName(name string) error {
if name == "" {
Expand Down
9 changes: 7 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"slices"
"strings"

"github.com/FollowTheProcess/cli/internal/flag"
)
Expand Down Expand Up @@ -162,6 +163,8 @@ func Stderr(stderr io.Writer) Option {
// The one line usage will appear in the help text as well as alongside
// subcommands when they are listed.
//
// For consistency of formatting, all leading and trailing whitespace is stripped.
//
// Successive calls will simply overwrite any previous calls.
//
// cli.New("rm", cli.Short("Delete files and directories"))
Expand All @@ -170,7 +173,7 @@ func Short(short string) Option {
if short == "" {
return errors.New("cannot set command short description to an empty string")
}
cfg.short = short
cfg.short = strings.TrimSpace(short)
return nil
}
return option(f)
Expand All @@ -181,6 +184,8 @@ func Short(short string) Option {
// The long description will appear in the help text for a command. Users
// are responsible for wrapping the text at a sensible width.
//
// For consistency of formatting, all leading and trailing whitespace is stripped.
//
// Successive calls will simply overwrite any previous calls.
//
// cli.New("rm", cli.Long("... lots of text here"))
Expand All @@ -189,7 +194,7 @@ func Long(long string) Option {
if long == "" {
return errors.New("cannot set command long description to an empty string")
}
cfg.long = long
cfg.long = strings.TrimSpace(long)
return nil
}
return option(f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
A cool CLI to do things

A longer, probably multiline description

Usage: test [OPTIONS] ARGS...

Options:
-h --help bool Show help for test
-V --version bool Show version info for test

0 comments on commit 387756b

Please sign in to comment.