Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Apr 14, 2024
1 parent 895db01 commit f4dec7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
40 changes: 19 additions & 21 deletions src/kapsis/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type
# keep indexing the registered arguments
callback: proc(v: Values) {.nimcall.}
# the command callback
isOptional: bool
of ctCmdSep:
label: string
of ctCmdDir:
Expand All @@ -134,10 +135,12 @@ type

KapsisSettings = object
usageIndent = 2
mainCommandId: string

KapsisCli = ref object
pkg*: tuple[description, version, author, license: string]
commands = newOrderedTable[string, KapsisCommand]()
mainCommand: KapsisCommand
settings: KapsisSettings

var Kapsis* = KapsisCli()
Expand Down Expand Up @@ -375,24 +378,9 @@ proc parse(cmd: NimNode, cmdParent: NimNode = nil): NimNode {.compileTime.} =
echo "ok"
else:
error("Invalid short flag `-" & $(x[1]) & "`")
# # let argDatatype = parseEnum[KapsisValueType](x[0].strVal)
# # add result,
# # newCall(
# # ident "addArg",
# # cmdx,
# # newLit(x[1].strVal),
# # ident "cmdArgument",
# # ident argDatatype.symbolName()
# # )
# else:
# error("Invalid short flag `-" & x[1].strVal & "`")
# elif x[0].eqIdent("--"):
# # add long flags
# if x[1].strVal.len > 1:
# discard
# else:
# error("Invalid long flag `--" & x[1].strVal & "`")
# else: discard # todo error
elif x[0].eqIdent("?"):
# parse optional arguments
discard # todo
else: discard # error?
cmdobj.addkv("ctype", ident symbolName(cmdType))
case cmdType
Expand Down Expand Up @@ -562,6 +550,10 @@ template collectInputData(values: var ValuesTable,
# # Retrieve a callback ident name based on command name
# var cbid: string

var kapsisSettings {.compileTime.} = KapsisSettings()
macro settings*(mainCommand: static string) =
kapsisSettings.mainCommandId = mainCommand

macro commands*(x: untyped, extras: untyped = nil) =
# Register commands at compile-time.
collectPackageInfo()
Expand All @@ -587,17 +579,23 @@ macro commands*(x: untyped, extras: untyped = nil) =

# init runtime parser
add blockStmt, quote do:
if Kapsis.commands.hasKey(`kapsisSettings`.mainCommandId):
Kapsis.mainCommand = Kapsis.commands[`kapsisSettings`.mainCommandId]
var
id: KapsisInput
p = initOptParser(quoteShellCommand(commandLineParams()))
input = p.getopt.toSeq()
inputValues = ValuesTable()
inputFlags: seq[(string, string)]
hasDefaultCommand = Kapsis.mainCommand != nil
if input.len == 0:
printUsage()
quit(QuitSuccess)
id = input[0]
input.delete(0)
var inputFlags: seq[(string, string)]
if Kapsis.commands.hasKey(input[0].key) == false and hasDefaultCommand:
id = (cmdArgument, Kapsis.mainCommand.id, "")
else:
id = input[0]
input.delete(0)
case id.kind
of cmdArgument:
if Kapsis.commands.hasKey(id.key):
Expand Down
4 changes: 2 additions & 2 deletions src/kapsis/runtime.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import std/[tables, strutils]

from ./app import Values, Value, KapsisValueType, KapsisPath,
getStr, getBool, getFloat, getInt, getPath,
getStr, getBool, getFloat, getInt, getPath, getFile,
getFilename, getDir, getMilliseconds, getSeconds,
getMinutes, getHours, getDays, getMonths, getYears,
getJson, getYaml

export Values, Value, KapsisValueType, KapsisPath,
getStr, getBool, getFloat, getInt, getPath,
getStr, getBool, getFloat, getInt, getPath, getFile,
getFilename, getDir, getMilliseconds, getSeconds,
getMinutes, getHours, getDays, getMonths, getYears,
getJson, getYaml
Expand Down

0 comments on commit f4dec7b

Please sign in to comment.