Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 3.01 KB

design.org

File metadata and controls

90 lines (65 loc) · 3.01 KB

design of argument parsing

dissattisfactions

  • error reporting, use help
  • usage
  • compile a usage into code
  • call as standalone program and as lib.

solutions

args_t

args_t which keeps all the argvs together in a buffer and releases the buffer on destruction. We need to store the buff = sum of all the lengths of the argvs. We need to store the argv pointers which is an array of string pointers of length argc. On destruction, we need to destroy this. lookup delete[];

when called as standalone, take argc, argv and output shell instructions

called as: > `argprocessor @args`

returns things like: export var1=val1 var2=val2 …

cases

  • single letter option without argument
  • single letter option with attached argument
  • single letter option with adjacent argument
  • single letter options appended together
  • multiple letter option without argument
  • multiple letter option with attached argument (=)
  • multiple letter option with adjacent argument
  • end of arguments (–)
  • invalid arguments

strategy options

Option 1:

Process normal arguments normally. Arrange appended args in connonical form.

  1. Try to process the field assuming it’s in normal form. If the attached flag is set, add a ‘-’ to it before processing.

    If the argument is short, it will match completely, get the value from the next field. Same for long argument.

  2. If the argument was not processed, try to separate it. a) If the argument is long, split at equal sign. b) If the argument is short, split off first letter and set “attached” flag to remember when processing the next fleld that it had been attached.

Option 2:

Track where you are and process explicitly. Treat simple non-appended arguments as a normal case of the complicated case.

get the right context

– -> stop processing –* -> process long argument

  • -> single dash argument

-* -> process single argument(s)

processing arguments

Arguments can either not take a value (boolean flags) or take a value.

Short flogs can be determined by merely examining the next char.

Long flogs can be determined by examining a string. It is also possible to turn off a flag by appending =0 or =false. In any case, we just examine a string.

Short values can be determined by examining a char and string. The string is either the rest of the current parameter or the next parameter if the char is at the end of the current parameter.

Long values can be determined by examining a string and a string. The string is either the part after the ‘=’ or the next parameter.

process long argument

Boolean (takes no value)

Match name completely.

Valued (takes a value)

Match name completely and get the value from the next token. Or Match name and = and get the value from what follows the equal sign.

process short arguments

Boolean (takes no value)

Match letter. Try to match another short argument.

Valued (takes a value)

Match letter completely. Get value from next token. Match letter and get the value from what’s appended.