Module parseopt2

This module provides the standard Nimrod command line parser. It supports one convenience iterator over all command line options and some lower-level features.

Supported syntax:

  1. short options - -abcd, where a, b, c, d are names
  2. long option - --foo:bar, --foo=bar or --foo
  3. argument - everything else

Types

TCmdLineKind = enum 
  cmdEnd,                     ## end of command line reached
  cmdArgument,                ## argument detected
  cmdLongOption,              ## a long option ``--option`` detected
  cmdShortOption              ## a short option ``-c`` detected
the detected command line token
TOptParser = object of TObject
  cmd: seq[string]
  pos: int
  remainingShortOptions: string
  kind*: TCmdLineKind         ## the dected command line token
  key*, val*: TaintedString   ## key and value pair; ``key`` is the option
                              ## or the argument, ``value`` is not "" if
                              ## the option was given a value
  
this object implements the command line parser
TGetoptResult = tuple[kind: TCmdLineKind, key, val: TaintedString]

Procs

proc initOptParser(cmdline: seq[string]): TOptParser {.rtl, 
    raises: [EInvalidIndex], tags: [FReadIO].}
Initalizes option parses with cmdline. cmdline should not contain argument 0 - program name. If cmdline == nil default to current command line arguments.
proc initOptParser(cmdline: string): TOptParser {.rtl, deprecated, 
    raises: [EInvalidIndex], tags: [FReadIO].}
Initalizes option parses with cmdline. Splits cmdline in on spaces and calls initOptParser(openarray[string]) Do not use.
proc initOptParser(): TOptParser {.raises: [EInvalidIndex], tags: [FReadIO].}
Initializes option parser from current command line arguments.
proc next(p: var TOptParser) {.rtl, extern: "npo$1", raises: [E_Base], 
                               tags: [TEffect].}
proc cmdLineRest(p: TOptParser): TaintedString {.rtl, extern: "npo$1", 
    deprecated, raises: [], tags: [].}
Returns part of command line string that has not been parsed yet. Do not use - does not correctly handle whitespace.

Iterators

iterator getopt(): TGetoptResult {.raises: [EInvalidIndex, E_Base], 
                                   tags: [FReadIO, TEffect].}
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) # cannot happen
if filename == "":
  # no filename has been given, so we show the help:
  writeHelp()
Generated: 2014-03-11 21:26:40 UTC