CLI Tool Examples
This page contains examples of using the CLI tool to generate CLI applications.
Example 1: Simple Greeting CLI
Generate a simple CLI that greets users:
bash
cli init --name greet --type singleInteractive prompts:
- Project name:
greet - Usage:
A simple greeting CLI - Version:
0.1.0 - CLI type:
single - Add flag:
name(String, required) - Add flag:
formal(Bool, default: false)
Generated code will include:
go
app.Command(func(ctx *cli.Context) error {
name := ctx.String("name")
formal := ctx.Bool("formal")
if formal {
fmt.Printf("Good day, %s!\n", name)
} else {
fmt.Printf("Hello, %s!\n", name)
}
return nil
})Usage:
bash
go run main.go --name "Alice"
go run main.go --name "Bob" --formalExample 2: Todo List CLI
Generate a multiple commands CLI for managing todos:
bash
cli init --name todo --type multipleAdd commands:
list command
- Flag:
--all(Bool) - Show all todos including completed - Flag:
--limit(Int) - Limit number of results
- Flag:
add command
- Flag:
--title(String, required) - Todo title - Flag:
--priority(String) - Priority level
- Flag:
complete command
- Flag:
--id(Int, required) - Todo ID to complete
- Flag:
Generated structure:
go
app.Register("list", &cli.Command{
// ...
})
app.Register("add", &cli.Command{
// ...
})
app.Register("complete", &cli.Command{
// ...
})Usage:
bash
todo list
todo add --title "Buy groceries" --priority "high"
todo complete --id 1Example 3: Server CLI with Configuration
Generate a CLI for managing a server:
bash
cli init --name server --type singleAdd flags:
--port(Int, default: 8080, alias:-p)--host(String, default: "localhost")--config(String, env:SERVER_CONFIG)--verbose(Bool, alias:-v)
Generated code:
go
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Usage: "Server port",
Value: 8080,
Aliases: []string{"p"},
},
&cli.StringFlag{
Name: "host",
Usage: "Server host",
Value: "localhost",
},
&cli.StringFlag{
Name: "config",
Usage: "Config file path",
EnvVars: []string{"SERVER_CONFIG"},
},
&cli.BoolFlag{
Name: "verbose",
Usage: "Enable verbose logging",
Aliases: []string{"v"},
},
}Usage:
bash
server --port 3000 --host 0.0.0.0
server -p 3000 -v
SERVER_CONFIG=/path/to/config serverExample 4: File Operations CLI
Generate a CLI for file operations:
bash
cli init --name fileops --type multipleAdd commands:
copy command
--source(String, required)--destination(String, required)--recursive(Bool)
move command
--source(String, required)--destination(String, required)
delete command
--path(String, required)--force(Bool)
Example 5: API Client CLI
Generate a CLI for interacting with an API:
bash
cli init --name apiclient --type singleAdd flags:
--endpoint(String, required, env:API_ENDPOINT)--token(String, required, env:API_TOKEN)--method(String, default: "GET")--data(String) - JSON data--headers(StringSlice) - Additional headers
Usage:
bash
apiclient --endpoint https://api.example.com/users --method GET
apiclient --endpoint https://api.example.com/users --method POST --data '{"name":"John"}'
API_ENDPOINT=https://api.example.com API_TOKEN=xxx apiclient --method GETBest Practices
- Use meaningful names: Choose descriptive project and command names
- Add helpful descriptions: Write clear usage descriptions for flags and commands
- Set defaults: Provide sensible default values for optional flags
- Use aliases: Add short aliases for commonly used flags
- Environment variables: Enable environment variable support for sensitive data
- Organize commands: Group related commands logically in multiple commands mode