// Package cli turns command-line arguments and config files into a resolved set // of options. The whole design is one flat table of option descriptors; help // text, defaults, parsing and validation all derive from it. The whole option // model is plain data rather than a class hierarchy. package cli // Kind is the value type of an option. It drives parsing and how the value is // later read back. type Kind int const ( Bool Kind = iota // bare flag means true; --flag=false also accepted Int // integer, optionally bounded by Min/Max Size // byte size with optional K/M/G suffix Float // floating point Str // free-form string Enum // one of Opt.Enum List // repeatable; values accumulate, newline-joined ) // Tag groups options for --help. type Tag int const ( Basic Tag = iota HTTP BitTorrent Advanced ) func (t Tag) String() string { switch t { case Basic: return "basic" case HTTP: return "http" case BitTorrent: return "bittorrent" case Advanced: return "advanced" default: return "?" } } // Opt describes one option: its names, value kind, default and help text. type Opt struct { Long string // long name without dashes, e.g. "max-connection-per-server" Short byte // short flag byte, e.g. 'x'; 0 if none Kind Kind Default string Help string Tag Tag Enum []string // valid values when Kind == Enum // Min is the inclusive lower Int/Size bound. The zero value enforces a // floor of 0 (negative inputs are rejected). Min int64 // Max is the inclusive upper Int/Size bound; the zero value means no upper // bound. For Size options Min/Max are byte counts. Max int64 } // options is the single source of truth: a focused, practical subset. var options = []Opt{ // --- basic --- {Long: "dir", Short: 'd', Kind: Str, Help: "directory to store downloaded files", Tag: Basic}, {Long: "out", Short: 'o', Kind: Str, Help: "output filename for the download", Tag: Basic}, {Long: "input-file", Short: 'i', Kind: Str, Help: "read URIs line by line from FILE (- for stdin)", Tag: Basic}, {Long: "continue", Short: 'c', Kind: Bool, Default: "false", Help: "resume a partially downloaded file", Tag: Basic}, {Long: "max-concurrent-downloads", Short: 'j', Kind: Int, Default: "5", Min: 1, Help: "max number of parallel downloads", Tag: Basic}, {Long: "check-integrity", Short: 'V', Kind: Bool, Default: "false", Help: "re-verify torrent data against piece hashes (BitTorrent)", Tag: Basic}, {Long: "show-files", Short: 'S', Kind: Bool, Default: "false", Help: "list files in a torrent and exit", Tag: Basic}, {Long: "allow-overwrite", Kind: Bool, Default: "false", Help: "overwrite an existing file", Tag: Basic}, {Long: "auto-file-renaming", Kind: Bool, Default: "true", Help: "rename file (.1, .2, ...) if it already exists", Tag: Basic}, {Long: "max-overall-download-limit", Kind: Size, Default: "0", Help: "global download speed limit (0 = unlimited)", Tag: Basic}, {Long: "max-download-limit", Kind: Size, Default: "0", Help: "per-download speed limit (0 = unlimited)", Tag: Basic}, {Long: "quiet", Short: 'q', Kind: Bool, Default: "false", Help: "suppress the progress readout", Tag: Basic}, {Long: "human-readable", Kind: Bool, Default: "true", Help: "show sizes as Ki/Mi/Gi", Tag: Basic}, // --- http --- {Long: "max-connection-per-server", Short: 'x', Kind: Int, Default: "1", Min: 1, Max: 16, Help: "max connections to one server (1-16)", Tag: HTTP}, {Long: "split", Short: 's', Kind: Int, Default: "5", Min: 1, Help: "split a download into N connections; actual connections are min(max-connection-per-server, split), and -x defaults to 1", Tag: HTTP}, {Long: "min-split-size", Short: 'k', Kind: Size, Default: "20M", Min: 1 << 20, Max: 1 << 30, Help: "do not split a piece smaller than SIZE (1M-1024M)", Tag: HTTP}, {Long: "auto-split", Kind: Bool, Default: "false", Help: "got-only: pick the connection count from file size (one per min-split-size, up to 16); overrides -x/-s", Tag: HTTP}, {Long: "force-sequential", Short: 'Z', Kind: Bool, Default: "false", Help: "download each command-line URI as its own file instead of mirroring them", Tag: HTTP}, {Long: "max-tries", Short: 'm', Kind: Int, Default: "5", Min: 0, Help: "max retries per segment (0 = unlimited)", Tag: HTTP}, {Long: "timeout", Short: 't', Kind: Int, Default: "60", Min: 1, Help: "connection timeout in seconds", Tag: HTTP}, {Long: "header", Kind: List, Help: "append an extra HTTP header (repeatable)", Tag: HTTP}, {Long: "user-agent", Short: 'U', Kind: Str, Default: "got/1.0", Help: "HTTP User-Agent", Tag: HTTP}, {Long: "referer", Kind: Str, Help: "HTTP Referer header", Tag: HTTP}, {Long: "all-proxy", Kind: Str, Help: "proxy for all protocols (host:port)", Tag: HTTP}, {Long: "check-certificate", Kind: Bool, Default: "true", Help: "verify the server's TLS certificate", Tag: HTTP}, {Long: "ca-certificate", Kind: Str, Help: "verify HTTPS servers against the CA certificates in FILE (PEM)", Tag: HTTP}, {Long: "retry-wait", Kind: Int, Default: "0", Min: 0, Max: 600, Help: "seconds to wait between retries (0 = no wait)", Tag: HTTP}, {Long: "connect-timeout", Kind: Int, Default: "60", Min: 1, Help: "connection establishment timeout in seconds", Tag: HTTP}, {Long: "load-cookies", Kind: Str, Help: "load Cookies from FILE (Netscape/Mozilla format)", Tag: HTTP}, {Long: "save-cookies", Kind: Str, Help: "save Cookies to FILE on exit", Tag: HTTP}, {Long: "conditional-get", Kind: Bool, Default: "false", Help: "download only if the remote file is newer than the local one", Tag: HTTP}, {Long: "remote-time", Kind: Bool, Default: "false", Help: "set the local file's mtime from the server", Tag: HTTP}, {Long: "http-user", Kind: Str, Help: "HTTP basic-auth user", Tag: HTTP}, {Long: "http-passwd", Kind: Str, Help: "HTTP basic-auth password", Tag: HTTP}, {Long: "lowest-speed-limit", Kind: Size, Default: "0", Help: "abort if speed stays below SIZE bytes/sec (0 = off)", Tag: HTTP}, {Long: "checksum", Kind: Str, Help: "verify the finished file: TYPE=DIGEST, e.g. sha-256= (md5, sha-1, sha-224, sha-256, sha-384, sha-512)", Tag: HTTP}, // --- bittorrent --- {Long: "torrent-file", Short: 'T', Kind: Str, Help: "path to a .torrent file", Tag: BitTorrent}, {Long: "seed-time", Kind: Float, Help: "minutes to seed after completion (0 = no seeding)", Tag: BitTorrent}, {Long: "seed-ratio", Kind: Float, Default: "1.0", Help: "stop seeding at this share ratio (0 = unlimited)", Tag: BitTorrent}, {Long: "bt-stop-timeout", Kind: Int, Default: "0", Min: 0, Help: "stop a torrent with no download progress for N seconds (0 = off)", Tag: BitTorrent}, {Long: "bt-metadata-timeout", Kind: Int, Default: "60", Min: 0, Help: "stop a magnet that can't fetch metadata in N seconds (0 = off)", Tag: BitTorrent}, {Long: "listen-port", Kind: Str, Default: "6881-6999", Help: "TCP port (range) for incoming peers", Tag: BitTorrent}, {Long: "enable-dht", Kind: Bool, Default: "true", Help: "enable the BitTorrent DHT", Tag: BitTorrent}, {Long: "bt-max-peers", Kind: Int, Default: "55", Min: 0, Help: "max peers per torrent (0 = unlimited)", Tag: BitTorrent}, {Long: "max-overall-upload-limit", Kind: Size, Default: "0", Help: "global upload speed limit (0 = unlimited)", Tag: BitTorrent}, {Long: "max-upload-limit", Short: 'u', Kind: Size, Default: "0", Help: "per-torrent upload speed limit", Tag: BitTorrent}, {Long: "select-file", Kind: Str, Help: "download only these file indexes, e.g. 1,3-5", Tag: BitTorrent}, {Long: "follow-torrent", Kind: Bool, Default: "true", Help: "after fetching a .torrent over HTTP, download its content", Tag: BitTorrent}, // --- advanced --- {Long: "file-allocation", Short: 'a', Kind: Enum, Default: "prealloc", Enum: []string{"none", "prealloc", "trunc", "falloc"}, Help: "how to allocate disk space", Tag: Advanced}, {Long: "dry-run", Kind: Bool, Default: "false", Help: "check that the file is available but do not download it", Tag: Advanced}, {Long: "stop", Kind: Int, Default: "0", Min: 0, Help: "stop the program after N seconds (0 = off)", Tag: Advanced}, {Long: "disable-ipv6", Kind: Bool, Default: "false", Help: "disable IPv6 (force IPv4-only connections)", Tag: Advanced}, {Long: "save-session", Kind: Str, Help: "on exit, write unfinished downloads to FILE (reload with -i)", Tag: Advanced}, {Long: "auto-save-interval", Kind: Int, Default: "60", Min: 0, Max: 600, Help: "save the session file every N seconds (0 = only on exit)", Tag: Advanced}, {Long: "conf-path", Kind: Str, Help: "path to the config file", Tag: Advanced}, {Long: "no-conf", Kind: Bool, Default: "false", Help: "do not read a config file", Tag: Advanced}, } // byLong and byShort index the table. Built once at init. var ( byLong = map[string]*Opt{} byShort = map[byte]*Opt{} ) func init() { for i := range options { o := &options[i] byLong[o.Long] = o if o.Short != 0 { byShort[o.Short] = o } } }