Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit eacbe8f

Browse files
committed
Clean up docs
1 parent 4579777 commit eacbe8f

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
# cli
22

3-
A minimal, command-oriented CLI packages.
3+
A minimal, command-oriented CLI package.
44

55
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/go.coder.com/cli)
66

77
## Features
88

99
- Very small, simple API.
10-
- Uses stdlib as much as possible.
11-
- 0 dependencies.
10+
- Uses standard `flag` package as much as possible.
11+
- No external dependencies.
12+
- Subcommands.
13+
- Auto-generated help.
14+
15+
## Install
16+
17+
```bash
18+
go get -u go.coder.com/cli
19+
```
1220

1321
## Examples
1422

@@ -82,7 +90,6 @@ func (c *subcmd) Spec() cli.CommandSpec {
8290
}
8391

8492
type cmd struct {
85-
message string
8693
}
8794

8895
func (c *cmd) Run(fl *flag.FlagSet) {
@@ -105,8 +112,6 @@ func (c *cmd) Subcommands() []cli.Command {
105112
}
106113

107114
func main() {
108-
cli.RunRoot(&cmd{message: "subcommands"})
115+
cli.RunRoot(&cmd{})
109116
}
110-
111-
112117
```

doc.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@
1111
// cli.FlaggedCommand
1212
// cli.ParentCommand
1313
// } = new(rootCmd)
14-
//
15-
// This package is exported since we think it could be interesting to the community,
16-
// but the API is subject to change in support of sail.
1714
package cli

examples/subcommands/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func (c *subcmd) Spec() cli.CommandSpec {
2323
}
2424

2525
type cmd struct {
26-
message string
2726
}
2827

2928
func (c *cmd) Run(fl *flag.FlagSet) {
@@ -46,5 +45,5 @@ func (c *cmd) Subcommands() []cli.Command {
4645
}
4746

4847
func main() {
49-
cli.RunRoot(&cmd{message: "subcommands"})
48+
cli.RunRoot(&cmd{})
5049
}

run.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
package cli
22

33
import (
4-
"flag"
5-
"os"
4+
"flag"
5+
"os"
66
)
77

88
func appendParent(parent string, add string) string {
9-
if parent == "" {
10-
return add + " "
11-
}
12-
return parent + add + " "
9+
if parent == "" {
10+
return add + " "
11+
}
12+
return parent + add + " "
1313
}
1414

15-
// Run executes sets up flags, helps, and executes the command with the provided
15+
// Run sets up flags, helps, and executes the command with the provided
1616
// arguments.
1717
//
1818
// parents is the list of parent commands.
1919
// E.g the parent for `sail run hello` would be `sail run`.
2020
//
2121
// Use RunRoot if this package is managing the entire CLI.
2222
func Run(cmd Command, args []string, parent string) {
23-
fl := flag.NewFlagSet(parent+""+cmd.Spec().Name, flag.ExitOnError)
24-
25-
if fc, ok := cmd.(FlaggedCommand); ok {
26-
fc.RegisterFlags(fl)
27-
}
28-
29-
fl.Usage = func() {
30-
renderHelp(cmd, fl, os.Stderr)
31-
}
32-
_ = fl.Parse(args)
33-
34-
subcommandArg := fl.Arg(0)
35-
36-
// Route to subcommand.
37-
if pc, ok := cmd.(ParentCommand); ok && subcommandArg != "" {
38-
for _, subcommand := range pc.Subcommands() {
39-
if subcommand.Spec().Name != subcommandArg {
40-
continue
41-
}
42-
43-
Run(
44-
subcommand, fl.Args()[1:],
45-
appendParent(parent, cmd.Spec().Name),
46-
)
47-
return
48-
}
49-
}
50-
51-
cmd.Run(fl)
23+
fl := flag.NewFlagSet(parent+""+cmd.Spec().Name, flag.ExitOnError)
24+
25+
if fc, ok := cmd.(FlaggedCommand); ok {
26+
fc.RegisterFlags(fl)
27+
}
28+
29+
fl.Usage = func() {
30+
renderHelp(cmd, fl, os.Stderr)
31+
}
32+
_ = fl.Parse(args)
33+
34+
subcommandArg := fl.Arg(0)
35+
36+
// Route to subcommand.
37+
if pc, ok := cmd.(ParentCommand); ok && subcommandArg != "" {
38+
for _, subcommand := range pc.Subcommands() {
39+
if subcommand.Spec().Name != subcommandArg {
40+
continue
41+
}
42+
43+
Run(
44+
subcommand, fl.Args()[1:],
45+
appendParent(parent, cmd.Spec().Name),
46+
)
47+
return
48+
}
49+
}
50+
51+
cmd.Run(fl)
5252
}
5353

5454
// RunRoot calls Run with the process's arguments.
5555
func RunRoot(cmd Command) {
56-
Run(cmd, os.Args[1:], "")
56+
Run(cmd, os.Args[1:], "")
5757
}

0 commit comments

Comments
 (0)