-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
72 lines (59 loc) · 1.46 KB
/
flag.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
package main
import (
"os"
"fmt"
"flag"
)
const (
version = "0.0.1"
description = "Synchronize audio collection among group of individuals"
)
// positional args template
const args = `
Positional Args:
LABEL give label for subsequent use
PATH1 1st directory path
PATH2 2nd directory path
`
// usage template
const printUsage = `
ssync v%s
%s
Usage: ssync [OPTIONS] LABEL PATH1 PATH2
%s
Options:
`
var flagMain *flag.FlagSet
var flagForcePath int
var flagConfirm, flagVersion bool
func init() {
flagMain = flag.NewFlagSet("main", flag.ContinueOnError)
// setup options
flagMain.IntVar(&flagForcePath, "force", 0, "update modified using this path regardless" +
" of modified timestamp (0=timestamp, 1=PATH1, 2=PATH2)")
flagMain.BoolVar(&flagConfirm, "confirm", false, "confirm before deleting files")
flagMain.BoolVar(&flagVersion, "version", false, "print program version, then exit")
// --help
flagMain.Usage = func() {
fmt.Printf(printUsage, version, description, args)
// print options from built-in flag helper
flagMain.PrintDefaults()
fmt.Println()
}
}
// handle flag --help && --version
func processFlags() ([]string, bool) {
// show --help unless args
if len(os.Args) < 4 {
flagMain.Usage()
return os.Args, false
}
flagMain.Parse(os.Args[1:])
a := flagMain.Args()
// --version
if flagVersion {
fmt.Printf("%s\n", version)
return a, false
}
return a, true
}