Skip to content

Commit 61c0eb8

Browse files
authored
Merge pull request jdefrancesco#1 from rodafr/flags
2 parents 9548187 + 2423b54 commit 61c0eb8

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

Diff for: main.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"log"
67
"os"
8+
"strconv"
79
"time"
810

911
"github.com/eiannone/keyboard"
@@ -83,27 +85,27 @@ func secToMins(t ClockTime) (mins, secs uint) {
8385
}
8486

8587
func main() {
88+
var gameTime ClockTime = 0
8689

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")
9192

92-
var gameTime ClockTime = 0
93+
flag.Parse()
9394

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])
99102
if err != nil {
100-
log.Fatal("error parsing time argument")
103+
pterm.Warning.Println("[+] Invalid time given")
104+
os.Exit(1)
101105
}
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)
107109
}
108110

109111
// Make sure our clock isn't set to a useless zero.

0 commit comments

Comments
 (0)