src/argparse/backend

Types

BuilderObj {.acyclic.} = object
  name*: string ## Command name for subcommand parsers, or program name for
                ## the parent parser.
  symbol*: string ## Unique tag to apply to Parser and Option types to avoid
                  ## conflicts.  By default, this is generated with Nim's
                  ## gensym algorithm.
  components*: seq[Component]
  help*: string
  groupName*: string
  children*: seq[Builder]
  parent*: Option[Builder]
  runProcBodies*: seq[NimNode]
A compile-time object used to accumulate parser options before building the parser
Component = object
  varname*: string
  hidden*: bool
  help*: string
  env*: string
  case kind*: ComponentKind
  of ArgFlag:
      flagShort*: string
      flagLong*: string
      flagMultiple*: bool
      shortCircuit*: bool

  of ArgOption:
      optShort*: string
      optLong*: string
      optMultiple*: bool
      optDefault*: Option[string]
      optChoices*: seq[string]
      optRequired*: bool

  of ArgArgument:
      nargs*: int
      argDefault*: Option[string]

  
ComponentKind = enum
  ArgFlag, ArgOption, ArgArgument
GenResponse = tuple[types: NimNode, procs: NimNode, instance: NimNode]
ParseState = object
  tokens*: seq[string]
  cursor*: int
  extra*: seq[string]        ## tokens that weren't parsed
  done*: bool
  token*: Option[string]     ## The current unprocessed token
  key*: Option[string]       ## The current key (possibly the head of a 'key=value' token)
  value*: Option[string]     ## The current value (possibly the tail of a 'key=value' token)
  valuePartOfToken*: bool    ## true if the value is part of the current token (e.g. 'key=value')
  runProcs*: seq[proc ()]    ## Procs to be run at the end of parsing
  
ShortCircuit = object of CatchableError
  flag*: string
  help*: string
UsageError = object of ValueError

Vars

ARGPARSE_STDOUT = newFileStream(stdout)
builderStack = newSeq(Natural(0))

Procs

proc `$`(b: Builder): string {....raises: [], tags: [].}
proc `$`(state: ref ParseState): string {.inline, ...raises: [], tags: [].}
proc add_command(name: string; group: string; content: proc ()) {.compileTime,
    ...raises: [], tags: [].}
Add a subcommand to a parser
proc add_runProc(body: NimNode) {.compileTime, ...raises: [], tags: [].}
Add a run block proc to the current parser
proc addParser(name: string; group: string; content: proc ()): Builder {.
    ...raises: [], tags: [].}
Add a parser (whether main parser or subcommand) and return the Builder Call generateDefs to get the type and proc definitions.
proc allChildren(builder: Builder): seq[Builder] {....raises: [], tags: [].}
Return all the descendents of this builder
proc consume(state: ref ParseState; thing: ComponentKind) {....raises: [], tags: [].}
Advance the parser, marking some tokens as consumed.
proc generateDefs(builder: Builder): NimNode {....raises: [ValueError, KeyError],
    tags: [].}
Generate the AST definitions for the current builder
proc getHelpText(b: Builder): string {....raises: [ValueError, KeyError], tags: [].}
Generate the static help text string
proc helpProcDef(b: Builder): NimNode {....raises: [ValueError, KeyError], tags: [].}
Generate the help proc for the parser
proc newBuilder(name = ""): Builder {....raises: [], tags: [].}
proc newParseState(args: openArray[string]): ref ParseState {....raises: [],
    tags: [].}
proc optsTypeDef(b: Builder): NimNode {....raises: [ValueError], tags: [].}
Generate the type definition for the return value of parsing:
proc parseProcDef(b: Builder): NimNode {....raises: [ValueError], tags: [].}

Generate the parse proc for this Builder

proc parse(p: MyParser, args: seq[string]): MyOpts =

proc parserTypeDef(b: Builder): NimNode {....raises: [], tags: [].}
Generate the type definition for the Parser object:
type
MyParser = object
proc popleft[T](s: var seq[T]): T
Pop from the front of a seq
proc popright[T](s: var seq[T]; n = 0): T
Pop the nth item from the end of a seq
proc raiseShortCircuit(flagname: string; help: string) {.inline,
    ...raises: [ShortCircuit], tags: [].}
proc setOrAdd(x: var seq[string]; val: string) {....raises: [], tags: [].}
proc setOrAdd(x: var string; val: string) {....raises: [], tags: [].}
proc skip(state: ref ParseState) {.inline, ...raises: [], tags: [].}
proc toVarname(x: string): string {....raises: [], tags: [].}
Convert x to something suitable as a Nim identifier Replaces - with _ for instance