This module provides the standard Nimrod command line parser. It supports one convenience iterator over all command line options and some lower-level features.
TCmdLineKind = enum
cmdEnd,
cmdArgument,
cmdLongoption,
cmdShortOption
-
the detected command line token
TOptParser = object of TObject
cmd: string
pos: int
inShortState: bool
kind*: TCmdLineKind
key*, val*: TaintedString
-
this object implements the command line parser
proc initOptParser(cmdline = ""): TOptParser {.raises: [EInvalidIndex],
tags: [FReadIO].}
-
inits the option parser. If cmdline == "", the real command line (as provided by the OS module) is taken.
proc next(p: var TOptParser) {.rtl, extern: "npo$1", raises: [], tags: [].}
-
parses the first or next option; p.kind describes what token has been parsed. p.key and p.val are set accordingly.
proc cmdLineRest(p: TOptParser): TaintedString {.rtl, extern: "npo$1",
raises: [], tags: [].}
-
retrieves the rest of the command line that has not been parsed yet.
iterator getopt(): tuple[kind: TCmdLineKind, key, val: TaintedString] {.
raises: [EInvalidIndex], tags: [FReadIO].}
-
This is an convenience iterator for iterating over the command line. This uses the TOptParser object. Example:
var
filename = ""
for kind, key, val in getopt():
case kind
of cmdArgument:
filename = key
of cmdLongOption, cmdShortOption:
case key
of "help", "h": writeHelp()
of "version", "v": writeVersion()
of cmdEnd: assert(false)
if filename == "":
writeHelp()