Command-line argument parsing: Difference between revisions

Content deleted Content added
m PHP: PHP-FIG PSR-2
m remove extraneous characters from perl code sample
 
(24 intermediate revisions by 16 users not shown)
Line 1:
{{short description|Programming languages parsing of command-line arguments}}
Different '''Commandcommand-line argument parsing''' methods are used by different [[programming languages]] to [[parse]] [[command-line argument]]s.
 
==Programming languages==
Line 7 ⟶ 8:
 
An example of [[C (programming language)|C]] argument parsing would be:
<sourcesyntaxhighlight lang="Cc">
#include <stdio.h>
int main (int argc, char *argv[])
{
int count;
for (count = 0; count < argc; count++)
puts (argv[count]);
}
</syntaxhighlight>
</source>
C also has functions called [[getopt]] and getopt_long.
 
===C#===
An example of [[C Sharp (programming language)|C#]] argument parsing would be:
<syntaxhighlight lang="csharp">
class Program
{
static void Main(string[] args)
{
foreach (var arg in args)
Console.WriteLine(arg);
}
}
</syntaxhighlight>
 
===Java===
An example of [[Java (programming language)|Java]] argument parsing would be:
<sourcesyntaxhighlight lang="java">
public class Echo {
public static void main (String[] args) {
Line 27 ⟶ 42:
}
}
</syntaxhighlight>
</source>
 
===Kotlin===
Here are some possible ways to print arguments in [[Kotlin (programming language)|Kotlin]]:<ref>{{cite web|url=https://kotlinlang.org/docs/basic-syntax.html#program-entry-point |title=Kotlin: Basic syntax |date= |accessdate=2022-05-13}}</ref>
 
<syntaxhighlight lang="kotlin">
fun main(args: Array<String>) = println(args.joinToString())
</syntaxhighlight>
 
<syntaxhighlight lang="kotlin">
fun main(args: Array<String>) = println(args.contentToString())
</syntaxhighlight>
 
<syntaxhighlight lang="kotlin">
fun main(args: Array<String>) {
for (arg in args)
println(arg)
}
</syntaxhighlight>
 
===Perl===
[[Perl]] uses <code>$@ARGV</code>.
 
<sourcesyntaxhighlight lang="perl">
foreach $arg (@ARGV)
{
print $arg;
}
</syntaxhighlight>
</source>
or
<sourcesyntaxhighlight lang="perl">
foreach $argnum (0 .. $#ARGV)
{
print $ARGV[$argnum];
}
</syntaxhighlight>
</source>
 
===AWK===
[[AWK]] uses <code>ARGV</code> also.
 
<sourcesyntaxhighlight lang="awk">
BEGIN {
for ( i = 0; i < ARGC; i++ )
Line 56 ⟶ 89:
}
}
</syntaxhighlight>
</source>
 
===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:
 
<sourcesyntaxhighlight lang="php">
$args = parseArgs($argv);
echo getArg($args, '"foo'");
 
function parseArgs(array $args)
{
foreach ($args as $arg) {
$tmp = explode('":'", $arg, 2);
if ($arg[0] === '"-'") {
$args[substr($tmp[0], 1)] = $tmp[1];
}
Line 83 ⟶ 116:
return false;
}
</syntaxhighlight>
</source>
 
PHP can also use <code>getopt()</code>.<ref>{{Cite web|url=https://php.net/getopt|title = PHP: Getopt - Manual}}</ref>
 
===Python===
[[Python (programming language)|Python]] uses <code>sys.argv</code>, e.g.:
<sourcesyntaxhighlight lang="python">
import sys
for arg in sys.argv:
print arg
</syntaxhighlight>
</source>
 
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/3/library/argparse.html|url-status=live|work=Python v2v3.710.20 documentation|accessdate=715 MarchOctober 2021|archive-url=https://web.archive.org/web/20121101061815/http://docs.python.org:80/3/library/argparse.html |archive-date=2012-11-01 }}</ref>
 
===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:
<sourcesyntaxhighlight lang="racket">
#lang racket
 
Line 122 ⟶ 155:
(if (nose?) "-" "")
(if (smile?) ")" "("))
</syntaxhighlight>
</source>
The library parses long and short flags, handles arguments, allows combining short flags, and handles <code>-h</code> and <code>--help</code> automatically:
<sourcesyntaxhighlight lang="console">
$ racket /tmp/c -nfe 8
8-(
</syntaxhighlight>
</source>
 
===Node.jsRexx===
[[Rexx]] uses <code>arg</code>, e.g.:
<syntaxhighlight lang="rexx">
do i=1 to words(arg(1))
say word(arg(1), i)
end
</syntaxhighlight>
 
===Rust===
The args are in <code>env::args()</code>.<ref>{{cite web |title=Accepting Command Line Arguments - The Rust Programming Language |url=https://doc.rust-lang.org/book/ch12-01-accepting-command-line-arguments.html |website=doc.rust-lang.org |access-date=22 December 2022}}</ref>
<syntaxhighlight lang="rust">
use std::env;
 
fn main() {
let args: Vec<String> = env::args().collect();
 
let query = &args[1];
let file_path = &args[2];
 
println!("Searching for {}", query);
println!("In file {}", file_path);
}
</syntaxhighlight>
 
===JavaScript===
====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>
 
<sourcesyntaxhighlight lang="javascript">
// argv.js
console.log(process.argv);
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="bash">
$ node argv.js one two three four five
[ 'node',
Line 146 ⟶ 204:
'four',
'five' ]
</syntaxhighlight>
</source>
 
[[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>
 
<sourcesyntaxhighlight lang="javascript">
// process-args.js
console.log(process.argv.slice(2));
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="bash">
$ node process-args.js one two=three four
[
Line 161 ⟶ 219:
'two=three',
'four' ]
</syntaxhighlight>
</source>
 
====Bun====
JavaScript written for [[Bun (software)|Bun]] use <code>Bun.argv</code> and the <code>util.parseArgs</code> function.<ref>{{cite web |title=Parse command-line arguments {{!}} Bun Examples |url=https://bun.sh/guides/process/argv |website=Bun |language=en}}</ref>
 
<syntaxhighlight lang="javascript">
console.log(Bun.argv);
</syntaxhighlight>
 
====Deno====
JavaScript written for [[Deno (software)|Deno]] use <code>Deno.args</code><ref>{{cite web |title=Deno.args |url=https://docs.deno.com/api/deno/~/Deno.args |website=docs.deno.com}}</ref> and the <code>parseArgs</code> function.<ref>{{cite web |title=parseArgs from parse-args - @std/cli - JSR |url=https://jsr.io/@std/cli/doc/parse-args/~/parseArgs |website=jsr.io |language=en}}</ref>
 
<syntaxhighlight lang="javascript">
console.log(Deno.args);
</syntaxhighlight>
 
==References==
Line 169 ⟶ 241:
[[Category:Articles with example Java code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Racket code]]