Content deleted Content added
→Python: Added "import sys" to python example |
Elibarzilay (talk | contribs) Racket command-line example |
||
Line 56:
print arg
</source>
===Racket===
[[Racket (programming language)|Racket]] uses a <code>current-command-line-arguments</code> parameter, and provides a <code>racket/cmdline</code><ref>[http://docs.racket-lang.org/reference/Command-Line_Parsing.html The Racket reference manual, Command-Line Parsing]</ref> library for parsing these arguments. Example:
<source lang="scheme">
#lang racket
(require racket/cmdline)
(define smile? #true)
(define nose? #false)
(define eyes ":")
(command-line
#:program "emoticon"
#:once-any ; the following two are mutually exclusive
[("-s" "--smile") "smile mode" (set! smile? #true)]
[("-f" "--frown") "frown mode" (set! smile? #false)]
#:once-each
[("-n" "--nose") "add a nose" (set! nose? #true)]
[("-e" "--eyes") char "use <char> for the eyes" (set! eyes char)])
(printf "~a~a~a\n" eyes (if nose? "-" "") (if smile? ")" "("))
</source>
The library parses long and short flags, handles arguments, allows combining short flags, and handles <code>-h</code> and <code>--help</code> automatically:
<pre>
$ racket /tmp/c -nfe 8
8-(
</pre>
==References==
|