-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags.go
183 lines (156 loc) · 3.98 KB
/
flags.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
_ "embed" // for go:embed
"errors"
"flag"
"fmt"
"io"
"strings"
)
var (
//go:embed usage.txt
_usage string
_shortHelp = firstLineOf(_usage)
)
// params defines the parameters for the command line program.
type params struct {
Preface string
Input string // defaults to stdin
Output string // defaults to stdout
Dir string
Offset int
NoTOC bool
Unsafe bool
Diff bool
ColorOutput colorOutput
}
// cliParser parses command line arguments.
type cliParser struct {
Stdout io.Writer // required
Stderr io.Writer // required
version bool
help bool
}
func (p *cliParser) newFlagSet() (*params, *flag.FlagSet) {
flag := flag.NewFlagSet("stitchmd", flag.ContinueOnError)
flag.SetOutput(p.Stderr)
flag.Usage = func() {
fmt.Fprint(p.Stderr, _shortHelp)
}
var opts params
flag.StringVar(&opts.Preface, "preface", "", "")
flag.StringVar(&opts.Output, "o", "", "")
flag.StringVar(&opts.Dir, "C", "", "")
flag.IntVar(&opts.Offset, "offset", 0, "")
flag.BoolVar(&opts.NoTOC, "no-toc", false, "")
flag.Var(&opts.ColorOutput, "color", "")
flag.BoolVar(&opts.Diff, "d", false, "")
flag.BoolVar(&opts.Diff, "diff", false, "")
flag.BoolVar(&opts.Unsafe, "unsafe", false, "")
flag.BoolVar(&p.version, "version", false, "")
flag.BoolVar(&p.help, "help", false, "")
flag.BoolVar(&p.help, "h", false, "")
return &opts, flag
}
type cliParseResult int
const (
cliParseSuccess cliParseResult = iota
cliParseHelp
cliParseError
)
// Parses and returns command line parameters.
// This function does not return an error to ensure
// that error messages are not double-printed.
func (p *cliParser) Parse(args []string) (*params, cliParseResult) {
opts, fset := p.newFlagSet()
if err := fset.Parse(args); err != nil {
return nil, cliParseError
}
args = fset.Args()
if p.version {
fmt.Fprintln(p.Stdout, fset.Name(), strings.TrimSpace(_version))
fmt.Fprintln(p.Stdout, "Copyright (C) 2023 Abhinav Gupta")
fmt.Fprintln(p.Stdout, " <https://github.com/abhinav/stitchmd>")
fmt.Fprintln(p.Stdout, "stitchmd comes with ABSOLUTELY NO WARRANTY.")
fmt.Fprintln(p.Stdout, "This is free software, and you are welcome to redistribute it")
fmt.Fprintln(p.Stdout, "under certain conditions. See source for details.")
return nil, cliParseHelp
}
if p.help {
fmt.Fprint(p.Stdout, _usage)
return nil, cliParseHelp
}
switch len(args) {
case 0:
fmt.Fprintln(p.Stderr, "please specify a file name")
fset.Usage()
return nil, cliParseError
case 1:
opts.Input = args[0]
default:
fmt.Fprintf(p.Stderr, "unexpected arguments: %q\n", args[1:])
fset.Usage()
return nil, cliParseError
}
// "-" means stdin/stdout
if opts.Input == "-" {
opts.Input = ""
}
if opts.Output == "-" {
opts.Output = ""
}
// Reject -d if -o is not set.
if opts.Diff && opts.Output == "" {
fmt.Fprintln(p.Stderr, "cannot use -d without -o")
fset.Usage()
return nil, cliParseError
}
return opts, cliParseSuccess
}
// Returns the first line of the given string.
func firstLineOf(s string) string {
if i := strings.IndexByte(s, '\n'); i >= 0 {
return s[:i+1]
}
return s
}
type colorOutput int
const (
colorOutputAuto colorOutput = iota
colorOutputAlways
colorOutputNever
)
var _ flag.Getter = (*colorOutput)(nil)
func (c colorOutput) String() string {
switch c {
case colorOutputAuto:
return "auto"
case colorOutputAlways:
return "always"
case colorOutputNever:
return "never"
default:
return fmt.Sprintf("unknown (%d)", int(c))
}
}
func (c colorOutput) Get() interface{} {
return c
}
func (c *colorOutput) Set(s string) error {
switch strings.ToLower(strings.TrimSpace(s)) {
case "auto":
*c = colorOutputAuto
case "always", "true":
*c = colorOutputAlways
case "never", "false":
*c = colorOutputNever
default:
return errors.New("must be one of 'always', 'never', 'auto'")
}
return nil
}
// Tells "flag" that the flag argument is optional.
// If not provided, Set will be called with "true".
func (c colorOutput) IsBoolFlag() bool {
return true
}