Parse command-line arguments: Difference between revisions

From Rosetta Code
Content added Content deleted
(icon)
(Added PicoLisp)
Line 23: Line 23:
{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/options.icn options.icn supports getting command-line options]
[http://www.cs.arizona.edu/icon/library/src/procs/options.icn options.icn supports getting command-line options]

=={{header|PicoLisp}}==
PicoLisp doesn't have a library to get options. Instead, the command line is parsed at startup and handled in the following way: Each command line argument is executed (interpreted) as a Lisp source file, except that if the first character is a hypen '-', then that arguments is taken as a Lisp function call (without the surrounding parentheses). For example, the command line
<lang shell>$ ./pil abc.l -foo def.l -"bar 3 4" -'mumble "hello"' -bye</lang>
has the effect that
# The file "abc.l" is executed
# (foo) is called
# The file "def.l" is executed
# (bar 3 4) is called
# (mumble "hello") is called
# (bye) is called, resulting in program termination
Command line arguments like "-v", "-n" and "-z" can be implemented simply by defining three functions 'v', 'n' and 'z'.

In addition to the above mechanism, the command line can also be handled "manually", by either processing the list of arguments returned by '[http://software-lab.de/doc/refA.html#argv argv]', or by fetching arguments individually with '[http://software-lab.de/doc/refO.html#opt opt]'.


=={{header|Ruby}}==
=={{header|Ruby}}==

Revision as of 14:28, 11 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

PicoLisp

PicoLisp doesn't have a library to get options. Instead, the command line is parsed at startup and handled in the following way: Each command line argument is executed (interpreted) as a Lisp source file, except that if the first character is a hypen '-', then that arguments is taken as a Lisp function call (without the surrounding parentheses). For example, the command line <lang shell>$ ./pil abc.l -foo def.l -"bar 3 4" -'mumble "hello"' -bye</lang> has the effect that

  1. The file "abc.l" is executed
  2. (foo) is called
  3. The file "def.l" is executed
  4. (bar 3 4) is called
  5. (mumble "hello") is called
  6. (bye) is called, resulting in program termination

Command line arguments like "-v", "-n" and "-z" can be implemented simply by defining three functions 'v', 'n' and 'z'.

In addition to the above mechanism, the command line can also be handled "manually", by either processing the list of arguments returned by 'argv', or by fetching arguments individually with 'opt'.

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>