cmd

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2019 License: MIT Imports: 7 Imported by: 8

README

cmd

More convenient commands builder base on Cobra.

Installation

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

Dependence

Quick Start

Only Root Command
package main

import (
	"fmt"

	"github.com/x-mod/cmd"
)

func main() {
    cmd.RootMain(RootMain)
    cmd.Version("specified version")
	cmd.Exit(cmd.Execute())
}

func RootMain(c *cmd.Command, args []string) {
	fmt.Println("my root command running ...")
}
Sub Commands
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/x-mod/cmd"
)

func main() {
	cmd.Add(
		cmd.Name("svc"),
		cmd.Short("service short name"),
		cmd.SubCommand(
			cmd.NewCommand(
				cmd.Name("v1"),
				cmd.Short("version 1"),
				cmd.Main(V1),
			),
		),
	)
	cmd.Version("version string")
	cmd.Exit(cmd.Execute())
}

func V1(c *cmd.Command, args []string) {
	fmt.Println("V1 called", filepath.Base(os.Args[0]))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute() error

Execute for default root command

func Exit

func Exit(err error)

Exit with error code

func Flags

func Flags() *pflag.FlagSet

Flags

func PersistentFlags

func PersistentFlags() *pflag.FlagSet

PersistentFlags

func RootMain

func RootMain(rootFn MainFunc)

Main root command

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 NewCommand

func NewCommand(opts ...CommandOpt) *Command

NewCommand create a new command

func (*Command) Add

func (c *Command) Add(sub *Command)

Add subcommand

func (*Command) Execute

func (c *Command) Execute() error

Execute command

func (*Command) Flags

func (c *Command) Flags() *pflag.FlagSet

Flags

func (*Command) PersistentFlags

func (c *Command) PersistentFlags() *pflag.FlagSet

PersistentFlags

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 Short

func Short(short string) CommandOpt

Short Description of command

func SubCommand

func SubCommand(sub *Command) CommandOpt

SubCommand of command

type MainFunc

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

MainFunc type

Directories

Path Synopsis
example
foo command
root command

Jump to

Keyboard shortcuts

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