-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
86 lines (78 loc) · 1.7 KB
/
util.go
File metadata and controls
86 lines (78 loc) · 1.7 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
package main
import (
"os"
"strings"
)
func printHelp() {
println("Syntax: golf source_file target_file [-v] [-- 'initialization']")
println()
}
func printTitle() {
println()
println("Go Language Facilitator v" + version)
println()
}
func checkFail(err error, message string) {
if err != nil {
println("Error: " + err.Error())
fail(message)
}
}
func fail(message string) {
println(message)
os.Exit(1)
}
func argumentError(arg string, verbose bool) {
if !verbose {
printTitle()
}
printHelp()
fail("Unsupported argument found: " + arg)
}
func processArguments(args []string) (source string, dest string, initialize string, verbose bool, include []string) {
initMode := false
for i, arg := range args {
if initMode {
initialize += arg
if i != len(args)-1 {
initialize += " "
}
} else if arg == "--" {
initMode = true
} else if strings.HasPrefix(arg, "-") {
if arg[1:] == "verbose" || arg[1:] == "v" {
verbose = true
} else if strings.HasPrefix(arg[1:], "include=") || strings.HasPrefix(arg[1:], "i=") {
include = append(include, getParseIncludeFilename(arg[1:]))
} else {
argumentError(arg, verbose)
}
} else {
if len(source) == 0 {
source = arg
} else if len(dest) == 0 {
dest = arg
} else {
argumentError(arg, verbose)
}
}
}
if len(source) == 0 || len(dest) == 0 {
if !verbose {
printTitle()
}
printHelp()
fail("Please provide a source and target file.")
}
return
}
func getParseIncludeFilename(arg string) string {
if strings.HasPrefix(arg, "i=") {
arg = arg[len("i="):]
} else if strings.HasPrefix(arg, "include=") {
arg = arg[len("include="):]
} else {
fail("Invalid include argument provided")
}
return arg
}