Skip to content

Commit 115ed8a

Browse files
Simplify the subcommand example code (#137)
1 parent 7272e33 commit 115ed8a

5 files changed

Lines changed: 5 additions & 18 deletions

File tree

command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ func defaultHelp(cmd *Command) error {
536536
writePositionalArgs(cmd, s)
537537
} else {
538538
// We have no named arguments so do the best we can
539+
// TODO(@FollowTheProcess): Can we detect if cli.NoArgs was used in which case
540+
// omit this
539541
s.WriteString("ARGS...")
540542
}
541543
} else {

docs/img/namedargs.gif

-9.69 KB
Loading

docs/img/quickstart.gif

579 Bytes
Loading

docs/img/subcommands.gif

1.97 KB
Loading

examples/subcommands/cli.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func BuildCLI() (*cli.Command, error) {
12-
demo, err := cli.New(
12+
return cli.New(
1313
"demo",
1414
cli.Short("An example CLI to demonstrate the library and play with it for real."),
1515
cli.Version("dev"),
@@ -20,11 +20,6 @@ func BuildCLI() (*cli.Command, error) {
2020
cli.Allow(cli.NoArgs()),
2121
cli.SubCommands(buildSayCommand, buildDoCommand),
2222
)
23-
if err != nil {
24-
return nil, err
25-
}
26-
27-
return demo, nil
2823
}
2924

3025
type sayOptions struct {
@@ -35,7 +30,7 @@ type sayOptions struct {
3530

3631
func buildSayCommand() (*cli.Command, error) {
3732
var options sayOptions
38-
say, err := cli.New(
33+
return cli.New(
3934
"say",
4035
cli.Short("Print a message"),
4136
cli.Example("Say a well known phrase", "demo say hello world"),
@@ -45,11 +40,6 @@ func buildSayCommand() (*cli.Command, error) {
4540
cli.Flag(&options.count, "count", 'c', 0, "Count the things"),
4641
cli.Flag(&options.thing, "thing", 't', "", "Name of the thing"),
4742
)
48-
if err != nil {
49-
return nil, err
50-
}
51-
52-
return say, nil
5343
}
5444

5545
type doOptions struct {
@@ -61,7 +51,7 @@ type doOptions struct {
6151

6252
func buildDoCommand() (*cli.Command, error) {
6353
var options doOptions
64-
do, err := cli.New(
54+
return cli.New(
6555
"do",
6656
cli.Short("Do a thing"),
6757
cli.Example("Do something", "demo do something --fast"),
@@ -74,11 +64,6 @@ func buildDoCommand() (*cli.Command, error) {
7464
cli.Flag(&options.duration, "duration", 'd', 1*time.Second, "Do the thing for a specific duration"),
7565
cli.Run(runDo(&options)),
7666
)
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
return do, nil
8267
}
8368

8469
func runSay(options *sayOptions) func(cmd *cli.Command, args []string) error {

0 commit comments

Comments
 (0)