/
cli.go
70 lines (57 loc) · 1.36 KB
/
cli.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
// cli.go
// Copyright (C) 2017 hackliff <xavier.bruhiere@gmail.com>
// Distributed under terms of the MIT license.
package main
import (
"flag"
"fmt"
"os"
"time"
)
const (
DEFAULT_REFRESH = "1s"
DEFAULT_DB_PATH = "/tmp/clip.db"
DEFAULT_COMMAND = "ls"
DEFAULT_LAST = 20
)
type CliOptions struct {
// one of cp|list|watch commands
Command string
// file path for database initialization
DBPath string
// empty database before starting
Reset bool
// human readable refresh rate before updating the clipboard history
Refresh time.Duration
// number of items to show with `ls`
Last int
}
func NewCliOptions() (*CliOptions, error) {
dbPath := flag.String("db", DEFAULT_DB_PATH, "database fs path")
reset := flag.Bool("reset", false, "empty database before starting")
rawRefresh := flag.String("refresh", DEFAULT_REFRESH, "clipboard refresh rate")
last := flag.Int("last", DEFAULT_LAST, "number of clips to show")
flag.Parse()
refreshRate, err := time.ParseDuration(*rawRefresh)
if err != nil {
return nil, err
}
var command string
if flag.NArg() < 1 {
command = DEFAULT_COMMAND
} else {
command = flag.Arg(0)
}
return &CliOptions{
Command: command,
DBPath: *dbPath,
Reset: *reset,
Refresh: refreshRate,
Last: *last,
}, nil
}
func fail(err error) {
fmt.Printf("failed to parse options: %v\n", err)
flag.Usage()
os.Exit(1)
}