From 65fb0814646444c3a1656ca952fb4613fe09bd56 Mon Sep 17 00:00:00 2001 From: Archargelod Date: Sat, 28 Oct 2023 18:27:31 +0800 Subject: [PATCH 1/2] use const for constant values --- src/boomer.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index a30fb11..ce05606 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -329,8 +329,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) From da408bf85c638ab2507ab58c86083e1371d819ab Mon Sep 17 00:00:00 2001 From: Archargelod Date: Sat, 28 Oct 2023 20:06:26 +0800 Subject: [PATCH 2/2] parse cli arguments with std/parseopt --- src/boomer.nim | 110 +++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 62 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index ce05606..344c488 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -14,7 +14,7 @@ import opengl, opengl/glx import la import strutils import math -import options +import parseopt type Shader = tuple[path, content: string] @@ -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"] @@ -192,67 +192,53 @@ proc main() = --new-config [filepath] generate a new default config at [filepath] -c, --config use config at -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: / -d""" + + 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)