Skip to content

Command line argument parsing

Martin Helmut Fieber edited this page Mar 1, 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 [reference]):

# 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 in development.

More examples

Here are more command-line examples for the given BNF:

litr --help                          # Show litr help
litr -h                              # Show litr help

litr build                           # Exec command "build"

litr build cpp                       # Exec command "cpp" inside "build"

litr build cpp,java                  # Exec "cpp" and "java" in that order inside "build"
litr build cpp, java                 # Exec "cpp" and "java" in that order inside "build"

litr build,run                       # Exec "build" and then "run"
litr build, run                      # Exec "build" and then "run"

litr build cpp another               # Exec "another" inside "cpp" inside "build"

litr --target="release" build,run    # Exec "build" and "run", both with the param "target=release"
litr --target="release" build, run   # Exec "build" and "run", both with the param "target=release"

litr build -t="release"              # Exec "build" with short param "t=release"

litr build -t="release" --debug -p=2 # Exec "build" with 3 different parameters

litr -t="release" build, run -p=1    # Exec "build" and "run" with parameter "t", and "run" extra with "p"
litr -t="release" build, run -p=1.23 # Exec "build" and "run" with parameter "t", and "run" extra with "p"
Clone this wiki locally