This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (135 loc) · 3.98 KB
/
main.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
package main
/*stwmmensa accepts the following arguments:
-h --help: Print this usage info and quit
-o --output: set the path to the output file (mandatory)
-l --location: set mensa location id (default: 421)
-f --format: select format (default: xml)
** Location
Here is a list of valid location codes with the name of the corresponding mensa:
411 : Mensa Leopoldstraße
412 : Mensa Martinsried
421 : Mensa Arcisstraße
422 : Mensa Garching
423 : Mensa Weihenstephan
431 : Mensa Lothstraße
432 : Mensa Pasing
** Format
xml : a generic xml file is created
lis : generate html snippet for LIS-infoscreen
** Examples:
stwmmensa -l 411 -o /my/path/leopold.xml -f xml
stwmmensa --location=423 --output=weihenstephan.html --format=lis
*/
import (
"fmt"
"log"
"os"
"strings"
"github.com/folixg/stwmmensa"
)
const help = `stwmmensa accepts the following arguments:
-h --help: Print this usage info and quit
-o --output: set the path to the output file (mandatory)
-l --location: set mensa location id (default: 421)
-f --format: select format (default: xml)
** Location
Here is a list of valid location codes with the name of the corresponding mensa:
411 : Mensa Leopoldstraße
412 : Mensa Martinsried
421 : Mensa Arcisstraße
422 : Mensa Garching
423 : Mensa Weihenstephan
431 : Mensa Lothstraße
432 : Mensa Pasing
** Format
xml : a generic xml file is created
lis : generate html snippet for LIS-infoscreen
** Examples:
stwmmensa -l 411 -o /my/path/leopold.xml -f xml
stwmmensa --location=423 --output=weihenstephan.html --format=lis
`
// Args is used to store command line arguments.
type Args struct {
Location string
Outfile string
Format string
}
/*
ParseArgs checks if the command line arguments are valid.
The input osargs is supposed to be the output of os.Args
Known arguments are:
-h --help: Print this usage info and quit
-o --output: set the path to the output file (mandatory)
-l --location: set mensa location id (default: 421)
-f --format: select format (default: xml)
If -h or --help is detected a "help" error is returned.
If the parsed command line aguments are not valid an other non-nil error
is returned. In both cases the returned arguments Args do not contain valid
command line arguments.
*/
func ParseArgs(osargs []string) (Args, error) {
// initialize internal arguents with empty strings
var r Args
r.Location = ""
r.Outfile = ""
r.Format = ""
i := 1
for i < len(osargs) {
switch {
case osargs[i] == "-h", osargs[i] == "--help":
//printHelp()
return r, fmt.Errorf("help")
case osargs[i] == "-l":
r.Location = osargs[i+1]
i += 2
case strings.HasPrefix(osargs[i], "--location="):
r.Location = strings.TrimPrefix(osargs[i], "--location=")
i++
case osargs[i] == "-o":
r.Outfile = osargs[i+1]
i += 2
case strings.HasPrefix(osargs[i], "--output="):
r.Outfile = strings.TrimPrefix(osargs[i], "--output=")
i++
case osargs[i] == "-f":
r.Format = osargs[i+1]
i += 2
case strings.HasPrefix(osargs[i], "--format="):
r.Format = strings.TrimPrefix(osargs[i], "--format=")
i++
default:
return r, fmt.Errorf("unknown argument " + osargs[i] + ". Run stwmmensa -h for help")
}
}
// was the mandatory output file provided?
if r.Outfile == "" {
return r, fmt.Errorf("no output file provided. Run stwmmensa -h for help")
}
// set default values for argmunets we did not get via command line
if r.Location == "" {
r.Location = "421"
}
if r.Format == "" {
r.Format = "xml"
}
// check if we have valid values
if !stwmmensa.LocationValid(r.Location) {
return r, fmt.Errorf(r.Location + " is not a valid location identifier. Run stwmmensa -h for help")
}
if !stwmmensa.FormatValid(r.Format) {
return r, fmt.Errorf(r.Format + " is not a valid format. Run stwmmensa -h for help")
}
return r, nil
}
func main() {
// parse command line arguments
args, err := ParseArgs(os.Args)
if err != nil {
if err.Error() == "help" {
fmt.Print(help)
os.Exit(0)
}
log.Fatal(err)
}
stwmmensa.UpdateMenuFile(args.Location, args.Format, args.Outfile)
}