Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Warnings, use std/parseopt to parse cli arguments #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 50 additions & 64 deletions src/boomer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import opengl, opengl/glx
import la
import strutils
import math
import options
import parseopt

type Shader = tuple[path, content: string]

Expand Down Expand Up @@ -181,7 +181,7 @@ proc main() =
var delaySec = 0.0

# TODO(#95): Make boomer optionally wait for some kind of event (for example, key press)
block:
block parseParams:
proc versionQuit() =
const hash = gorgeEx("git rev-parse HEAD")
quit "boomer-$#" % [if hash.exitCode == 0: hash.output[0 .. 7] else: "unknown"]
Expand All @@ -192,67 +192,53 @@ proc main() =
--new-config [filepath] generate a new default config at [filepath]
-c, --config <filepath> use config at <filepath>
-V, --version show the current version and exit
-w, --windowed windowed mode instead of fullscreen"""
var i = 1
while i <= paramCount():
let arg = paramStr(i)

template asParam(paramVar: untyped, body: untyped) =
if i + 1 > paramCount():
echo "No value is provided for $#" % [arg]
usageQuit()
let paramVar = paramStr(i + 1)
body
i += 2

template asFlag(body: untyped) =
body
i += 1

template asOptionalParam(paramVar: untyped, body: untyped) =
let paramVar = block:
var resultVal = none(string)
if i + 1 <= paramCount():
let param = paramStr(i + 1)
if len(param) > 0 and param[0] != '-':
resultVal = some(param)
resultVal
body
if paramVar.isNone:
i += 1
else:
i += 2

case arg
of "-d", "--delay":
asParam(delayParam):
delaySec = parseFloat(delayParam)
of "-w", "--windowed":
asFlag():
windowed = true
of "-h", "--help":
asFlag():
usageQuit()
of "-V", "--version":
asFlag():
versionQuit()
of "--new-config":
asOptionalParam(configName):
let newConfigPath = configName.get(configFile)

createDir(newConfigPath.splitFile.dir)
if newConfigPath.fileExists:
stdout.write("File ", newConfigPath, " already exists. Replace it? [yn] ")
if stdin.readChar != 'y':
quit "Disaster prevented"

generateDefaultConfig(newConfigPath)
quit "Generated config at $#" % [newConfigPath]
of "-c", "--config":
asParam(configParam):
configFile = configParam
-w, --windowed windowed mode instead of fullscreen

Syntax for short flags: -h / -d:<value> / -d<value>"""

var optParser = initOptParser(
shortNoVal = {'w', 'h', 'v'},
longNoVal = @["new-config", "windowed", "help", "version"]
)

for kind, key, val in getOpt(optParser):
case kind:
of cmdShortOption, cmdLongOption:
case key:
of "d", "delay":
try:
delaySec = parseFloat(val)
except ValueError:
quit "Delay parameter `$#` is not a valid number." % [val]
of "w", "windowed":
windowed = true
of "h", "help":
usageQuit()
of "V", "version":
versionQuit()
of "new-config":
let remParams = optParser.remainingArgs()
let newConfigPath =
if remParams.len > 0 and remParams[0][0] != '-':
remParams[0]
else:
configFile

createDir(newConfigPath.splitFile.dir)
if newConfigPath.fileExists:
stdout.write("File ", newConfigPath, " already exists. Replace it? [yn] ")
if stdin.readChar != 'y':
quit "Disaster prevented"

generateDefaultConfig(newConfigPath)
quit "Generated config at `$#`" % [newConfigPath]
of "c", "config":
configFile = val
else:
echo "Unknown flag `$#`" % [key]
usageQuit()
else:
echo "Unknown flag `$#`" % [arg]
echo "Unknown argument `$#`" % [key]
usageQuit()
sleep(floor(delaySec * 1000).int)

Expand Down Expand Up @@ -329,8 +315,8 @@ proc main() =

discard XMapWindow(display, win)

var wmName = "boomer"
var wmClass = "Boomer"
const wmName: cstring = "boomer"
const wmClass: cstring = "Boomer"
var hints = XClassHint(res_name: wmName, res_class: wmClass)

discard XStoreName(display, win, wmName)
Expand Down