Subcommands

Subcommands can be defined for a more git-like command line app.

package main

import (
    "fmt"
    "log"
    "os"
    "context"

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

func main() {
    cmd := &cli.Command{
        Commands: []*cli.Command{
            {
                Name:    "add",
                Aliases: []string{"a"},
                Usage:   "add a task to the list",
                Action: func(ctx context.Context, cmd *cli.Command) error {
                    fmt.Println("added task: ", cmd.Args().First())
                    return nil
                },
            },
            {
                Name:    "complete",
                Aliases: []string{"c"},
                Usage:   "complete a task on the list",
                Action: func(ctx context.Context, cmd *cli.Command) error {
                    fmt.Println("completed task: ", cmd.Args().First())
                    return nil
                },
            },
            {
                Name:    "template",
                Aliases: []string{"t"},
                Usage:   "options for task templates",
                Commands: []*cli.Command{
                    {
                        Name:  "add",
                        Usage: "add a new template",
                        Action: func(ctx context.Context, cmd *cli.Command) error {
                            fmt.Println("new task template: ", cmd.Args().First())
                            return nil
                        },
                    },
                    {
                        Name:  "remove",
                        Usage: "remove an existing template",
                        Action: func(ctx context.Context, cmd *cli.Command) error {
                            fmt.Println("removed task template: ", cmd.Args().First())
                            return nil
                        },
                    },
                },
            },
        },
    }

    if err := cmd.Run(context.Background(), os.Args); err != nil {
        log.Fatal(err)
    }
}