-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy patharguments.go
83 lines (64 loc) · 1.79 KB
/
arguments.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"regexp"
"strconv"
"time"
"github.com/docopt/docopt-go"
)
const defaultConcurrency = maxOpenFiles / 2
const usage = `Link checker for Markdown and HTML
Usage:
liche [-c <num-requests>] [-d <directory>] [-r] [-t <timeout>] [-x <regex>] [-v] <filenames>...
Options:
-c, --concurrency <num-requests> Set max number of concurrent HTTP requests. [default: %v]
-d, --document-root <directory> Set document root directory for absolute paths.
-r, --recursive Search Markdown and HTML files recursively
-t, --timeout <timeout> Set timeout for HTTP requests in seconds. Disabled by default.
-x, --exclude <regex> Regex of links to exclude from checking.
-v, --verbose Be verbose.`
type arguments struct {
filenames []string
documentRoot string
concurrency int
timeout time.Duration
excludedPattern *regexp.Regexp
recursive bool
verbose bool
}
func getArguments(argv []string) (arguments, error) {
args, err := docopt.ParseArgs(fmt.Sprintf(usage, defaultConcurrency), argv, "0.2.0")
if err != nil {
return arguments{}, err
}
c, err := strconv.ParseInt(args["--concurrency"].(string), 10, 32)
if err != nil {
return arguments{}, err
}
if args["--document-root"] == nil {
args["--document-root"] = ""
}
t := 0.0
if args["--timeout"] != nil {
t, err = strconv.ParseFloat(args["--timeout"].(string), 64)
if err != nil {
return arguments{}, err
}
}
r := (*regexp.Regexp)(nil)
if args["--exclude"] != nil {
r, err = regexp.Compile(args["--exclude"].(string))
if err != nil {
return arguments{}, err
}
}
return arguments{
args["<filenames>"].([]string),
args["--document-root"].(string),
int(c),
time.Duration(t) * time.Second,
r,
args["--recursive"].(bool),
args["--verbose"].(bool),
}, nil
}