-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.go
More file actions
93 lines (83 loc) · 2.6 KB
/
Copy pathcli.go
File metadata and controls
93 lines (83 loc) · 2.6 KB
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
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"strconv"
"strings"
)
type commandLineOptions struct {
initialPath string
verbosity int
disableCache bool
showHelp bool
showVersion bool
}
func parseCommandLine(args []string) (commandLineOptions, error) {
options := commandLineOptions{verbosity: defaultVerbosity}
positionalOnly := false
for i := 0; i < len(args); i++ {
argument := args[i]
if !positionalOnly {
switch {
case argument == "--":
positionalOnly = true
continue
case argument == "-h" || argument == "--help":
options.showHelp = true
continue
case argument == "--version":
options.showVersion = true
continue
case argument == "--no-cache":
options.disableCache = true
continue
case argument == "-v" || argument == "--verbosity":
if i+1 >= len(args) {
return options, fmt.Errorf("%s requires a value from 0 to %d", argument, maximumVerbosity)
}
i++
if err := setVerbosity(&options, args[i]); err != nil {
return options, err
}
continue
case strings.HasPrefix(argument, "-v="):
if err := setVerbosity(&options, strings.TrimPrefix(argument, "-v=")); err != nil {
return options, err
}
continue
case strings.HasPrefix(argument, "--verbosity="):
if err := setVerbosity(&options, strings.TrimPrefix(argument, "--verbosity=")); err != nil {
return options, err
}
continue
case strings.HasPrefix(argument, "-"):
return options, fmt.Errorf("unknown option %q", argument)
}
}
if options.initialPath != "" {
return options, fmt.Errorf("only one scan path may be specified")
}
options.initialPath = argument
}
if options.disableCache && options.initialPath == "" && !options.showHelp && !options.showVersion {
return options, fmt.Errorf("--no-cache requires a startup scan path")
}
return options, nil
}
func setVerbosity(options *commandLineOptions, value string) error {
verbosity, err := strconv.Atoi(value)
if err != nil || verbosity < verbosityCritical || verbosity > maximumVerbosity {
return fmt.Errorf("verbosity must be a number from 0 to %d", maximumVerbosity)
}
options.verbosity = verbosity
return nil
}
func commandLineUsage(executable string) string {
return fmt.Sprintf(`Usage: %s [path] [options]
Launch SpaceBrowser and optionally begin scanning path.
Options:
--no-cache Disable caching for the startup scan
-v, --verbosity level Logging verbosity: 0=critical, 1=error,
2=warning, 3=info, 4=debug, 5=trace (default 3)
-h, --help Show this help
--version Show the SpaceBrowser version`, executable)
}