use golang flag instead of external getopt library

This commit is contained in:
Vineet K 2023-11-20 10:06:35 -05:00
parent 23226c46a1
commit 138121330e
4 changed files with 51 additions and 24 deletions

View File

@ -63,7 +63,7 @@ func submit_code(course_code int, attendance_code string) {
func launch_aplus(course_code int) string {
err := init_aplus_toolid(course_code)
if err != nil {
fmt.Println("Failed to get A+ base URL and tool ID")
fmt.Println("Failed to get A+ base URL and tool ID. Try again with a different course code.")
os.Exit(1)
}

4
go.mod
View File

@ -1,7 +1,5 @@
module codeberg.org/flpolyaplus/aplus
go 1.21.3
go 1.21.1
require golang.org/x/net v0.18.0
require github.com/pborman/getopt v1.1.0 // indirect

2
go.sum
View File

@ -1,4 +1,2 @@
github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0=
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=

67
main.go
View File

@ -1,43 +1,74 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/pborman/getopt"
)
var base_link string
var base_aplus_link string
var external_tools_code int
var token string
var client http.Client
func usage() {
fmt.Println("Usage: aplus [--code attendance_code --course course_code] [--list] [--listfav] [--help]")
}
func main() {
initialize()
code := getopt.StringLong("code", 'c', "", "Code to enter")
course := getopt.IntLong("course", 'C', -1, "Canvas course number")
list_all := getopt.BoolLong("list-all", 'L', "List all courses")
list_favorites := getopt.BoolLong("list-favorites", 'F', "List favorite courses")
help := getopt.BoolLong("help", 'h', "Help")
var code string
var course int
var listall bool
var listfav bool
var help bool
getopt.Parse()
flag.StringVar(&code, "code", "", "5 character attendance code")
flag.StringVar(&code, "c", "", "5 character attendance code")
flag.IntVar(&course, "course", -1, "canvas course code")
flag.IntVar(&course, "C", -1, "canvas course code")
flag.BoolVar(&listall, "list", false, "list all canvas courses")
flag.BoolVar(&listall, "L", false, "list all canvas courses")
flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses")
flag.BoolVar(&listfav, "F", false, "list favorited canvas courses")
flag.BoolVar(&help, "help", false, "view usage message")
flag.BoolVar(&help, "h", false, "view usage message")
if *help || (*list_all == false && *list_favorites == false && *code == "" && *course == -1) {
getopt.Usage()
flag.Parse()
if (code == "" && course == -1 && !listall && !listfav) {
usage()
os.Exit(1)
}
if (listall) {
list_all_courses()
os.Exit(0)
}
if *list_all {
list_all_courses()
}
if *list_favorites {
if (listfav) {
list_favorite_courses()
os.Exit(0)
}
if *code != "" && *course != -1 {
fmt.Printf("%d %s", *course, *code)
submit_code(*course, *code)
if (help) {
usage()
os.Exit(0)
}
if (len(code) != 5) {
fmt.Println("Attendance code must be 5 characters long")
os.Exit(1)
}
if code != "" && course != -1 {
fmt.Printf("%d %s\n", course, code)
submit_code(course, code)
} else {
usage()
os.Exit(1)
}
}