Parse command-line arguments: Difference between revisions
No edit summary |
(icon) |
||
Line 2: | Line 2: | ||
[[Command-line arguments]] can be quite complicated, as in "nc -v -n -z -w 1 192.168.1.2 1-1000". Many languages provide a library (getopt or GetOpt) to parse the raw command line options in an intelligent way. |
[[Command-line arguments]] can be quite complicated, as in "nc -v -n -z -w 1 192.168.1.2 1-1000". Many languages provide a library (getopt or GetOpt) to parse the raw command line options in an intelligent way. |
||
=={{header|Icon}} and {{header|Unicon}}== |
|||
The Icon Programming Library provides a procedure for processing command line options. See the library reference for detailed documentation. The code below is an example. |
|||
<lang Icon>link options |
|||
procedure main(ARGLIST) |
|||
/errproc := stop # special error procedure or stop() |
|||
opstring := "f!s:i+r.flag!string:integer+real." # example |
|||
opttable := options(ARGLIST,optstring,errproc) |
|||
if \opttable[flag] then ... # test a flag |
|||
r := opttable(real) # assign a real |
|||
r2 := opttable(r) # assign another real |
|||
s := opttable(s) # assign a string |
|||
i := opttable(i) # assign an integer |
|||
... |
|||
end</lang> |
|||
{{libheader|Icon Programming Library}} |
|||
[http://www.cs.arizona.edu/icon/library/src/procs/options.icn options.icn supports getting command-line options] |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
Revision as of 03:10, 9 August 2011
Command-line arguments can be quite complicated, as in "nc -v -n -z -w 1 192.168.1.2 1-1000". Many languages provide a library (getopt or GetOpt) to parse the raw command line options in an intelligent way.
Icon and Unicon
The Icon Programming Library provides a procedure for processing command line options. See the library reference for detailed documentation. The code below is an example.
<lang Icon>link options
procedure main(ARGLIST) /errproc := stop # special error procedure or stop() opstring := "f!s:i+r.flag!string:integer+real." # example opttable := options(ARGLIST,optstring,errproc)
if \opttable[flag] then ... # test a flag r := opttable(real) # assign a real r2 := opttable(r) # assign another real s := opttable(s) # assign a string i := opttable(i) # assign an integer ... end</lang>
options.icn supports getting command-line options
Ruby
<lang shell>$ ./pargs.rb -h
Usage
pargs [OPTIONS]
--help, -h:
show usage
--eddy, -e <message>
call eddy
--daniel, -d <message>
call daniel
--test, -t
run unit tests
$ ./pargs.rb -e Yo! Calling eddy... Yo! $ ./pargs.rb --test Calling Barry... Hi! Calling Cindy... Hello!</lang>
<lang ruby>#!/usr/bin/env ruby
- == Synopsis
- pargs: Phone a friend
- == Usage
- pargs [OPTIONS]
- --help, -h:
- show usage
- --eddy, -e <message>
- call eddy
- --danial, -d <message>
- call daniel
- --test, -t
- run unit tests
require "getoptlong" require "rdoc/usage"
def phone(name, message) puts "Calling #{name}..." puts message end
def test phone("Barry", "Hi!") phone("Cindy", "Hello!") end
def main mode = :usage
name = "" message = ""
opts=GetoptLong.new( ["--help", "-h", GetoptLong::NO_ARGUMENT], ["--eddy", "-e", GetoptLong::REQUIRED_ARGUMENT], ["--daniel", "-d", GetoptLong::REQUIRED_ARGUMENT], ["--test", "-t", GetoptLong::NO_ARGUMENT] )
opts.each { |option, value| case option when "--help" RDoc::usage("Usage") when "--eddy" mode = :call name = "eddy" message = value when "--daniel" mode = :call name = "daniel" message = value when "--test" mode = :test end }
case mode when :usage RDoc::usage("Usage") when :call phone(name, message) when :test test end end
if __FILE__==$0 begin main rescue Interrupt => e nil end end</lang>