go-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo CLI Skill
Go CLI 技能
Supplementary CLI patterns and utilities.
补充性CLI模式与实用工具。
Overview
概述
Additional CLI patterns for argument parsing, progress indicators, and user interaction.
用于参数解析、进度指示器和用户交互的额外CLI模式。
Parameters
参数
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| feature | string | yes | - | Feature: "progress", "prompt", "color" |
| 参数 | 类型 | 是否必填 | 默认值 | 描述 |
|---|---|---|---|---|
| feature | string | 是 | - | 功能:"progress"、"prompt"、"color" |
Core Topics
核心主题
Argument Validation
参数验证
go
var cmd = &cobra.Command{
Use: "process [file]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
file := args[0]
if _, err := os.Stat(file); os.IsNotExist(err) {
return fmt.Errorf("file not found: %s", file)
}
return process(file)
},
}go
var cmd = &cobra.Command{
Use: "process [file]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
file := args[0]
if _, err := os.Stat(file); os.IsNotExist(err) {
return fmt.Errorf("file not found: %s", file)
}
return process(file)
},
}Progress Bar
进度条
go
func DownloadWithProgress(url, dest string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
file, err := os.Create(dest)
if err != nil {
return err
}
defer file.Close()
bar := progressbar.DefaultBytes(resp.ContentLength, "downloading")
_, err = io.Copy(io.MultiWriter(file, bar), resp.Body)
return err
}go
func DownloadWithProgress(url, dest string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
file, err := os.Create(dest)
if err != nil {
return err
}
defer file.Close()
bar := progressbar.DefaultBytes(resp.ContentLength, "downloading")
_, err = io.Copy(io.MultiWriter(file, bar), resp.Body)
return err
}Interactive Prompt
交互式提示
go
func ConfirmAction(message string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/N]: ", message)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(strings.ToLower(input))
return input == "y" || input == "yes"
}go
func ConfirmAction(message string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/N]: ", message)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(strings.ToLower(input))
return input == "y" || input == "yes"
}Colored Output
彩色输出
go
import "github.com/fatih/color"
var (
successColor = color.New(color.FgGreen).SprintFunc()
errorColor = color.New(color.FgRed).SprintFunc()
warnColor = color.New(color.FgYellow).SprintFunc()
)
func PrintSuccess(msg string) {
fmt.Println(successColor("✓"), msg)
}
func PrintError(msg string) {
fmt.Fprintln(os.Stderr, errorColor("✗"), msg)
}go
import "github.com/fatih/color"
var (
successColor = color.New(color.FgGreen).SprintFunc()
errorColor = color.New(color.FgRed).SprintFunc()
warnColor = color.New(color.FgYellow).SprintFunc()
)
func PrintSuccess(msg string) {
fmt.Println(successColor("✓"), msg)
}
func PrintError(msg string) {
fmt.Fprintln(os.Stderr, errorColor("✗"), msg)
}Troubleshooting
故障排除
Failure Modes
故障模式
| Symptom | Cause | Fix |
|---|---|---|
| Colors not showing | No TTY | Check isatty |
| Input hangs | Stdin closed | Handle EOF |
| 症状 | 原因 | 解决方法 |
|---|---|---|
| 颜色不显示 | 无TTY | 检查isatty |
| 输入挂起 | 标准输入关闭 | 处理EOF |
Usage
使用方法
Skill("go-cli")Skill("go-cli")