Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

20 Commits

Repository files navigation

cmd

More convenient commands builder base on Cobra.The key feature of the package:

  • use filesystem path like format to route the subcommands

Installation

$: go get github.com/x-mod/cmd

Dependence

Quick Start

Only Root Command

replace default Root Command settings. cmd.Parent("") means replace the default Root Command with the new command.

import (
"fmt""github.com/x-mod/cmd"
)
funcmain() {
cmd.Add(
cmd.Name("root"),
cmd.Main(Main),
)
cmd.Execute()
}
funcMain(c*cmd.Command, args []string) error {
fmt.Println("my root command running ...")
returnnil
}

run the code in 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.

import (
"fmt""github.com/x-mod/cmd"
)
funcmain() {
cmd.Add(
cmd.Path("/foo/bar/v1"),
cmd.Main(V1),
).PersistentFlags().StringP("parameter", "p", "test", "flags usage")
cmd.Version("version string")
cmd.Execute()
}
funcV1(c*cmd.Command, args []string) error {
fmt.Println("V1 called")
returnnil
}

run the code in bash:

$: go run main.go foo bar v1
V1 called

About

More convenient command-line builder base on cobra

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages