Entry
TSE: Parse: Parameter: Command line: MSDOS: How to possibly parse the command line?
May 15th, 2005 01:31
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 15 May 2021 - 01:29 pm ------------------------
TSE: Parse: Parameter: Command line: MSDOS: How to possibly parse the
command line?
===
e.g.
g32.exe -ic:\temp\test.txt
---
e.g.
e32.exe -lc:\temp\my.mac
---
e.g.
c:\tse\g32.exe -i"c:\temp\ -itisatestdirectory\"
---
e.g.
c:\tse\g32.exe -i"c:\temp\ -itisatestdirectory\" -e"c:\my-macro-
example.mac"
---
e.g.
c:\tse\g32.exe -i"c:\temp\ -itisatestdirectory\" -e"c:\my-macro-
example.mac" -lc:\my.mac
===
In words:
1. The command line is
1. an executable name,
followed by zero or more parameters,
followed by an end of line
1. A parameter is
one or more spaces,
followed by the character '-',
followed by a character,
followed by zero or one filename
1. A filename
starts with a double quote,
followed by some characters
(possibly also spaces and dashes),
finished by a double quote
-- or --
1. A filename contains some characters, but no spaces
===
Backus Naur Form:
---
command line =
+--------<--------+
| |
->-[executablename]->-+->-[parameter]->-+->-[end of line]
| |
+-------->--------+
---
parameter =
+-----<-----+
| |
->-+-(space)->-+-(-)->-[character]->-+->-[filename]->-+->-
| | | |
+-----------+ +-------->-------+
---
filename =
->-+->-(")->-[some characters possibly also spaces]->-(")->-+-->
| |
+->-------[some characters, but no spaces]->-------+-->--+
===
To parse:
1. start parsing from begin of the line, and from left to right (and
not somewhere in the middle thus)
1. if you while parsing from left to right:
1. first find a space with a '-' followed by a character after
it (e.g. ' -i'), it must be a parameter.
1. Then after this parameter comes possibly a filename, which
you then parse as usual
1. If you find a starting double quote, parse further
until you find the closing double quote (it might also
have a '-' with a character after it (e.g. ' -i') in
it, if the filename has quotes around it)
1. If you do not find a double quote, parse it as usual
for a filename
===
To parse: program
---
if not [executablename]
exit
endif
if [end of line]
exit
endif
// read parameters
while ( current character == (space) )
while (space)
read
endwhile
if (-)
read
else
exit
endif
if [character]
read
else
error
exit
endif
if [filename]
if (double quote)
read until finishing (double quote)
else
read until space or end of line
endif
endif
endwhile
if not [end of line]
error
exit
endif
===
Note:
About spaces:
You possibly will have to write this parsing routine
yourself.
If you are relying on the argv[] array in C or C++, or ParamStr() in
Delphi, or other command line parameter arrays in the different
programming languages, which see by design the spaces as parameter
separators, that will thus not work if your filenames contain spaces
(as it will then break the filenames in parts, at the position of this
spaces)
---
---
Internet: see also:
---
----------------------------------------------------------------------