Content deleted Content added
→Perl: DT Tags: Mobile edit Mobile web edit |
m change source to syntaxhighlight |
||
Line 7:
An example of [[C (programming language)|C]] argument parsing would be:
<
#include <stdio.h>
int main (int argc, char *argv[])
Line 15:
puts (argv[count]);
}
</syntaxhighlight>
===Java===
An example of [[Java (programming language)|Java]] argument parsing would be:
<
public class Echo {
public static void main (String[] args) {
Line 27:
}
}
</syntaxhighlight>
===Perl===
[[Perl]] uses <code>@ARGV</code>.
<
foreach $arg (@ARGV)GT
{
print $arg;
}
</
or
<
foreach $argnum (0 .. $#ARGV)ST
{
print $ARGV[$argnum];
}
</syntaxhighlight>
===AWK===
[[AWK]] uses <code>ARGV</code> also.
<
BEGIN {
for ( i = 0; i < ARGC; i++ )
Line 56:
}
}
</syntaxhighlight>
===PHP===
[[PHP]] uses <code>argc</code> as a count of arguments and <code>argv</code> as an [[array data structure|array]] containing the values of the arguments.<ref>{{cite web|url=http://php.net/manual/en/reserved.variables.argv.php |title=PHP Manual |publisher=PHP |date= |accessdate=2010-05-31}}</ref><ref>[[wikibooks:PHP Programming/CLI]]</ref> To create an array from command-line arguments in the <code>-foo:bar</code> format, the following might be used:
<
$args = parseArgs($argv);
echo getArg($args, 'foo');
Line 83:
return false;
}
</syntaxhighlight>
PHP can also use <code>getopt()</code>.<ref>https://php.net/getopt</ref>
Line 89:
===Python===
[[Python (programming language)|Python]] uses <code>sys.argv</code>, e.g.:
<
import sys
for arg in sys.argv:
print arg
</syntaxhighlight>
Python also has a module called <code>argparse</code> in the standard library for parsing command-line arguments.<ref>{{cite web|title=argparse — Parser for command-line options, arguments and sub-commands|url=https://docs.python.org/library/argparse.html|work=Python v2.7.2 documentation|accessdate=7 March 2012}}</ref>
Line 99:
===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:
<
#lang racket
Line 122:
(if (nose?) "-" "")
(if (smile?) ")" "("))
</syntaxhighlight>
The library parses long and short flags, handles arguments, allows combining short flags, and handles <code>-h</code> and <code>--help</code> automatically:
<
$ racket /tmp/c -nfe 8
8-(
</syntaxhighlight>
===Node.js===
[[JavaScript]] programs written for [[Node.js]] use the <code>process.argv</code> global variable.<ref>{{cite web|title=process.argv|work=Node.js v10.16.3 Documentation|url=https://nodejs.org/docs/latest-v10.x/api/process.html#process_process_argv|accessdate=3 October 2019}}</ref>
<
// argv.js
console.log(process.argv);
</syntaxhighlight>
<
$ node argv.js one two three four five
[ 'node',
Line 146:
'four',
'five' ]
</syntaxhighlight>
[[Node.js]] programs are invoked by running the interpreter node interpreter with a given file, so the first two arguments will be <code>node</code> and the name of the JavaScript source file. It is often useful to extract the rest of the arguments by slicing a sub-array from <code>process.argv</code>.<ref>{{cite web|title=How to parse command line arguments|work=Node.js Foundation Documentation|accessdate=3 October 2019|url=https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/}}</ref>
<
// process-args.js
console.log(process.argv.slice(2));
</syntaxhighlight>
<
$ node process-args.js one two=three four
[
Line 161:
'two=three',
'four' ]
</syntaxhighlight>
==References==
|