go-commander alternatives and similar packages
Based on the "Standard CLI" category.
Alternatively, view go-commander alternatives based on common mentions on social networks and blogs.
-
The Platinum Searcher
A code search tool similar to ack and the_silver_searcher(ag). It supports multi platforms and multi encodings. -
readline
A pure golang implementation that provide most of features in GNU-Readline under MIT license. -
pflag
Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags. -
mitchellh/cli
A Go library for implementing command-line interfaces. -
docopt.go
A command-line arguments parser that will make you smile. -
cli-init
The easy way to start building Golang command line application. -
complete
Write bash completions in Go + Go command bash completion. -
mow.cli
A Go library for building CLI applications with sophisticated flag and argument parsing and validation. -
flaggy
A robust and idiomatic flags package with excellent subcommand support. -
cli
A feature-rich and easy to use command-line package based on golang tag -
argparse
Command line argument parser inspired by Python's argparse module. -
commandeer
Dev-friendly CLI apps: sets up flags, defaults, and usage based on struct fields and tags. -
sflags
Struct based flags generator for flag, urfave/cli, pflag, cobra, kingpin and other libraries. -
wmenu
An easy to use menu structure for cli applications that prompts users to make choices. -
1build
Command line tool to frictionlessly manage project-specific commands. -
flag
A simple but powerful command line option parsing library for Go support subcommand -
wlog
A simple logging interface that supports cross-platform color and concurrency. -
go-getoptions
Go option parser inspired on the flexibility of Perl’s GetOpt::Long. -
argv
A Go library to split command line string as arguments array using the bash syntax. -
flagvar
A collection of flag argument types for Go's standard flag package. -
Go-Console
GoConsole allows you to create butifull command-line commands with arguments and options. (follow the docopt standard) -
hiboot cli
cli application framework with auto configuration and dependency injection.
Scout APM - Leading-edge performance monitoring starting at $39/month
Do you think we are missing an alternative of go-commander or a related project?
Popular Comparisons
README
This is a simple Go library to manage commands for your CLI tool. Easy to use and now you can focus on Business Logic instead of building the command routing.
What this library does for you?
Manage your separated commands. How? Generates a general help and command
specific helps for your commands. If your command fails somewhere
(panic
for example), commander will display the error message and
the command specific help to guide your user.
Install
$ go get github.com/yitsushi/go-commander
Sample output (from totp-cli)
$ totp-cli help
change-password Change password
update Check and update totp-cli itself
version Print current version of this application
generate <namespace>.<account> Generate a specific OTP
add-token [namespace] [account] Add new token
list [namespace] List all available namespaces or accounts under a namespace
delete <namespace>[.account] Delete an account or a whole namespace
help [command] Display this help or a command specific help
Usage
Every single command has to implement CommandHandler
.
Check this project for examples.
package main
// Import the package
import "github.com/yitsushi/go-commander"
// Your Command
type YourCommand struct {
}
// Executed only on command call
func (c *YourCommand) Execute(opts *commander.CommandHelper) {
// Command Action
}
func NewYourCommand(appName string) *commander.CommandWrapper {
return &commander.CommandWrapper{
Handler: &YourCommand{},
Help: &commander.CommandDescriptor{
Name: "your-command",
ShortDescription: "This is my own command",
LongDescription: `This is a very long
description about this command.`,
Arguments: "<filename> [optional-argument]",
Examples: []string {
"test.txt",
"test.txt copy",
"test.txt move",
},
},
}
}
// Main Section
func main() {
registry := commander.NewCommandRegistry()
registry.Register(NewYourCommand)
registry.Execute()
}
Now you have a CLI tool with two commands: help
and your-command
.
❯ go build mytool.go
❯ ./mytool
your-command <filename> [optional-argument] This is my own command
help [command] Display this help or a command specific help
❯ ./mytool help your-command
Usage: mytool your-command <filename> [optional-argument]
This is a very long
description about this command.
Examples:
mytool your-command test.txt
mytool your-command test.txt copy
mytool your-command test.txt move
How to use subcommand pattern?
When you create your main command, just create a new CommandRegistry
inside
the Execute
function like you did in your main()
and change Depth
.
import subcommand "github.com/yitsushi/mypackage/command/something"
func (c *Something) Execute(opts *commander.CommandHelper) {
registry := commander.NewCommandRegistry()
registry.Depth = 1
registry.Register(subcommand.NewSomethingMySubCommand)
registry.Execute()
}
PreValidation
If you want to write a general pre-validation for your command or just simply keep your validation logic separated:
// Or you can define inline if you want
func MyValidator(c *commander.CommandHelper) {
if c.Arg(0) == "" {
panic("File?")
}
info, err := os.Stat(c.Arg(0))
if err != nil {
panic("File not found")
}
if !info.Mode().IsRegular() {
panic("It's not a regular file")
}
if c.Arg(1) != "" {
if c.Arg(1) != "copy" && c.Arg(1) != "move" {
panic("Invalid operation")
}
}
}
func NewYourCommand(appName string) *commander.CommandWrapper {
return &commander.CommandWrapper{
Handler: &YourCommand{},
Validator: MyValidator
Help: &commander.CommandDescriptor{
Name: "your-command",
ShortDescription: "This is my own command",
LongDescription: `This is a very long
description about this command.`,
Arguments: "<filename> [optional-argument]",
Examples: []string {
"test.txt",
"test.txt copy",
"test.txt move",
},
},
}
}
Define arguments with type
&commander.CommandWrapper{
Handler: &MyCommand{},
Arguments: []*commander.Argument{
&commander.Argument{
Name: "list",
Type: "StringArray[]",
},
},
Help: &commander.CommandDescriptor{
Name: "my-command",
},
}
In your command:
if opts.HasValidTypedOpt("list") == nil {
myList := opts.TypedOpt("list").([]string)
if len(myList) > 0 {
mockPrintf("My list: %v\n", myList)
}
}
Define own type
Yes you can ;)
// Define your struct (optional)
type MyCustomType struct {
ID uint64
Name string
}
// register your own type with parsing/validation
commander.RegisterArgumentType("MyType", func(value string) (interface{}, error) {
values := strings.Split(value, ":")
if len(values) < 2 {
return &MyCustomType{}, errors.New("Invalid format! MyType => 'ID:Name'")
}
id, err := strconv.ParseUint(values[0], 10, 64)
if err != nil {
return &MyCustomType{}, errors.New("Invalid format! MyType => 'ID:Name'")
}
return &MyCustomType{
ID: id,
Name: values[1],
},
nil
})
// Define your command
&commander.CommandWrapper{
Handler: &MyCommand{},
Arguments: []*commander.Argument{
&commander.Argument{
Name: "owner",
Type: "MyType",
FailOnError: true, // Optional boolean
},
},
Help: &commander.CommandDescriptor{
Name: "my-command",
},
}
In your command:
if opts.HasValidTypedOpt("owner") == nil {
owner := opts.TypedOpt("owner").(*MyCustomType)
mockPrintf("OwnerID: %d, Name: %s\n", owner.ID, owner.Name)
}