cmd

package module
v0.2.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2021 License: MIT Imports: 9 Imported by: 8

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute()

Execute for default root command

func ExitCode added in v0.2.9

func ExitCode(enable bool)

ExitCode

func Flags

func Flags() *pflag.FlagSet

Flags

func PersistentFlags

func PersistentFlags() *pflag.FlagSet

PersistentFlags

func Version

func Version(v string)

Version root command

Types

type Command

type Command struct {
	*cobra.Command
	// contains filtered or unexported fields
}

Command struct

func Add

func Add(opts ...CommandOpt) *Command

Add for default root command

func (*Command) Execute

func (c *Command) Execute() error

Execute command

type CommandOpt

type CommandOpt func(*Command)

CommandOpt command option definition

func Description

func Description(desc string) CommandOpt

Description of command

func Main

func Main(main MainFunc) CommandOpt

Main of command

func Name

func Name(name string) CommandOpt

Name of command

func Parent added in v0.1.0

func Parent(parent string) CommandOpt

Parent of command

func Path added in v0.2.1

func Path(p string) CommandOpt

Path of command

func Short

func Short(short string) CommandOpt

Short Description of command

type MainFunc

type MainFunc func(cmd *Command, args []string) error

MainFunc type

Directories

Path Synopsis
example
foo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL