-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
78 lines (64 loc) · 1.91 KB
/
util.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
package main
import (
"strings"
"unicode"
)
const (
aboutDesc = `usql is the universal command-line interface for SQL databases.
`
welcomeDesc = `Type "help" for help.
`
helpDesc = `You are using usql, the universal command-line interface for SQL databases.
Type: \c[onnect] <url> connect to url
\q quit
`
)
// Args are the command line arguments.
type Args struct {
DSN string `arg:"positional,help:database url"`
Commands []string `arg:"-c,--command,separate,help:run only single command (SQL or internal) and exit"`
File string `arg:"-f,--file,help:execute commands from file then exit"`
Out string `arg:"-o,--output,help:output file"`
HistoryFile string `arg:"--hist-file,env:USQL_HISTFILE,help:history file"`
Username string `arg:"-U,--username,help:database user name"`
DisablePretty bool `arg:"-p,--disable-pretty,help:disable pretty formatting"`
NoRC bool `arg:"-X,--disable-rc,help:do not read start up file"`
}
// Description provides the go-arg description.
func (a *Args) Description() string {
return aboutDesc
}
// startsWith checks that s begins with the specified prefix and is followed by
// at least one space, returning the remaining string trimmed of spaces.
//
// ie, a call of startsWith(`\c blah `, `\c`) should return `blah`.
func startsWith(s string, prefix string) (string, bool) {
if prefix == "" {
return s, true
}
var i int
rs, rslen := []rune(s), len(s)
for ; i < rslen; i++ {
if !unicode.IsSpace(rs[i]) {
break
}
}
if i >= rslen {
return "", false
}
match := true
ps, pslen := []rune(prefix), len(prefix)
if i+pslen+1 > rslen {
return "", false
}
for j := 0; j < pslen && i+j < rslen; j++ {
match = match && rs[i+j] == ps[j]
if !match {
return "", false
}
}
if !unicode.IsSpace(rs[i+pslen]) {
return "", false
}
return strings.TrimSpace(string(rs[i+pslen+1:])), true
}