You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.0 KiB
61 lines
1.0 KiB
One of the philosophies behind cli is that an API should be playful and full of
|
|
discovery. So a cli app can be as little as one line of code in `main()`.
|
|
|
|
<!-- {
|
|
"args": ["--help"],
|
|
"output": "A new cli application"
|
|
} -->
|
|
``` go
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
func main() {
|
|
err := cli.NewApp().Run(os.Args)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
```
|
|
|
|
This app will run and show help text, but is not very useful. Let's give an
|
|
action to execute and some help documentation:
|
|
|
|
<!-- {
|
|
"output": "boom! I say!"
|
|
} -->
|
|
``` go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
func main() {
|
|
app := cli.NewApp()
|
|
app.Name = "boom"
|
|
app.Usage = "make an explosive entrance"
|
|
app.Action = func(c *cli.Context) error {
|
|
fmt.Println("boom! I say!")
|
|
return nil
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
```
|
|
|
|
Running this already gives you a ton of functionality, plus support for things
|
|
like subcommands and flags, which are covered below.
|