-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patharguments.go
75 lines (65 loc) · 1.96 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
// arguments.go parses arguments in order inferring
// the target path and flags invoked to the best of its ability.
//
package main
import (
filepath "path/filepath"
)
var mode *Mode
// Mode reflects running mode with superset of ls flags and target path
type Mode struct {
inputPath string // target path literal from the argument parsing (e.g. "~/hi")
absolutePath string // absolute path to the target directory (e.g. "/home/dima/hi")
comments []string // free form descriptions of dir in question (like when it is a git repo)
summary bool // no header for file group, file type in desscription column
d bool // shows directories only
h bool // "himan-readable" mode
long bool // "long" form, more details
size bool // "show and order by size" mode
time bool // "show and order by modification time" mode
pyramid bool // align files to the center or to the sides
}
const flagDash = '-'
var err error
// ParseArguments parses the arguments passed on to `lsp` into
// mode flags and target dir.
// There are many excellent libraries that parse flag arguments,
// but I ended up rewriting my own to enable
// `ls`-style single letter triggers within one flag
// (so that `lsp -al` is equivalent to `lsp -a -l` or `lsp -la`)
func ParseArguments(arguments []string) (*Mode, error) {
mode = new(Mode)
for _, l := range arguments[1:] {
if l[0] == flagDash {
// this argument seems to be a flag
for _, flag := range l[1:] {
var f *bool
switch flag {
case 'd':
f = &mode.d
case 'h':
f = &mode.h
case 'l':
f = &mode.long
case 's':
f = &mode.size
case 'p':
f = &mode.pyramid
case 't':
f = &mode.time
}
if f != nil {
*f = true
}
}
} else {
mode.inputPath = mode.inputPath + l
}
}
mode.summary = !(mode.time || mode.size || mode.long)
if mode.inputPath == "" {
mode.inputPath = "."
}
mode.absolutePath, err = filepath.Abs(mode.inputPath)
return mode, err
}