How to Use urfave/cli for Building CLIs in Go

Cli
Build Go CLIs quickly by defining commands and flags in a cli.App struct and running it with app.Run(os.Args).

Use urfave/cli by defining a cli.App struct with commands and actions, then calling app.Run(os.Args). This library handles flag parsing, help generation, and command execution automatically.

package main

import (
	"fmt"
	"os"

	"github.com/urfave/cli/v2"
)

func main() {
	app := &cli.App{
		Name:  "myapp",
		Usage: "A simple CLI tool",
		Commands: []*cli.Command{
			{
				Name:  "greet",
				Usage: "Greet someone",
				Flags: []cli.Flag{
					&cli.StringFlag{
						Name:  "name",
						Usage: "The name to greet",
						Value: "World",
					},
				},
				Action: func(c *cli.Context) error {
					name := c.String("name")
					fmt.Printf("Hello, %s!\n", name)
					return nil
				},
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}