CLI Tool Examples
This page contains comprehensive examples of using the CLI tool to manage CLI projects.
Example 1: Complete Project Lifecycle
Step 1: Initialize Project
bash
cli init --name todo --type multipleInteractive prompts:
- Usage:
A todo list manager - Version:
0.1.0 - Add commands:
list,add,complete,delete
Step 2: Add Commands
bash
# Add list command with flags
cli add --command list
# Follow prompts to add --all and --limit flags
# Add add command
cli add --command add
# Follow prompts to add --title and --priority flags
# Add complete command
cli add --command complete
# Follow prompts to add --id flagStep 3: Validate Project
bash
cli validateStep 4: Format Code
bash
cli formatStep 5: List Commands
bash
cli listOutput:
Commands:
list - List todos
add - Add a new todo
complete - Complete a todo
delete - Delete a todo
Flags:
Global:
(none)
Command 'list':
--all (bool) Show all todos including completed
--limit (int) Limit number of results
Command 'add':
--title (string) Todo title
--priority (string) Priority levelStep 6: Update Command
bash
cli update --command list --usage "List all todos with optional filters"Step 7: Build and Run
bash
# Build
cli build
# Run
cli run -- list --allExample 2: Using Templates
List Available Templates
bash
cli template listOutput:
Available templates:
basic - Basic CLI template (default) (builtin)
api-client - API client CLI template (builtin)
file-manager - File management CLI template (builtin)
server - Server management CLI template (builtin)
database - Database CLI template (builtin)
devops - DevOps CLI template (builtin)Use a Template
bash
cli template use --name api-client --output ./my-api-clientAdd Custom Template
bash
# Create template directory
mkdir -p ~/templates/my-template
# Add template files...
# Register template
cli template add --name my-template --path ~/templates/my-templateExample 3: Code Quality Workflow
Format Code
bash
# Format all code
cli format
# Check formatting without modifying
cli format --checkLint Code
bash
# Lint code
cli lint
# Lint and auto-fix
cli lint --fixGenerate Tests
bash
# Generate general test
cli test
# Generate test for specific command
cli test --command listValidate Project
bash
# Validate
cli validate
# Validate and fix
cli validate --fix
# Validate with JSON output
cli validate --format jsonExample 4: Project Management
Add Flag to Command
bash
cli add --flag verbose --to-command listInteractive prompts:
- Flag type:
bool - Usage:
Enable verbose output - Alias:
v
Update Flag
bash
cli update --flag verbose --from-command list --flag-default trueRemove Flag
bash
cli remove --flag verbose --from-command listRemove Command
bash
cli remove --command deleteExample 5: Cross-Platform Build
Build for Current Platform
bash
cli buildBuild for Linux
bash
cli build --platform linux/amd64Build for Multiple Platforms
bash
# Linux
cli build --platform linux/amd64 --output ./bin/todo-linux-amd64
# macOS
cli build --platform darwin/amd64 --output ./bin/todo-darwin-amd64
# Windows
cli build --platform windows/amd64 --output ./bin/todo-windows-amd64.exeExample 6: Documentation Generation
Generate Documentation
bash
cli docs --output ./docsGenerate Man Page
bash
cli manGenerate Shell Completions
bash
# Bash
cli completion --shell bash > /etc/bash_completion.d/todo
# Zsh
cli completion --shell zsh > ~/.zsh/completions/_todo
# Fish
cli completion --shell fish > ~/.config/fish/completions/todo.fishExample 7: Project Migration
Migrate Single to Multiple Commands
bash
# Current: Single command CLI
# Target: Multiple commands CLI
cli migrate --to multipleThis will:
- Convert
NewSingleProgramtoNewMultipleProgram - Create backup of original
main.go - Update project structure
Example 8: Framework Upgrade
Check for Upgrades
bash
cli upgrade --checkUpgrade to Latest
bash
cli upgradeUpgrade to Specific Version
bash
cli upgrade --to v1.2.0Example 9: Configuration Management
List Configuration
bash
cli configGet Configuration Value
bash
cli config --key output.formatSet Configuration Value
bash
cli config --key output.format --value json
cli config --key template.default --value api-clientExample 10: Development Workflow
Watch and Rebuild
bash
# Watch for changes and rebuild
cli watch --buildWatch and Test
bash
# Watch for changes and run tests
cli watch --testRun with Arguments
bash
# Run project
cli run
# Run with specific arguments
cli run -- list --all --limit 10Best Practices
Always validate before committing:
bashcli validate --fixFormat code regularly:
bashcli formatUse templates for common patterns:
bashcli template use --name api-clientGenerate tests early:
bashcli test --command listKeep documentation updated:
bashcli docsUse version control:
bashgit add . git commit -m "Add new command"