-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathoption.go
64 lines (54 loc) · 1.85 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package pget
import (
"bytes"
"fmt"
"github.com/Code-Hex/updater"
"github.com/jessevdk/go-flags"
"github.com/pkg/errors"
)
// Options struct for parse command line arguments
type Options struct {
Help bool `short:"h" long:"help"`
NumConnection int `short:"p" long:"procs" default:"1"`
Output string `short:"o" long:"output"`
Timeout int `short:"t" long:"timeout" default:"10"`
UserAgent string `short:"u" long:"user-agent"`
Referer string `short:"r" long:"referer"`
Update bool `long:"check-update"`
Trace bool `long:"trace"`
}
func (opts *Options) parse(argv []string, version string) ([]string, error) {
p := flags.NewParser(opts, flags.PrintErrors)
args, err := p.ParseArgs(argv)
if err != nil {
stdout.Write(opts.usage(version))
return nil, errors.Wrap(err, "invalid command line options")
}
return args, nil
}
func (opts Options) usage(version string) []byte {
buf := bytes.Buffer{}
msg := "Pget %s, The fastest file download client\n"
fmt.Fprintf(&buf, msg+
`Usage: pget [options] URL
Options:
-h, --help print usage and exit
-p, --procs <num> the number of connections for a single URL (default 1)
-o, --output <filename> output file to <filename>
-t, --timeout <seconds> timeout of checking request in seconds (default 10s)
-u, --user-agent <agent> identify as <agent>
-r, --referer <referer> identify as <referer>
--check-update check if there is update available
--trace display detail error messages
`, version)
return buf.Bytes()
}
func (opts Options) isupdate(version string) ([]byte, error) {
buf := bytes.Buffer{}
result, err := updater.CheckWithTag("Code-Hex", "pget", version)
if err != nil {
return nil, err
}
fmt.Fprintf(&buf, result+"\n")
return buf.Bytes(), nil
}