Different command-line argument parsing methods are used by different
programming languages
A programming language is a system of notation for writing computer program, computer programs. Most programming languages are text-based formal languages, but they may also be visual programming language, graphical. They are a kind of computer ...
to
parse
Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from Lati ...
command-line argument
A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
s.
Programming languages
C
C uses
argv
to process command-line arguments.
An example of
C argument parsing would be:
#include
int main (int argc, char *argv[])
C also has functions called getopt and getopt_long.
C#
class TestClass
Java
An example of
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
argument parsing would be:
public class Echo
Kotlin
Here are some possible ways to print arguments in
Kotlin:
fun main(args: Array) = println(args.joinToString())
fun main(args: Array) = println(args.contentToString())
fun main(args: Array)
Perl
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
uses
@ARGV
.
foreach $arg (@ARGV)GT
FT
or
foreach $argnum (0 .. $#ARGV)ST
AWK
AWK
AWK (''awk'') is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems.
The AWK l ...
uses
ARGV
also.
BEGIN
PHP
PHP
PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
uses
argc
as a count of arguments and
argv
as an
array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
containing the values of the arguments. 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)
function getArg(array $args, string $arg)
PHP can also use
getopt()
.
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.
Racket
Racket uses a
current-command-line-arguments
parameter, and provides a
racket/cmdline
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) for the eyes" (eyes char)">"-e" "--eyes") char "use 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:
$ racket /tmp/c -nfe 8
8-(
Rexx
Rexx
Rexx (Restructured Extended Executor) is a programming language that can be interpreted or compiled. It was developed at IBM by Mike Cowlishaw. It is a structured, high-level programming language designed for ease of learning and reading. ...
uses
arg
, e.g.:
do i=1 to words(arg(1))
say word(arg(1), i)
end
Rust
The args are in
env::args()
.
use std::env;
fn main()
Node.js
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
programs written for
Node.js
Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code ou ...
use the
process.argv
global variable.
// 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
Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code ou ...
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
.
// process-args.js
console.log(process.argv.slice(2));
$ node process-args.js one two=three four
'one',
'two=three',
'four'
References
{{Reflist
Command shells
Articles with example Java code
Articles with example PHP code
Articles with example Python (programming language) code
Articles with example Racket code