CLI Tool - Init Command
The cli init command helps you quickly generate CLI application templates using the go-zoox/cli framework.
Note: For a complete guide to all CLI tool commands, see CLI Tool Complete Guide.
Installation
The CLI tool is included in the CLI framework. To use it, you need to build it:
go build -o cli ./cmd/cliOr install it globally:
go install github.com/go-zoox/cli/cmd/cli@latestQuick Start
Interactive Mode
The easiest way to use the CLI tool is in interactive mode:
cli initThis will guide you through:
- Project name and description
- CLI type (single or multiple commands)
The tool will automatically generate example flags and commands for you to get started quickly.
Mixed Mode (Recommended)
You can provide some parameters via command line, and the tool will only ask for the missing ones:
# Provide name, tool will ask for type
cli init --name myapp
# Provide name and type, tool will generate example code directly
cli init --name myapp --type multiple
# Provide all parameters, skip all prompts
cli init --name myapp --type single --output ./myapp --skip-interactiveNon-Interactive Mode
Use --skip-interactive to skip all prompts (requires all parameters):
cli init --name myapp --type single --output ./myapp --skip-interactiveUsage
Initialize a Single Command CLI
cli init --name myapp --type singleThis creates a new single command CLI application with:
main.go- Main application filego.mod- Go module fileREADME.md- Project documentation.gitignore- Git ignore file
Initialize a Multiple Commands CLI
cli init --name myapp --type multipleThis creates a new multiple commands CLI application where you can add multiple subcommands.
Flags
| Flag | Short | Description | Default |
|---|---|---|---|
--name | -n | Project name | Required |
--type | -t | CLI type (single|multiple) | single |
--output | -o | Output directory | . (current directory) |
--skip-interactive | -s | Skip interactive prompts | false |
Interactive Prompts
Project Information
- Project name: The name of your CLI application
- Usage/Description: A brief description of what your CLI does
- Version: Version number (default:
0.1.0) - CLI type: Choose between single command or multiple commands
Generated Examples
The tool automatically generates example code to help you get started:
Single Command Mode includes:
--nameflag (string, default: "World", alias:-n)--verboseflag (bool, alias:-v)
Multiple Commands Mode includes:
listcommand with--allflag (bool, alias:-a)createcommand with--nameflag (string, required, alias:-n)
You can modify these examples or use the cli add command to add more commands and flags later.
Generated Files
main.go
The generated main.go file includes:
- Complete CLI setup with your configuration
- Flag definitions
- Command handlers with TODO comments
- Example code showing how to access flag values
go.mod
The generated go.mod file includes:
- Module name
- Go version (1.18)
- Dependency on
github.com/go-zoox/cli
README.md
A basic README with:
- Project name and description
- Installation instructions
- Usage examples
.gitignore
A standard Go .gitignore file that excludes:
- Binaries
- Test files
- IDE files
- OS-specific files
Examples
Example 1: Simple Single Command CLI
cli init --name greet --type singleThe generated code will include example flags. Here's what it looks like:
app.Command(func(ctx *cli.Context) error {
name := ctx.String("name")
fmt.Printf("Hello, %s!\n", name)
return nil
})Example 2: Multiple Commands CLI
cli init --name todo --type multipleThe generated code will include example commands (list and create) with their flags. You can modify or add more commands using cli add.
Add Command
The cli add command allows you to add new commands or flags to an existing CLI project.
Add a Command
To add a new command to a multiple commands CLI:
# Fully interactive mode
cli add
# Provide command name, tool will ask for usage and flags
cli add --command mycommandAdd a Flag
To add a new flag to your CLI:
# Fully interactive mode
cli add
# Provide flag name, tool will ask for type and other details
cli add --flag port
# Add flag to a specific command
cli add --flag port --to-command listMixed Mode for Add Command
The add command supports mixed mode - you can provide partial information and the tool will ask for the rest:
# Provide command name, tool asks for usage and flags
cli add --command delete
# Provide flag name, tool asks for type, usage, etc.
cli add --flag verbose
# Provide flag name and target command, tool asks for flag details
cli add --flag limit --to-command listNext Steps
After generating your CLI project:
Navigate to the project directory:
bashcd myappInstall dependencies:
bashgo mod tidyRun your CLI:
bashgo run main.goBuild your CLI:
bashgo build -o myappTest with flags:
bash./myapp --help ./myapp --name "World"
Tips
- Use mixed mode for the best experience - provide what you know, let the tool ask for the rest
- The tool generates example code automatically - you can modify it or add more using
cli add - Use
--skip-interactiveto skip all prompts when you have all required parameters - You can always edit the generated code to customize it
- The generated code includes TODO comments to guide you
- Use
cli addto add more commands and flags after initialization - All flags support environment variables automatically
- Short aliases make your CLI more user-friendly
Troubleshooting
"command not found: cli"
Make sure you've built or installed the CLI tool:
go install github.com/go-zoox/cli/cmd/cli@latest"failed to create directory"
Make sure you have write permissions in the output directory.
Generated code doesn't compile
Run go mod tidy to ensure all dependencies are properly resolved.
Related Commands
After initializing your project, you can use other CLI tool commands:
cli add- Add commands or flagscli list- List commands and flagscli validate- Validate project structurecli format- Format codecli build- Build project
See CLI Tool Complete Guide for all available commands.