Categorygithub.com/x-mod/cmd
modulepackage
0.2.12
Repository: https://github.com/x-mod/cmd.git
Documentation: pkg.go.dev

# 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

$: 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"
)

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:

$: 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"
)

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:

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

# Packages

No description provided by the author

# Functions

Add for default root command.
Description of command.
Execute for default root command.
ExitCode.
Flags.
Main of command.
Name of command.
Parent of command.
Path of command.
PersistentFlags.
Short Description of command.
Version root command.

# Structs

Command struct.

# Type aliases

CommandOpt command option definition.
MainFunc type.