Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align the argument required/default specifier using tabwriter #127

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tasks:
msg: golines not installed, see https://github.com/segmentio/golines
cmds:
- go fmt ./...
- golines . --ignore-generated --write-output --max-len 140
- golines . --ignore-generated --write-output --max-len 120

test:
desc: Run the test suite
Expand Down
6 changes: 3 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ func writeArgumentsSection(cmd *Command, s *strings.Builder) error {
for _, arg := range cmd.positionalArgs {
switch arg.defaultValue {
case requiredArgMarker:
tab.Row(" %s\t%s [required]\n", colour.Bold(arg.name), arg.description)
tab.Row(" %s\t%s\t[required]\n", colour.Bold(arg.name), arg.description)
case "":
tab.Row(" %s\t%s [default %q]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
tab.Row(" %s\t%s\t[default %q]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
default:
tab.Row(" %s\t%s [default %s]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
tab.Row(" %s\t%s\t[default %s]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
}
}
if err := tab.Flush(); err != nil {
Expand Down
75 changes: 61 additions & 14 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,59 @@ func TestSubCommandExecute(t *testing.T) {
wantErr: false,
},
{
name: "invoke sub1 with arg terminator",
stdout: "Hello from sub1, my args were: [my subcommand args more args here], force was true, something was here, extra args: [more args here]",
stderr: "",
args: []string{"sub1", "my", "subcommand", "args", "--force", "--something", "here", "--", "more", "args", "here"},
name: "invoke sub1 with arg terminator",
stdout: "Hello from sub1, my args were: [my subcommand args more args here], force was true, something was here, extra args: [more args here]",
stderr: "",
args: []string{
"sub1",
"my",
"subcommand",
"args",
"--force",
"--something",
"here",
"--",
"more",
"args",
"here",
},
wantErr: false,
},
{
name: "invoke sub1 with sub1 in the arg list",
stdout: "Hello from sub1, my args were: [my sub1 args sub1 more args here], force was true, something was here, extra args: []",
stderr: "",
args: []string{"sub1", "my", "sub1", "args", "sub1", "--force", "--something", "here", "more", "args", "here"},
name: "invoke sub1 with sub1 in the arg list",
stdout: "Hello from sub1, my args were: [my sub1 args sub1 more args here], force was true, something was here, extra args: []",
stderr: "",
args: []string{
"sub1",
"my",
"sub1",
"args",
"sub1",
"--force",
"--something",
"here",
"more",
"args",
"here",
},
wantErr: false,
},
{
name: "invoke sub1 with sub1 as a flag value",
stdout: "Hello from sub1, my args were: [my subcommand args more args here], force was true, something was sub2, extra args: []",
stderr: "",
args: []string{"sub1", "my", "subcommand", "args", "--force", "--something", "sub2", "more", "args", "here"},
name: "invoke sub1 with sub1 as a flag value",
stdout: "Hello from sub1, my args were: [my subcommand args more args here], force was true, something was sub2, extra args: []",
stderr: "",
args: []string{
"sub1",
"my",
"subcommand",
"args",
"--force",
"--something",
"sub2",
"more",
"args",
"here",
},
wantErr: false,
},
{
Expand Down Expand Up @@ -334,7 +369,13 @@ func TestPositionalArgs(t *testing.T) {
cli.OptionalArg("dest", "The destination path", "dest.txt"), // Dest has a default
cli.RequiredArg("something", "Another arg"), // Required again
cli.Run(func(cmd *cli.Command, args []string) error {
fmt.Fprintf(cmd.Stdout(), "src: %s, dest: %s, something: %s\n", cmd.Arg("src"), cmd.Arg("dest"), cmd.Arg("something"))
fmt.Fprintf(
cmd.Stdout(),
"src: %s, dest: %s, something: %s\n",
cmd.Arg("src"),
cmd.Arg("dest"),
cmd.Arg("something"),
)
return nil
}),
},
Expand All @@ -349,7 +390,13 @@ func TestPositionalArgs(t *testing.T) {
cli.OptionalArg("dest", "The destination path", "default-dest.txt"), // Dest has a default
cli.RequiredArg("something", "Another arg"), // Required again
cli.Run(func(cmd *cli.Command, args []string) error {
fmt.Fprintf(cmd.Stdout(), "src: %s, dest: %s, something: %s\n", cmd.Arg("src"), cmd.Arg("dest"), cmd.Arg("something"))
fmt.Fprintf(
cmd.Stdout(),
"src: %s, dest: %s, something: %s\n",
cmd.Arg("src"),
cmd.Arg("dest"),
cmd.Arg("something"),
)
return nil
}),
},
Expand Down
Binary file modified docs/img/quickstart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion examples/namedargs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func run() error {
cmd, err := cli.New(
"copy", // A fictional copy command
cli.Short("Copy a file from a src to a destination"),
cli.RequiredArg("src", "The file to copy from"), // src is required, failure to provide it will error
cli.RequiredArg(
"src",
"The file to copy from",
), // src is required, failure to provide it will error
cli.OptionalArg("dest", "The destination to copy to", "./dest"), // dest has a default if not provided
cli.Stdout(os.Stdout),
cli.Example("Copy a file to somewhere", "copy src.txt ./some/where/else"),
Expand Down
7 changes: 6 additions & 1 deletion internal/flag/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func TestParse(t *testing.T) {
test.False(t, exists)
test.Equal(t, f, nil)

test.EqualFunc(t, set.Args(), []string{"some", "args", "here", "no", "flags", "extra", "args"}, slices.Equal)
test.EqualFunc(
t,
set.Args(),
[]string{"some", "args", "here", "no", "flags", "extra", "args"},
slices.Equal,
)
test.EqualFunc(t, set.ExtraArgs(), []string{"extra", "args"}, slices.Equal)
},
args: []string{"some", "args", "here", "no", "flags", "--", "extra", "args"},
Expand Down
6 changes: 3 additions & 3 deletions testdata/snapshots/TestHelp/with_named_arguments.snap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ A placeholder for something cool
Usage: test [OPTIONS] SRC [DEST] [OTHER]

Arguments:
src The file to copy [required]
dest Destination to copy to [default ./dest]
other Something else [default ""]
src The file to copy [required]
dest Destination to copy to [default ./dest]
other Something else [default ""]

Options:
-h --help bool Show help for test
Expand Down
4 changes: 2 additions & 2 deletions testdata/snapshots/TestHelp/with_verbosity_count.snap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ A placeholder for something cool
Usage: test [OPTIONS] SRC [DEST]

Arguments:
src The file to copy [required]
dest Destination to copy to [default ./dest]
src The file to copy [required]
dest Destination to copy to [default ./dest]

Options:
-h --help bool Show help for test
Expand Down
Loading