Initial commit with working cli
commit
1716001eb3
|
@ -0,0 +1 @@
|
||||||
|
main
|
|
@ -0,0 +1,87 @@
|
||||||
|
import std/cmdline
|
||||||
|
import std/parseopt
|
||||||
|
import std/strutils
|
||||||
|
|
||||||
|
type TimeOrTicks = object
|
||||||
|
isTime: bool
|
||||||
|
isTicks: bool
|
||||||
|
|
||||||
|
type Time = object
|
||||||
|
seconds: int = 0
|
||||||
|
minutes: int = 0
|
||||||
|
hours: int = 0
|
||||||
|
days: int = 0
|
||||||
|
|
||||||
|
proc convertToTicks(time: var Time): int =
|
||||||
|
time.hours += time.days * 24
|
||||||
|
time.minutes += time.hours * 60
|
||||||
|
time.seconds += time.minutes * 60
|
||||||
|
|
||||||
|
time.seconds * 20
|
||||||
|
|
||||||
|
proc convertToTime(ticks: var int): Time =
|
||||||
|
# Note that here div is used for integer division specifically
|
||||||
|
const secondsInHour = 3600
|
||||||
|
|
||||||
|
ticks = ticks div 20
|
||||||
|
var days = ticks div (24 * secondsInHour)
|
||||||
|
|
||||||
|
ticks = ticks mod (24 * secondsInHour)
|
||||||
|
var hours = ticks div secondsInHour
|
||||||
|
|
||||||
|
ticks = ticks mod secondsInHour
|
||||||
|
var minutes = ticks div 60
|
||||||
|
|
||||||
|
ticks = ticks mod 60
|
||||||
|
var seconds = ticks
|
||||||
|
|
||||||
|
Time(seconds: seconds, minutes: minutes, hours: hours, days: days)
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
var params = commandLineParams()
|
||||||
|
var p = initOptParser(params)
|
||||||
|
|
||||||
|
var check = TimeOrTicks()
|
||||||
|
var time = Time()
|
||||||
|
var ticks = 0
|
||||||
|
|
||||||
|
while true:
|
||||||
|
p.next()
|
||||||
|
case p.kind
|
||||||
|
of cmdEnd: break
|
||||||
|
of cmdArgument: continue
|
||||||
|
of cmdShortOption, cmdLongOption:
|
||||||
|
case p.key
|
||||||
|
of "t":
|
||||||
|
if check.isTime:
|
||||||
|
quit("Cannot convert ticks while converting time")
|
||||||
|
ticks = parseInt(p.val)
|
||||||
|
check.isTicks = true
|
||||||
|
of "s":
|
||||||
|
if check.isTicks:
|
||||||
|
quit("Cannot convert time while converting ticks")
|
||||||
|
time.seconds = parseInt(p.val)
|
||||||
|
check.isTime = true
|
||||||
|
of "m":
|
||||||
|
if check.isTicks:
|
||||||
|
quit("Cannot convert time while converting ticks")
|
||||||
|
time.minutes = parseInt(p.val)
|
||||||
|
check.isTime = true
|
||||||
|
of "h":
|
||||||
|
if check.isTicks:
|
||||||
|
quit("Cannot convert time while converting ticks")
|
||||||
|
time.hours = parseInt(p.val)
|
||||||
|
check.isTime = true
|
||||||
|
of "d":
|
||||||
|
if check.isTicks:
|
||||||
|
quit("Cannot convert time while converting ticks")
|
||||||
|
time.days = parseInt(p.val)
|
||||||
|
check.isTime = true
|
||||||
|
|
||||||
|
if check.isTime:
|
||||||
|
var result = convertToTicks(time)
|
||||||
|
echo "Ticks: ", result
|
||||||
|
else:
|
||||||
|
var result = convertToTime(ticks)
|
||||||
|
echo "Days: ", result.days, "\nHours: ", result.hours, "\nMinutes: ", result.minutes, "\nSeconds: ", result.seconds
|
||||||
|
|
Loading…
Reference in New Issue