Parse command-line arguments: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, separated blocks of comments into preformatted text blocks.
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>
<lang rexx>/*────────────────────────────────────────────────────────────────────────
╔═════════════════════════════════════════════════════════════════════════════╗
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 totoe be
║ be parsed, and I'm guessing at its syntax (from other examples found on the
║ the web), but I'll take a incomplete stab at it.
/* get rid of extraneous blanks.*/
For the most part, every command seemsappear to have itstheir own rules for their
operands (options), as does the NC command. For instance:
/*parse optons for a command. */
║ nc -u xxx -p port1 ··· ║
and !.wrange_1 '-' !.wrange_2 opts
║ 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.
║ ║
I║ It can only assumebe assumed that any operand for the -u option can't start with a
║ with a minus sign [-]. ║
╚═════════════════════════════════════════════════════════════════════════════╝
</pre>
<lang rexx>/*REXX program demonstrates one method to parse options for a command (entered on the CL*/
parse arg opts /*this preserves the case of opts options. */
opts=space(opts) /*thiselide probablysuperfluous shouldblanks bein doneoptions. to*/
!.= /*assume all options to be "null." (default).*/
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
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.
 
I can only assume that any operand for the -u option can't start with a
minus sign.
────────────────────────────────────────────────────────────────────────*/
/*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. */
errors=0
 
do while opts\==''
parse var opts x opts
 
select
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
!.wrange_1 '-' !.wrange_2 opts
when x=='-z' then !.=1
otherwise saycall sayer 'option ' x " isn't a legalknown option."
errors=errors+1
 
end /*select*/
end /*do while opts\=='' */
 
/*check for conflicts here and/or validity of values.*/
 
if !.z_==1 & !.n_==1 then docall sayer "N and Z can't both be specified."
say "error, N and Z can't both be specified."
errors=errors+1
end
 
if !.wrange_1\=='' then do /*testsee if it's a whole number (integer). */
if \datatypeisInt(!.wrange1_,'W') then saycall sayer "wRange isn't an integer..."
yada yada yada
.
.
.
end
 
/*────────────────────────────────────────────────────────────────────────
nc -u xxx -p port1 ...stuff...
Note: a programming trick is to append (say) an underscore to an
...more andstuff...
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.
/*──────────────────────────────────────────────────────────────────────────────────────*/
────────────────────────────────────────────────────────────────────────*/</lang>
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>
╔═══════════════════════════════════════════════════════════════════════╗
Note: a programming trick is to append (say) an underscore [_] to an
an option's name as to not preclude that variable being used elsewhere
elsewhere in the REXX program. That way, the option J can be used, as well
be used, as well as the variable J in the program.
╚═══════════════════════════════════════════════════════════════════════╝
</pre>
 
=={{header|Ruby}}==