Parse command-line arguments: Difference between revisions
Content deleted Content added
m highlighted the case shown, split the case from it's introduction, used bigger font. |
m →{{header|REXX}}: changed/added comments and whitespace, changed indentations, separated blocks of comments into preformatted text blocks. |
||
Line 408:
=={{header|REXX}}==
<pre>
╔═════════════════════════════════════════════════════════════════════════════╗
║ The subject of parsing text (such as a command line) is ingrained in the ║
║ REXX language; it has a PARSE instruction. It's too rich to describe all ║
║ its functionality/capabilities here, but since the task isn't described, ║
║ I'm assuming the NC [NetCat] example (or something like it) is
║ be parsed, and I'm guessing at its syntax (from other examples found on
║ the web), but I'll take a incomplete stab at it. ║
║ nc -u xxx -p port1 ··· ║
║ nc -u -p port1 ··· ║
║ ║
║ where the -u option has an operand in the first case, but not the 2nd, ║▼
║ even though there is something following the -u option. ║▼
║ ║
║ with a minus sign [-]. ║
╚═════════════════════════════════════════════════════════════════════════════╝
</pre>
<lang rexx>/*REXX program demonstrates one method to parse options for a command (entered on the CL*/
do while opts\=='' /*keep parsing 'til all opts examined*/
parse var opts x opts /*obtain a single keyword from options.*/
select /*hard-coded WHENs for option detection*/
▲For the most part, every command seems to have its own rules for their
when x=='-e' then parse var opts !.e_ opts▼
▲operands (options), as does the NC command. For instance:
when x=='-p' then parse var opts !.p_ opts▼
nc -u xxx -p port1 ...▼
and▼
▲where the -u option has an operand in the first case, but not the 2nd,
▲even though there is something following the -u option.
▲I can only assume that any operand for the -u option can't start with a
▲ /*parse optons for a command. */
▲parse arg opts /*this preserves the case of opts*/
▲opts=space(opts) /*this probably should be done to*/
▲ /* get rid of extraneous blanks.*/
▲!.= /*assume all options to be null. */
▲ when x=='-e' then parse var opts !.e_ opts
▲ when x=='-p' then parse var opts !.p_ opts
when x=='-n' then !.z_=1
when x=='-u' then parse var opts !.uname_ !.unnn_ opts
when x=='-ul' then parse var opts !.ul_ opts
when x=='-vzu' then parse var opts !.vzu_ !.vzurange opts
when x=='-w' then parse var opts !.wStart_ !.waddr_
▲ !.wrange_1 '-' !.wrange_2 opts
when x=='-z' then !.=1
otherwise
end /*select*/
end /*do while
/*check for conflicts here and/or validity of values.*/
if !.z_==1 & !.n_==1 then
if !.wrange_1\=='' then do /*
if \
yada yada yada
.
.
.
end
Note: a programming trick is to append (say) an underscore to an▼
option's name as to not preclude that variable being used elsewhere▼
...and still more stuff...
in the REXX program. That way, the option J can be used, as well▼
exit /*stick a fork in it, we're all done. */
as the variable J in the program.▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
isInt: return datatype(arg(1), 'W') /*return 1 if argument is an integer*/
isNum: return datatype(arg(1), 'N') /*return 1 if argument is a number.*/
sayer: say; say '***error***' arg(1); exit 13</lang>
<pre>
╔═══════════════════════════════════════════════════════════════════════╗
▲║ be used, as well as the variable J in the program. ║
╚═══════════════════════════════════════════════════════════════════════╝
</pre>
=={{header|Ruby}}==
|