summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorvin <git@vineetk.net>2023-11-20 10:06:35 -0500
committervin <git@vineetk.net>2023-11-20 10:27:50 -0500
commit138121330e97cf6a31a4abd91b8bec206d7d7f80 (patch)
tree0032911ae231e1cd3782cc61417a2d584ee1e015 /main.go
parent23226c46a16bd1037016c1578b608eb6d23f7546 (diff)
use golang flag instead of external getopt library
Diffstat (limited to 'main.go')
-rw-r--r--main.go63
1 files changed, 47 insertions, 16 deletions
diff --git a/main.go b/main.go
index 9386b46..93defd3 100644
--- a/main.go
+++ b/main.go
@@ -1,43 +1,74 @@
1package main 1package main
2 2
3import ( 3import (
4 "flag"
4 "fmt" 5 "fmt"
5 "net/http" 6 "net/http"
6 "os" 7 "os"
7
8 "github.com/pborman/getopt"
9) 8)
10 9
11var base_link string 10var base_link string
11var base_aplus_link string
12var external_tools_code int
12var token string 13var token string
13var client http.Client 14var client http.Client
14 15
16func usage() {
17 fmt.Println("Usage: aplus [--code attendance_code --course course_code] [--list] [--listfav] [--help]")
18}
19
15func main() { 20func main() {
16 initialize() 21 initialize()
17 22
18 code := getopt.StringLong("code", 'c', "", "Code to enter") 23 var code string
19 course := getopt.IntLong("course", 'C', -1, "Canvas course number") 24 var course int
20 list_all := getopt.BoolLong("list-all", 'L', "List all courses") 25 var listall bool
21 list_favorites := getopt.BoolLong("list-favorites", 'F', "List favorite courses") 26 var listfav bool
22 help := getopt.BoolLong("help", 'h', "Help") 27 var help bool
23 28
24 getopt.Parse() 29 flag.StringVar(&code, "code", "", "5 character attendance code")
30 flag.StringVar(&code, "c", "", "5 character attendance code")
31 flag.IntVar(&course, "course", -1, "canvas course code")
32 flag.IntVar(&course, "C", -1, "canvas course code")
33 flag.BoolVar(&listall, "list", false, "list all canvas courses")
34 flag.BoolVar(&listall, "L", false, "list all canvas courses")
35 flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses")
36 flag.BoolVar(&listfav, "F", false, "list favorited canvas courses")
37 flag.BoolVar(&help, "help", false, "view usage message")
38 flag.BoolVar(&help, "h", false, "view usage message")
25 39
26 if *help || (*list_all == false && *list_favorites == false && *code == "" && *course == -1) { 40 flag.Parse()
27 getopt.Usage() 41
28 os.Exit(0) 42 if (code == "" && course == -1 && !listall && !listfav) {
43 usage()
44 os.Exit(1)
29 } 45 }
30 46
31 if *list_all { 47 if (listall) {
32 list_all_courses() 48 list_all_courses()
49 os.Exit(0)
33 } 50 }
34 51
35 if *list_favorites { 52 if (listfav) {
36 list_favorite_courses() 53 list_favorite_courses()
54 os.Exit(0)
55 }
56
57 if (help) {
58 usage()
59 os.Exit(0)
60 }
61
62 if (len(code) != 5) {
63 fmt.Println("Attendance code must be 5 characters long")
64 os.Exit(1)
37 } 65 }
38 66
39 if *code != "" && *course != -1 { 67 if code != "" && course != -1 {
40 fmt.Printf("%d %s", *course, *code) 68 fmt.Printf("%d %s\n", course, code)
41 submit_code(*course, *code) 69 submit_code(course, code)
70 } else {
71 usage()
72 os.Exit(1)
42 } 73 }
43} 74}