https://github.com/x-mod/cmd
More convenient command-line builder base on cobra
https://github.com/x-mod/cmd
Last synced: 5 months ago
JSON representation
More convenient command-line builder base on cobra
- Host: GitHub
- URL: https://github.com/x-mod/cmd
- Owner: x-mod
- License: mit
- Created: 2019-08-11T14:42:12.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-31T14:53:24.000Z (almost 5 years ago)
- Last Synced: 2024-06-19T15:14:04.230Z (almost 2 years ago)
- Language: Go
- Size: 2.15 MB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
cmd
===
More convenient commands builder base on **Cobra**.The key feature of the package:
- use filesystem path like format to route the subcommands
## Installation
````bash
$: go get github.com/x-mod/cmd
````
## Dependence
- [cobra](https://github.com/spf13/cobra)
## Quick Start
### Only Root Command
replace default Root Command settings. `cmd.Parent("")` means replace the default Root Command with the new command.
````go
import (
"fmt"
"github.com/x-mod/cmd"
)
func main() {
cmd.Add(
cmd.Name("root"),
cmd.Main(Main),
)
cmd.Execute()
}
func Main(c *cmd.Command, args []string) error {
fmt.Println("my root command running ...")
return nil
}
````
run the code in bash:
````bash
$: go run main.go
my root command running ...
````
### Sub Commands
sub commands routing rules:
- `cmd.Path("/")` root command
- `cmd.Parent("/foo/bar")` 3 level command
subcommand's `cmd.Parent("/command/path")` must be setting.
````go
import (
"fmt"
"github.com/x-mod/cmd"
)
func main() {
cmd.Add(
cmd.Path("/foo/bar/v1"),
cmd.Main(V1),
).PersistentFlags().StringP("parameter", "p", "test", "flags usage")
cmd.Version("version string")
cmd.Execute()
}
func V1(c *cmd.Command, args []string) error {
fmt.Println("V1 called")
return nil
}
````
run the code in bash:
````bash
$: go run main.go foo bar v1
V1 called
````