package cli import ( "fmt" "strconv" "strings" ) // ProgressMode is the resolved meaning of --progress. type ProgressMode int const ( ProgressAuto ProgressMode = iota // live on a terminal, silent otherwise ProgressOff ProgressOn ProgressRows // enabled, plus a row-count threshold ) // ProgressValue is the flag.Value behind --progress. It keeps the raw // argument so invalid values are reported as configuration errors by Validate // rather than as bare flag parse errors. type ProgressValue struct { Mode ProgressMode Rows int64 raw string } // Set records the raw argument; resolve happens in Validate. func (p *ProgressValue) Set(s string) error { p.raw = s return nil } func (p *ProgressValue) String() string { if p.raw != "" { return p.raw } switch p.Mode { case ProgressOn: return "on" case ProgressOff: return "off" case ProgressRows: return strconv.FormatInt(p.Rows, 10) default: return "auto" } } // resolve parses the recorded argument into Mode and Rows. func (p *ProgressValue) resolve() error { switch strings.ToLower(strings.TrimSpace(p.raw)) { case "", "auto": p.Mode, p.Rows = ProgressAuto, 0 case "on": p.Mode, p.Rows = ProgressOn, 0 case "off": p.Mode, p.Rows = ProgressOff, 0 default: n, err := strconv.ParseInt(strings.TrimSpace(p.raw), 10, 64) if err != nil || n < 0 { return fmt.Errorf("--progress must be auto, on, off or a non-negative integer, got %q", p.raw) } if n == 0 { p.Mode, p.Rows = ProgressOff, 0 } else { p.Mode, p.Rows = ProgressRows, n } } return nil } // Enabled reports whether progress output should be produced. tty describes // whether stderr is a terminal. func (p ProgressValue) Enabled(tty bool) bool { switch p.Mode { case ProgressOn, ProgressRows: return true case ProgressAuto: return tty default: return false } }