Skip to content
Renaud Guillard edited this page Dec 29, 2013 · 1 revision

Parser pseudocode

Main loop

  • End of control statements are noted end <keyword> but are only present in case of ambiguity
  • then is used in if statements only when the condition is written on multiple lines + Names in monospace font are variables
    • result: The structure containing all options and positional arguments values
    • state: The internal parser set
    • actuve option: The currently processed program option
  • Numerical operation on string means take a substring of
    • (var + 1) value of var from the second character to the end

initialize resultintitializestate## set a to first argument index of the command line while a < number of argument of the command line set arg to value of command line argument a if active option is set and active option type is 'switch' or (active option type is 'argument' and active option current argument count = 1) or (active option type is 'multiargument' and ((active option current argument count + active option argument count) = active option max argument count) then unset active option if context is "end of options" treat arg as a positional argument else if arg = "--" set context to "end of options" unset active option else if arg = "-" if active option is set if active option type is 'multiargument" if active option current argument count is 0 warning #2 Ignore end-of-argument marker add arg to active option current argument list else unset active option else if active option type is 'argument" set arg to active option current argument end if else treat arg as positional argument or subcommand name else if arg starts with '-' remove leading '' from arg if active option is set and active option accepts argument (which should be always true) then append arg to active option current argument list else treat arg as positional argument or subcommand name else if active option is set and active option argument count == 0 append arg to active option current argument list else if arg starts with "--" if active option is set unset active option set arg tail to content of arg after the first '=' set temp cli option name to content of arg before the first '=' set temp option name to content of (arg + 2) before the first '=' if an option can be found using temp option name set active option to this option set active option cli name to temp cli option nmae else fatal error #1 Unknown option <option-cli-name> mark state as aborted break loop if active option is set check if active option is expected if active option is not expected mark active option as 'unexpected' if arg tail is not empty append arg to active option current argument list end if else if arg starts with "-" if active option is set unset active option set arg to content of (arg + 1) while arg is not empty if active option is set unset active option set temp option name to first character of arg set temp cli option name to ('-' + first character of arg) if an option can be found using temp option name set active option to this option set active option cli name to temp cli option nmae check if active option is expected if active option is not expected mark active option as 'unexpected' if active option type is 'argument' or 'multiargument' and (arg + 1) is not empty append (arg + 1) to active option current argument list break while else fatal error #1 Unknown option <option-cli-name> mark state as aborted break loop set arg to content of (arg + 1) end while else if active option is set append arg to active option current argument list else treat arg as positional argument or subcommand set value of a to (a + 1) end while unset active option do set change occurs to false for each option if option type is 'mutli-argument' if option is set and "min argument" attribute is defined and option argument count < min argument then error #11 At least <number> arguments required for <option-cli-name> set change occurs to true unmark option and exclusive group parents else if option type is 'argument' if option is not set and option as a default value and option is expected then set option argument value to default value set change occurs to true mark option and all its parents as set while change occurs for each option of program and selected subcommand if not set and required error #4,#5 or #6 Missing required option ... post process positional arguments according to subcommand or program rule

Procedures


The program interface definition framework

Clone this wiki locally