argparse

Author: Max Grender-Jones

Module inspired by python's argparse to provide simple commandline parsing

Types

Command = distinct string
Count = distinct int
Help = distinct bool
Parser = ref ParserObj
The parser object
ParseError = object of CatchableError
RequiredKind = enum
  rkRequired, rkOptional, rkDefault
Controls whether the option/argument is required or optional

Procs

proc `$`(c: Command): string {...}{.borrow.}
proc `$`(c: Count): string {...}{.borrow.}
proc newParser(prolog, epilog, help: string = ""; addHelp = true; longPrefix = "--";
              shortPrefix = '-'; leftBracket = '<'; rightBracket = '>'): Parser {...}{.
    raises: [ValueError], tags: [].}
Creates a new parser
  • prolog is the opening part of the help text
  • epilog is the closing part of the help test
  • longPrefix introduces multi-character options
  • shortPrefix introduces single-character options
proc hasOption(parser: ParseResult; variant: string): bool {...}{.raises: [KeyError],
    tags: [].}
Returns true if parser has a variant option
proc get[T: Value](parse_result: ParseResult; variant: string): T
Gets a value from the ParseResult of the appropriate type
proc gets[T: Value](parse_result: ParseResult; variant: string): seq[T]
Gets a sequence of values from the ParseResult of the appropriate type
proc getInt(parse_result: ParseResult; variant: string): int {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getFloat(parse_result: ParseResult; variant: string): float {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getString(parse_result: ParseResult; variant: string): string {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getBool(parse_result: ParseResult; variant: string): bool {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getCommand(parse_result: ParseResult; variant: string): string {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getParseResult(parse_result: ParseResult; variant: string): ParseResult {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getCount(parse_result: ParseResult; variant: string): int {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getInts(parse_result: ParseResult; variant: string): seq[int] {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getFloats(parse_result: ParseResult; variant: string): seq[float] {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getStrings(parse_result: ParseResult; variant: string): seq[string] {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc getBools(parse_result: ParseResult; variant: string): seq[bool] {...}{.
    raises: [ValueError, KeyError], tags: [].}
proc printHelp(parser: Parser; command: string = extractFilename(getAppFilename())) {...}{.
    raises: [KeyError], tags: [].}
proc parse(parser: Parser; args: seq[string] | seq[TaintedString]; pos: var int;
          returnOnComplete = false): ParseResult
proc parse(parser: Parser; args: seq[string] | seq[TaintedString]): ParseResult
proc parse(parser: Parser; args: string | TaintedString): ParseResult
proc parse(parser: Parser): ParseResult {...}{.raises: [OSError], tags: [ReadIOEffect].}

Funcs

func add[T: Value](parser: var Parser; variants: seq[string]; help: string; default: T;
                 choices: seq[T] = newSeq[T](); name = ""; n = 1;
                 required = RequiredKind.rkDefault)
Adds an option to the parser
  • variants - sequence of triggers for this option
    • -f``or ``--foo imples an optional argument
    • <foo> implies a positional argument or sub-command
  • help - help text for this option
  • default - default value for the option
  • choices - acceptable values for the option
  • name - the canonical name for this option (used to fetch its value). Defaults to the text part of the first variant
  • n - how many values this argument takes (0 implies none, -1 implies unlimited, n>0 implies finitely many)
  • required - whether an argument is optional or required
func add[T: Value](parser: var Parser; variant: string; help: string; default: T;
                 choices: seq[T] = newSeq[T](); name = ""; n = 1;
                 required = RequiredKind.rkDefault)
func addString(parser: var Parser; variants: seq[string]; help: string; default = "";
              name = ""; choices: seq[string] = newSeq[string](); n = 1;
              required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addString(parser: var Parser; variant: string; help: string; default = ""; name = "";
              choices: seq[string] = newSeq[string](); n = 1;
              required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addInt(parser: var Parser; variants: seq[string]; help: string; default = 0;
           name = ""; choices: seq[int] = newSeq[int](); n = 1;
           required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addInt(parser: var Parser; variant: string; help: string; default: int = 0; name = "";
           choices: seq[int] = newSeq[int](); n = 1; required = RequiredKind.rkDefault) {...}{.
    raises: [ValueError], tags: [].}
func addBool(parser: var Parser; variants: seq[string]; help: string; default: bool;
            name = ""; choices: seq[bool] = newSeq[bool](); n = 1;
            required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addBool(parser: var Parser; variant: string; help: string; default: bool; name = "";
            choices: seq[bool] = newSeq[bool](); n = 1;
            required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addCount(parser: var Parser; variants: seq[string]; help: string; default: int = 0;
             name = ""; required = RequiredKind.rkDefault) {...}{.raises: [ValueError],
    tags: [].}
func addCount(parser: var Parser; variant: string; help: string; default: int = 0;
             name = ""; required = RequiredKind.rkDefault) {...}{.raises: [ValueError],
    tags: [].}
func addCommand(parser: var Parser; variants: seq[string]; help: string;
               parsers: OrderedTable[string, Parser]; default = ""; name = "";
               required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addCommand(parser: var Parser; variant: string; help: string;
               parsers: OrderedTable[string, Parser]; default = ""; name = "";
               required = RequiredKind.rkDefault) {...}{.raises: [ValueError], tags: [].}
func addHelp(parser: var Parser; variants: seq[string]; help: string) {...}{.
    raises: [ValueError], tags: [].}