-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
55 lines (47 loc) · 1.2 KB
/
cli.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
package main
import (
"os"
"fmt"
"flag"
)
type Arguments struct {
quiet bool
port int
host string
log string
directory string
cors bool
noCache bool
}
func exitOnError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func parseArguments(args *Arguments) {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: [-host HOST] [-port PORT] [DIRECTORY]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Serves the directory specified by the first argument " +
"which defaults to the current working directory if " +
"not specified.\n\n")
flag.PrintDefaults()
}
flag.BoolVar(&args.quiet, "quiet", false, "Quiet mode")
flag.IntVar(&args.port, "port", 8080, "Port to listen")
flag.StringVar(&args.host, "host", "localhost", "Host to listen")
flag.StringVar(&args.log, "log", "", "Optional log file")
flag.BoolVar(&args.cors, "cors", false, "Elable cross-origin resource sharing")
flag.BoolVar(&args.noCache, "no-cache", false, "Disable caching")
flag.Parse()
if flag.NArg() > 1 {
flag.Usage()
os.Exit(1)
}
args.directory = flag.Arg(0)
if len(args.directory) == 0 {
args.directory = "."
}
_, err := os.Stat(args.directory)
exitOnError(err)
}