Different command-line argument parsing methods are used by different programming languages to parse command-line arguments.
C uses argv to process command-line arguments.[1][2]
argv
An example of C argument parsing would be:
#include <stdio.h> 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.
An example of C# argument parsing would be:
class Program { static void Main(string[] args) { foreach (var arg in args) Console.WriteLine(arg); } }
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); } } }
Here are some possible ways to print arguments in Kotlin:[3]
fun main(args: Array<String>) = println(args.joinToString())
fun main(args: Array<String>) = println(args.contentToString())
fun main(args: Array<String>) { for (arg in args) println(arg) }
Perl uses @ARGV.
@ARGV
foreach $arg (@ARGV) { print $arg; }
or
foreach $argnum (0 .. $#ARGV) { print $ARGV[$argnum]; }
AWK uses ARGV also.
ARGV
BEGIN { for ( i = 0; i < ARGC; i++ ) { print ARGV[i] } }
PHP uses argc as a count of arguments and argv as an array containing the values of the arguments.[4][5] To create an array from command-line arguments in the -foo:bar format, the following might be used:
argc
-foo:bar
$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().[6]
getopt()
Python uses sys.argv, e.g.:
sys.argv
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.[7]
argparse
Racket uses a current-command-line-arguments parameter, and provides a racket/cmdline[8] library for parsing these arguments. Example:
current-command-line-arguments
racket/cmdline
#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 <char> for the eyes" (eyes char)]) (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:
-h
--help
$ racket /tmp/c -nfe 8 8-(
Rexx uses arg, e.g.:
arg
do i=1 to words(arg(1)) say word(arg(1), i) end
The args are in env::args().[9]
env::args()
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); }
JavaScript programs written for Node.js use the process.argv global variable.[10]
process.argv
// 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.[11]
node
// process-args.js console.log(process.argv.slice(2));
$ node process-args.js one two=three four [ 'one', 'two=three', 'four' ]
JavaScript written for Bun use Bun.argv and the util.parseArgs function.[12]
Bun.argv
util.parseArgs
console.log(Bun.argv);
JavaScript written for Deno use Deno.args[13] and the parseArgs function.[14]
Deno.args
parseArgs
console.log(Deno.args);