-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
83 lines (68 loc) · 1.77 KB
/
main.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
79
80
81
82
83
package main
import (
"github.com/urfave/cli"
"os"
"fmt"
)
func main() {
app := cli.NewApp()
app.Name = "bsearch"
app.Usage = "utility for binary searching a sorted file for lines that start with the search key"
app.Version = "1.0.2"
app.ArgsUsage = "SEARCH_KEY FILENAME"
var reverse = false
var ignoreWhitespace = false
var caseInsensitive = false
var numeric = false
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "r,reverse",
Usage: "the reverse flag indicates the file is sorted in descending order",
Destination: &reverse,
},
cli.BoolFlag{
Name: "i,ignore-case",
Usage: "case insensitive",
Destination: &caseInsensitive,
},
cli.BoolFlag{
Name: "t,trim",
Usage: "ignore whitespace",
Destination: &ignoreWhitespace,
},
cli.BoolFlag{
Name: "n,numeric",
Usage: "use numeric comparison",
Destination: &numeric,
},
}
app.Action = func(c *cli.Context) error {
if c.NArg() != 2 {
fmt.Println("Usage: bsearch [options] SEARCH_KEY FILENAME")
fmt.Println("Try 'bsearch --help' for more information.")
os.Exit(1)
}
searchCriteria := c.Args().Get(0)
fileName := c.Args().Get(1)
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fmt.Println("bsearch: no such file")
os.Exit(1)
}
var compareMode CompareMode = 0
if ignoreWhitespace {
compareMode = compareMode | IgnoreWhitespace
}
if caseInsensitive {
compareMode = compareMode | CaseInsensitive
} else if numeric {
compareMode = compareMode | Numeric
}
bsearch := NewBinarySearch(fileName, reverse, compareMode)
startPosition := bsearch.FindStart(searchCriteria)
if startPosition > -1 {
bsearch.PrintMatch(startPosition, searchCriteria)
}
return nil
}
app.Run(os.Args)
}