Command-line argument parsing
{{short description|Programming languages parsing of command-line arguments}}
Different command-line argument parsing methods are used by different programming languages to parse command-line arguments.
Programming languages
=C=
{{Main|C_syntax#Command-line_arguments}}
C uses argv
to process command-line arguments.{{cite web|url=http://publications.gbdirect.co.uk/c_book/chapter10/arguments_to_main.html |title=The C Book — Arguments to main |publisher=Publications.gbdirect.co.uk |date= |accessdate=2010-05-31}}[http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/share/misc/style An example of parsing C arguments and options]
An example of C argument parsing would be:
- include
int main (int argc, char *argv[])
{
int count;
for (count = 0; count < argc; count++)
puts (argv[count]);
}
C also has functions called getopt and getopt_long.
=C#=
An example of C# argument parsing would be:
class Program
{
static void Main(string[] args)
{
foreach (var arg in args)
Console.WriteLine(arg);
}
}
=Java=
An example of Java argument parsing would be:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
=Kotlin=
Here are some possible ways to print arguments in Kotlin:{{cite web|url=https://kotlinlang.org/docs/basic-syntax.html#program-entry-point |title=Kotlin: Basic syntax |date= |accessdate=2022-05-13}}
fun main(args: Array
fun main(args: Array
fun main(args: Array
for (arg in args)
println(arg)
}
=Perl=
Perl uses @ARGV
.
foreach $arg (@ARGV)
{
print $arg;
}
or
foreach $argnum (0 .. $#ARGV)
{
print $ARGV[$argnum];
}
=AWK=
=PHP=
PHP uses argc
as a count of arguments and argv
as an array containing the values of the arguments.{{cite web|url=http://php.net/manual/en/reserved.variables.argv.php |title=PHP Manual |publisher=PHP |date= |accessdate=2010-05-31}}wikibooks:PHP Programming/CLI To create an array from command-line arguments in the -foo:bar
format, the following might be used:
$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];
}
}
return $args;
}
function getArg(array $args, string $arg)
{
if (isset($args[$arg])) {
return $args[$arg];
}
return false;
}
PHP can also use getopt()
.{{Cite web|url=https://php.net/getopt|title = PHP: Getopt - Manual}}
=Python=
Python uses sys.argv
, e.g.:
import sys
for arg in sys.argv:
print arg
Python also has a module called argparse
in the standard library for parsing command-line arguments.{{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 v3.10.0 documentation|accessdate=15 October 2021|archive-url=https://web.archive.org/web/20121101061815/http://docs.python.org:80/3/library/argparse.html |archive-date=2012-11-01 }}
=Racket=
Racket uses a current-command-line-arguments
parameter, and provides a racket/cmdline
[http://docs.racket-lang.org/reference/Command-Line_Parsing.html The Racket reference manual, Command-Line Parsing] library for parsing these arguments. Example:
- lang racket
(require racket/cmdline)
(define smile? (make-parameter #t))
(define nose? (make-parameter #false))
(define eyes (make-parameter ":"))
(command-line #:program "emoticon"
#:once-any ; the following two are mutually exclusive
[("-s" "--smile") "smile mode" (smile? #true)]
[("-f" "--frown") "frown mode" (smile? #false)]
#:once-each
[("-n" "--nose") "add a nose" (nose? #true)]
[("-e" "--eyes") char "use
(printf "~a~a~a\n"
(eyes)
(if (nose?) "-" "")
(if (smile?) ")" "("))
The library parses long and short flags, handles arguments, allows combining short flags, and handles -h
and --help
automatically:
$ racket /tmp/c -nfe 8
8-(
=Rexx=
=Rust=
The args are in env::args()
.{{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}}
use std::env;
fn main() {
let args: Vec
let query = &args[1];
let file_path = &args[2];
println!("Searching for {}", query);
println!("In file {}", file_path);
}
=JavaScript=
==Node.js==
JavaScript programs written for Node.js use the process.argv
global variable.{{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}}
// argv.js
console.log(process.argv);
$ node argv.js one two three four five
[ 'node',
'/home/avian/argvdemo/argv.js',
'one',
'two',
'three',
'four',
'five' ]
Node.js programs are invoked by running the interpreter node interpreter with a given file, so the first two arguments will be node
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 process.argv
.{{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/}}
// process-args.js
console.log(process.argv.slice(2));
$ node process-args.js one two=three four
[
'one',
'two=three',
'four' ]
==Bun==
JavaScript written for Bun use Bun.argv
and the util.parseArgs
function.{{cite web |title=Parse command-line arguments {{!}} Bun Examples |url=https://bun.sh/guides/process/argv |website=Bun |language=en}}
console.log(Bun.argv);
==Deno==
JavaScript written for Deno use Deno.args
{{cite web |title=Deno.args |url=https://docs.deno.com/api/deno/~/Deno.args |website=docs.deno.com}} and the parseArgs
function.{{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}}
console.log(Deno.args);