-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
234 lines (197 loc) · 4.16 KB
/
options.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Command-line options handling
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
//
// Froxy command
//
type OptCmd int
const (
OptCmdNone OptCmd = iota
OptCmdDebug
OptCmdRunFg
OptCmdHelp
OptCmdInstall
OptCmdKill
OptCmdOpen
OptCmdRunBg
OptCmdUninstall
)
//
// Command flags
//
type OptFlags int
const (
OptFlgNoRun OptFlags = 1 << iota
OptFlgNoAutostart
OptFlgNoShortcut
)
//
// Test if some of particular flags are set
//
func (flags OptFlags) Test(mask OptFlags) bool {
return flags&mask != 0
}
//
// Parsed options
//
type Options struct {
Cmd OptCmd // Froxy command
Flags OptFlags // Command flags
Port int // TCP port
}
//
// Parse options
//
func (opt *Options) Parse(env *Env) error {
// No arguments is a special case
args := os.Args[1:]
if len(args) == 0 {
*opt = Options{Cmd: OptCmdNone}
return nil
}
// Setup parser
flagset := flag.NewFlagSet("", flag.ContinueOnError)
flagset.Usage = func() {}
flagset.SetOutput(ioutil.Discard)
debug := flagset.Bool("debug", false, "")
fg := flagset.Bool("fg", false, "")
install := flagset.Bool("i", false, "")
kill := flagset.Bool("k", false, "")
open := flagset.Bool("open", false, "")
run := flagset.Bool("r", false, "")
uninstall := flagset.Bool("u", false, "")
norun := flagset.Bool("norun", false, "")
noautostart := flagset.Bool("noautostart", false, "")
port := flagset.Int("p", env.GetPort(), "")
// Parse arguments
err := flagset.Parse(args)
if err == flag.ErrHelp {
opt.Cmd = OptCmdHelp
return nil
}
if err != nil {
return err
}
if flagset.NArg() != 0 {
return fmt.Errorf("Unrecognized argument %q", flagset.Arg(0))
}
// Decode command
commands := []struct {
flg bool
cmd OptCmd
}{
{*debug, OptCmdDebug},
{*fg, OptCmdRunFg},
{*install, OptCmdInstall},
{*kill, OptCmdKill},
{*open, OptCmdOpen},
{*run, OptCmdRunBg},
{*uninstall, OptCmdUninstall},
}
var cmd OptCmd
for _, c := range commands {
if c.flg {
if cmd == OptCmdNone {
cmd = c.cmd
} else {
return errors.New("Multiple commands not allowed")
}
}
}
if cmd == OptCmdNone {
return errors.New("Missed command")
}
// Decode flags
flags := []struct {
flg bool
bit OptFlags
}{
{*norun, OptFlgNoRun},
{*noautostart, OptFlgNoAutostart},
}
var bits OptFlags
for _, f := range flags {
if f.flg {
bits |= f.bit
}
}
// Check port
if *port < 1 || *port > 0xffff {
return fmt.Errorf("Port number %d out of range", *port)
}
// Pack result
opt.Cmd = cmd
opt.Flags = bits
opt.Port = *port
return nil
}
//
// Print full usage
//
func (opt *Options) Usage(env *Env, full bool) {
const short_usage = `Usage: froxy command [options]
Common commands:
froxy -i [-p port] [-norun] [-noshortcut] [-noautostart]
Install and start the ${PROG}
froxy -u
Uninstall the ${PROG}
froxy -h
Print all commands and options
`
const full_usage = `Usage: froxy command [options]
Commands:
-debug Run ${PROG} in debug mode
-fg Run ${PROG} in foreground
-h Print help page
-i Install and start the ${PROG}
-k Kill running ${PROG}
-open Open ${PROG} configuration in browser window
-r Run ${PROG} in background
-u Uninstall the ${PROG}
Options:
-noautostart Don't add ${PROG} to autostart
-norun Don't run after installation
-noshortcut Don't create desktop shortcut
-p port TCP port (default ${PORT})
Advanced options:
-fg Run in foreground
`
usage := short_usage
if full {
usage = full_usage
}
usage = os.Expand(usage, func(name string) string {
switch name {
case "prog":
return strings.ToLower(PROGRAM_NAME)
case "PROG":
return PROGRAM_NAME
case "PORT":
return strconv.Itoa(env.GetPort())
}
return ""
})
print(usage)
os.Exit(0)
}
//
// Print error message and exit
//
func (opt *Options) Error(err error) {
println(err.Error())
println("Try " + strings.ToLower(PROGRAM_NAME) + " -h for more information.")
os.Exit(1)
}