A wrapper for the flag package to define commands which lazy-load subcommands.
package main
import (
"fmt"
"os"
"github.com/ferroxyl/cmd"
)
// Define a new cli
var app = cmd.New("example", "0.0.0")
// Define a subcommand 'version' by satisfying the Cmd interface
type Version struct{}
func (s *Version) Description() string {
return "Print the version"
}
func (s *Version) Init() *flag.FlagSet {
return nil
}
func (s *Version) Run(args []string) error {
if _, err := fmt.Printf("%s\n", "example v1.0.0"); err != nil {
return err
}
return nil
}
func init() {
// Register a subcommand
app.Register("version", &cli.Version{})
}
func main() {
// Custom message to show if no subcommands are provided
if len(os.Args) < 2 {
fmt.Println("No command provided. Use 'example -h' for more info.")
os.Exit(1)
}
// Run the provided subcommand or return the error and usage
if err := app.Run(os.Args[1]); err != nil {
fmt.Printf("example: %s\n", err)
app.Usage()
os.Exit(1)
}
os.Exit(0)
}