ππ¨βπ»Scotty
- Zero dependencies library to build simple commandline apps.
Basically it is a thin wrapper around standard flag.FlagSet
type.
- π€ Simple API.
- π Zero dependencies.
- π Plays nice with standard
flag
package. - π Nice default
-help
information.
go get github.com/heartwilltell/scotty
The usage is pretty simple:
- π Declare the root command.
- π Attach subcommands and flags to it.
- π Write your stuff inside the
Run
function. - π Call
Exec
function of the root command somewhere inmain
function.
package main
import (
"fmt"
"log"
"os"
"github.com/heartwilltell/scotty"
)
func main() {
// Declare the root command.
rootCmd := scotty.Command{
Name: "app",
Short: "Main command which holds all subcommands",
}
// Declare the subcommand.
subCmd := scotty.Command{
Name: "subcommand",
Short: "Subcommands that does something",
Run: func(cmd *scotty.Command, args []string) error {
// Do some your stuff here.
return nil
},
}
// And here how you bind some flags to your command.
var logLVL string
subCmd.Flags().StringVar(&logLVL, "loglevel", "info",
"set logging level: 'debug', 'info', 'warning', 'error'",
)
// Or use the SetFlags function.
subCmd2 := scotty.Command{
Name: "subcommand2",
Short: "Subcommands that does something",
SetFlags: func(flags *FlagSet) {
flags.StringVar(&logLVL, "loglevel", "info",
"set logging level: 'debug', 'info', 'warning', 'error'",
)
},
Run: func(cmd *scotty.Command, args []string) error {
// Do some your stuff here.
return nil
},
}
// Attach subcommand to the root command.
rootCmd.AddSubcommands(&subCmd)
// Execute the root command.
if err := rootCmd.Exec(); err != nil {
fmt.Println(err)
os.Exit(2)
}
}