|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "flag" |
4 | 5 | "fmt"
|
5 | 6 | "log"
|
6 | 7 | "os"
|
| 8 | + "strconv" |
7 | 9 | "time"
|
8 | 10 |
|
9 | 11 | "github.com/eiannone/keyboard"
|
@@ -83,27 +85,27 @@ func secToMins(t ClockTime) (mins, secs uint) {
|
83 | 85 | }
|
84 | 86 |
|
85 | 87 | func main() {
|
| 88 | + var gameTime ClockTime = 0 |
86 | 89 |
|
87 |
| - if len(os.Args) < 2 { |
88 |
| - fmt.Fprintln(os.Stdin, "Usage: chessclk <TIME_SECONDS>") |
89 |
| - os.Exit(1) |
90 |
| - } |
| 90 | + // flags for the different game modes, and a custom time |
| 91 | + gameMode := flag.String("m", "rapid", "Game modes: \n- \"rapid\" (15 min)\n- \"blitz\" (3 min)\n- \"classical\" (120 min)\n- Define a custom time (in secs): \"-m time 60\"\n") |
91 | 92 |
|
92 |
| - var gameTime ClockTime = 0 |
| 93 | + flag.Parse() |
93 | 94 |
|
94 |
| - // If user supplied times, usse for both openents. |
95 |
| - // TODO: Refactor with proper flags interface. |
96 |
| - if os.Args[1] != "" { |
97 |
| - initTime := os.Args[1] |
98 |
| - m, err := time.ParseDuration((initTime)) |
| 95 | + switch *gameMode { |
| 96 | + case "blitz": |
| 97 | + gameTime = ClockTime(180) |
| 98 | + case "classical": |
| 99 | + gameTime = ClockTime(7200) |
| 100 | + case "time": |
| 101 | + secs, err := strconv.Atoi(flag.Args()[0]) |
99 | 102 | if err != nil {
|
100 |
| - log.Fatal("error parsing time argument") |
| 103 | + pterm.Warning.Println("[+] Invalid time given") |
| 104 | + os.Exit(1) |
101 | 105 | }
|
102 |
| - // Convert to proper type |
103 |
| - gameTime = ClockTime(m.Round(time.Second).Seconds()) |
104 |
| - } else { |
105 |
| - log.Println("No argument passed, will use default time value of 15 mins") |
106 |
| - gameTime = ClockTime(15 * 60) |
| 106 | + gameTime = ClockTime(secs) |
| 107 | + default: |
| 108 | + gameTime = ClockTime(900) |
107 | 109 | }
|
108 | 110 |
|
109 | 111 | // Make sure our clock isn't set to a useless zero.
|
|
0 commit comments