▄████████ ▄█ ▄█
███ ███ ███ ███
███ █▀ ███ ███▌
███ ███ ███▌
███ ███ ███▌
███ █▄ ███ ███
███ ███ ███▌ ▄ ███
████████▀ █████▄▄██ █▀
▀
a lightweight and simple cli package
You can use this command to install the package
go get github.com/teamseodo/cliA example code for the basics of the cli
package main
import "github.com/teamseodo/cli"
func main() {
mainCommand := cli.NewCommand("app", "app [command] [flags]", "description about the app")
say := cli.NewCommand("say", "say [flags]", "prints the values")
mainCommand.AddCommand(say)
var messageParameter string
say.AddStringParameter(&cli.Parameter{
Use: "app print --message [value]",
Name: "message",
Shortname: "m",
}, &messageParameter, "hello world")
say.Do(func(cmd *cli.Command) {
fmt.Println(messageParameter)
})
help, err := mainCommand.FindHelp(os.Args[1:])
if err != nil {
log.Fatal(err)
}
help.ShowHelp()
cmd, err := mainCommand.Parse(os.Args[1:])
if err != nil {
log.Fatal(err)
}
err = cmd.Run()
if err != nil {
cmd.Help().ShowHelp()
}
}You need to create a configurator to use commands or anything in this package. Configurator takes a main command and you can add sub commands to the main command.
You can create a main command like that,
mainCommand := cli.NewCommand("app", "app [command] [flags]", "description about the app")Every command can have multiple sub commands, you can add a subcommand like that
hi := cli.NewCommand("hi", "hi", "prints a hello message back")
mainCommand.AddCommand(hi)Now you will get an structure in command line like that,
app hiIf you want to add a functionality to the command, you can use the command.Do method.
hi.Do(func (cmd *cli.Command) {
fmt.Println("hi")
})Parse the args after all things are done and run the returned command
cmd, err := mainCommand.Parse(os.Args[1:])
if err != nil {
log.Fatal(err)
}
err = cmd.Run()
if err != nil {
cmd.Help().ShowHelp()
}Now when you type app hi, application will print a "hi" message.
You can add a functionality to the main command either, so it will run when no command is received.
Every command can take multiple parameters (also known as: flags) You can add three types of parameters, string, int and bool.
AddBoolParameter(parameter *Parameter, value *bool, defaultValue bool) *Parameter
AddIntParameter(parameter *Parameter, value *int, defaultValue int) *Parameter
AddStringParameter(parameter *Parameter, value *string, defaultValue string) *ParameterIf you want to add a string parameter, you can add the parameter like this
var messageParameter string
printCommand.AddStringParameter(&cli.Parameter{
Use: "app print --message [value]",
Name: "message",
Shortname: "m",
}, &messageParameter, "hello world")If you want to print a help message for the command you need to run the FindHelp() method before configurator initialization.
help, err := mainCommand.FindHelp(os.Args[1:])
if err != nil {
log.Fatal(err)
}
help.ShowHelp()When running the wanted command parsing errors can be occur, so you can make an error check when you run the wanted command and print a help.
err = cmd.Run()
if err != nil {
cmd.Help().ShowHelp()
}Pull requests are welcome. please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.