forked from dalinaum/braille-printer-client
/
options.go
112 lines (98 loc) · 2.98 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
// Copyright 2012, Braille Printer Team. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
/* Filename: options.go
* Author: Leonardo YongUkKim <dalinaum@gmail.com>,
* Homin Lee <homin.lee@suapapa.net>
* Created: 2012-10-13 12:44:45.83294 +0900 KST
* Description: Option parsing for braille-printer-client
*/
import (
"flag"
"fmt"
"log"
"os"
)
// TODO Customize exported (capitalized) variables, types, and functions.
var (
CmdHelpUsage string // Custom usage string.
CmdHelpFoot string // Printed after help.
)
// A struct that holds braille-printer-client's parsed command line flags.
type Options struct {
Verbose bool
ServerAddr string
Lang string
Format string
Type string
Status int
}
// Create a flag.FlagSet to parse the braille-printer-client's flags.
func SetupFlags(opt *Options) *flag.FlagSet {
fs := flag.NewFlagSet("braille-printer-client", flag.ExitOnError)
fs.BoolVar(&opt.Verbose, "v", false, "Verbose program output.")
fs.StringVar(&opt.ServerAddr, "a", "http://braille-printer.appspot.com",
"Address of braille-printer (server)")
fs.StringVar(&opt.Lang, "l", "ko", "Braille language {ko|en}")
fs.StringVar(&opt.Format, "f", "svg", "Format to print out {svg|text}")
fs.StringVar(&opt.Type, "t", "all",
"Type to display out {all|label|paper}")
fs.IntVar(&opt.Status, "s", 1,
"Status Value. If item was printed, it should been 1.")
return setupUsage(fs)
}
// Check the braille-printer-client's flags and arguments for acceptable values.
// When an error is encountered, panic, exit with a non-zero status, or override
// the error.
func VerifyFlags(opt *Options, fs *flag.FlagSet) {
switch opt.Lang {
case "ko", "en":
default:
log.Fatalf("Unknown lang, %s! Use one of ko, en.", opt.Lang)
}
switch opt.Format {
case "text", "svg":
default:
log.Fatalf("Unkonw format, %s! Use one of text or svg.",
opt.Format)
}
switch opt.Type {
case "label", "paper", "all":
default:
log.Fatalf(
"Unkonw type, %s! Use one of label, paper or all.",
opt.Type)
}
switch opt.Status {
case 0, 1:
default:
log.Fatalf("Unkonw status, %d! Use one of 0 or 1.",
opt.Status)
}
}
// Print a help message to standard error. See CmdHelpUsage and CmdHelpFoot.
func PrintHelp() { SetupFlags(&Options{}).Usage() }
// Hook up CmdHelpUsage and CmdHelpFoot with flag defaults to function flag.Usage.
func setupUsage(fs *flag.FlagSet) *flag.FlagSet {
printNonEmpty := func(s string) {
if s != "" {
fmt.Fprintf(os.Stderr, "%s\n", s)
}
}
fs.Usage = func() {
printNonEmpty(CmdHelpUsage)
fs.PrintDefaults()
printNonEmpty(CmdHelpFoot)
}
return fs
}
// Parse the flags, validate them, and post-process (e.g. Initialize more complex structs).
func parseFlags() (Options, []string) {
var opt Options
fs := SetupFlags(&opt)
fs.Parse(os.Args[1:])
VerifyFlags(&opt, fs)
// Process the verified Options...
return opt, fs.Args()
}