Parse command-line arguments: Difference between revisions
m (moved Parsing command-line arguments to Parse command-line arguments: Remove extra -ing, see Rosetta Code:Village Pump/Renaming to rename tasks) |
(J: something of a joke, given lack of any specified task... or maybe just really concise?) |
||
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|J}}== |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
Revision as of 22:28, 12 September 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
J
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 '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
- == 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>