package main import ( "fmt" "github.com/gonuts/commander" ) func main() { cmd := &commander.Command{ UsageLine: "myapp [OPTIONS] [ARGS...]", Short: "A brief description of my app", Long: ` A longer description of my app. This text can span multiple lines and may include supporting documentation, examples, or usage notes. `, Run: func(cmd *commander.Command, args []string) error { fmt.Println("Hello, world!") return nil }, } if err := cmd.Dispatch(nil); err != nil { fmt.Println("Error:", err) // handle error } }
package main import ( "fmt" "github.com/gonuts/commander" ) func main() { cmd := &commander.Command{ UsageLine: "myapp [OPTIONS] [ARGS...]", Short: "A brief description of my app", Long: ` A longer description of my app. This text can span multiple lines and may include supporting documentation, examples, or usage notes. `, Flag: *flag.Bool("q", false, "Run quietly"), Run: func(cmd *commander.Command, args []string) error { if cmd.Flag.Lookup("q").Value.Get().(bool) { fmt.Println("Running quietly...") } fmt.Println("Hello, world!") return nil }, } if err := cmd.Dispatch(nil); err != nil { fmt.Println("Error:", err) // handle error } }Here, we've added a flag to our command-line interface (`-q`), which can be used to run the command quietly (i.e., without printing any output). We define the flag using the `flag` package from the Go standard library, and add it to our `Command` object using the `Flag` property. In the `Run` method, we check the value of the flag using the `Lookup` method, and print a message if it's set to true. We then print our "Hello, world!" message as before. Overall, the `Command` package from `github.com/gonuts/commander` is a useful library for creating command-line interfaces in Go. It provides a simple and flexible framework for defining commands and flags, parsing arguments, and executing code in response to user input.