Documentation ¶
Overview ¶
Package console eases the creation of beautiful and testable command line interfaces. The Console package allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs. console application can be written as follows:
//cmd/console/main.go func main() { console.New().Execute(context.Background()) }
Then, you can register the commands using Add():
package main import ( "context" "gitoa.ru/go-4devs/console" "gitoa.ru/go-4devs/console/example/pkg/command" ) func main() { console. New(). Add( command.Hello(), command.Args(), command.Hidden(), command.Namespace(), ). Execute(context.Background()) }
Index ¶
- Constants
- Variables
- func Commands() []string
- func Default(d *input.Definition) *input.Definition
- func Execute(ctx context.Context, cmd *Command, opts ...func(*App))
- func MustRegister(cmd *Command)
- func Register(cmd *Command) error
- func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) error
- func WithExit(f func(int)) func(*App)
- func WithInput(in input.Input) func(*App)
- func WithOutput(out output.Output) func(*App)
- func WithSkipArgs(l int) func(*App)
- type Action
- type AlternativesError
- type App
- type Command
- type Configure
- type Handle
- type Option
- type Prepare
Examples ¶
Constants ¶
const ( OptionHelp = "help" OptionVersion = "version" OptionAnsi = "ansi" OptionNoAnsi = "no-ansi" OptionQuiet = "quiet" OptionVerbose = "verbose" )
const ( ArgumentCommandName = "command_name" OptionFormat = "format" )
const ( CommandHelp = "help" CommandList = "list" )
const (
ArgumentNamespace = "namespace"
)
Variables ¶
Functions ¶
func Commands ¶
func Commands() []string
Commands returns a sorted list of the names of the registered commands.
func Default ¶
func Default(d *input.Definition) *input.Definition
Default options and argument command.
func Execute ¶
Execute the current command with option.
Example ¶
cmd := Command() ctx := context.Background() in := &input.Array{} // Run command: ./bin "argument value" -b --string="same value" --string="other value" in.SetOption("bool", value.New(true)) in.SetOption("string", value.New([]string{"same value", "other value"})) in.SetArgument("test_argument", value.New("argument value")) console.Execute(ctx, cmd, console.WithInput(in), console.WithExit(func(int) {}))
Output: test argument:argument value bool option:true duration option with default:1s array string:[same value,other value]
func Register ¶
Register makes a command available execute in app. If Register is called twice with the same name or if driver is nil, return error.
func Run ¶
Run current command by input and output.
Example ¶
cmd := Command() ctx := context.Background() out := output.Stdout() in := &input.Array{} err := console.Run(ctx, cmd, in, out) fmt.Println("err:", err)
Output: test argument: bool option:false duration option with default:1s array string:[] err: <nil>
func WithOutput ¶
WithOutput sets outpu,^ by default output os.Stdout.
func WithSkipArgs ¶
WithSkipArgs sets how many arguments are passed. For example, you don't need to pass the name of a single command.
Types ¶
type AlternativesError ¶ added in v0.1.0
func (AlternativesError) Error ¶ added in v0.1.0
func (e AlternativesError) Error() string
func (AlternativesError) Is ¶ added in v0.1.0
func (e AlternativesError) Is(err error) bool
func (AlternativesError) Unwrap ¶ added in v0.1.0
func (e AlternativesError) Unwrap() error
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is collection of command and configure env.
func New ¶
New creates and configure new console app.
Example (Help) ¶
ctx := context.Background() os.Args = []string{ "bin/console", "test:command", "-h", "--no-ansi", } console.New(console.WithExit(func(int) {})). Add( Command(), ). Execute(ctx)
Output: Description: test command Usage: test:command [options] [--] [<test_argument>] Arguments: test_argument test argument Options: --duration[=DURATION] test duration with default [default: 1s] --bool test bool option --string[=STRING] array string (multiple values allowed) -q, --quiet Do not output any message -v, --verbose Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace (multiple values allowed) -h, --help Display this help message -V, --version Display this application version --ansi Do not ask any interactive question --no-ansi Disable ANSI output
Example (List) ¶
ctx := context.Background() os.Args = []string{ "bin/console", "--no-ansi", } console.New(console.WithExit(func(int) {})). Add( Command(), &console.Command{ Name: "fdevs:console:arg", Description: "Understanding how Console Arguments and Options Are Handled", }, &console.Command{ Name: "fdevs:console:hello", Description: "example hello command", }, &console.Command{ Name: "app:start", Description: "example command in other namespace", }, ). Execute(ctx)
Output: Usage: command [options] [arguments] Options: -q, --quiet Do not output any message -v, --verbose Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace (multiple values allowed) -h, --help Display this help message -V, --version Display this application version --ansi Do not ask any interactive question --no-ansi Disable ANSI output Available commands: help Displays help for a command list Lists commands app app:start example command in other namespace fdevs fdevs:console:arg Understanding how Console Arguments and Options Are Handled fdevs:console:hello example hello command fdevs:console:test test command test test:command test command
type Command ¶
type Command struct { // The name of the command. Name string // A short description of the usage of this command. Description string // A longer explanation of how the command works. Help string // Vervion command. Version string // Boolean to hide this command from help or completion. Hidden bool // Configures the current command. Configure Configure // The middleware for configures current command. Prepare Prepare // The function to call when this command is invoked. Execute Action // The middleware for executes current command. Handle Handle }
func Find ¶
Find command by name, tries to find the best match if you give it an abbreviation of a name.
type Handle ¶
func ChainHandle ¶
ChainHandle creates middleware for executes command.
type Option ¶
type Option func(*Command)
func WithHandle ¶
WithHandle append middleware for executed command.
func WithPrepare ¶
WithPrepare append middleware for configuration command.