Parse command-line arguments: Difference between revisions

From Rosetta Code
Content added Content deleted
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

Parse command-line arguments is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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

  1. == Synopsis
  2. pargs: Phone a friend
  3. == Usage
  4. pargs [OPTIONS]
  5. --help, -h:
  6. show usage
  7. --eddy, -e <message>
  8. call eddy
  9. --danial, -d <message>
  10. call daniel
  11. --test, -t
  12. 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>