Skip to content

Command line argument parsing

Martin Helmut Fieber edited this page Feb 28, 2021 · 9 revisions

Example

Command-line argument example for Litr:

litr -t="debug" build cpp -p=1 , java --pog=2

Parsing

Simplified Backus–Naur form (BNF):

# Entry point:
arguments = parameters? commands?;

parameters = parameter+;

parameter = parameter_name ( "=" literal )?;

parameter_name = short_parameter | long_parameter;

short_parameter = "-" STRICT_ALPHA;

long_parameter = "--" STRICT_ALPHA;

commands = command ( "," command )?;

command = ALPHA parameters?;

# Numbers are handled as strings as well. This won't make a difference for any shell.
literal = NUMBER | STRING;

NUMBER = "-"? 0-9+ ("." 0-9+)?
STRING = '"' .* '"';
ALPHA = A-Za-z_;
STRICT_ALPHA = A-Za-z;

Legend:

  • | Or
  • () Grouping
  • ? Optional
  • * Repeat 0 to n times
  • + Repeat 1 to n times

Instructions

Parsing the example results in the following set of instructions:

=== Test instructions ===
0000 DEFINE              0 't'
0002 CONSTANT            1 'debug'
0004 BEGIN_SCOPE         2 'build'
0006 BEGIN_SCOPE         3 'cpp'
0008 DEFINE              4 'p'
0010 CONSTANT            5 '1'
0012 EXECUTE             6 'build.cpp'
0014 CLEAR
0015 BEGIN_SCOPE         7 'java'
0017 DEFINE              8 'pog'
0019 CONSTANT            9 '2'
0021 EXECUTE            10 'build.java'

Note: This output is generated by setting LITR_ENABLE_DISASSEMBLE = 1.

Clone this wiki locally