To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Command-line argument parsing

From Wikipedia, the free encyclopedia

Different command-line argument parsing methods are used by different programming languages to parse command-line arguments.

YouTube Encyclopedic

  • 1/3
    Views:
    9 433
    46 780
    20 953
  • Python Tutorial - 25. Command line argument processing using argparse
  • Command Line Arguments in C | bestdotnettraining.com
  • Beginning C Programming - Part 51 - Commandline arguments

Transcription

Programming languages

C

C uses argv to process command-line arguments.[1][2]

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.

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:[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

Perl uses @ARGV.

foreach $arg (@ARGV)GT
{
    print $arg;
}

FT

or

foreach $argnum (0 .. $#ARGV)ST
{
    print $ARGV[$argnum];
}

AWK

AWK uses ARGV also.

BEGIN {
   for ( i = 0; i < ARGC; i++ )
   {
       print ARGV[i]
   }
}

PHP

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:

$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]

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.[7]

Racket

Racket uses a current-command-line-arguments parameter, and provides a racket/cmdline[8] 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 <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:

$ racket /tmp/c -nfe 8
8-(

Rexx

Rexx uses arg, e.g.:

do i=1 to words(arg(1))
	say word(arg(1), i)
end

Rust

The args are in env::args().[9]

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);
}

Node.js

JavaScript programs written for Node.js use the process.argv global variable.[10]

// 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]

// process-args.js
console.log(process.argv.slice(2));
$ node process-args.js one two=three four
[ 
  'one',
  'two=three',
  'four' ]

References

  1. ^ "The C Book — Arguments to main". Publications.gbdirect.co.uk. Retrieved 2010-05-31.
  2. ^ An example of parsing C arguments and options
  3. ^ "Kotlin: Basic syntax". Retrieved 2022-05-13.
  4. ^ "PHP Manual". PHP. Retrieved 2010-05-31.
  5. ^ wikibooks:PHP Programming/CLI
  6. ^ "PHP: Getopt - Manual".
  7. ^ "argparse — Parser for command-line options, arguments and sub-commands". Python v3.10.0 documentation. Archived from the original on 2012-11-01. Retrieved 15 October 2021.
  8. ^ The Racket reference manual, Command-Line Parsing
  9. ^ "Accepting Command Line Arguments - The Rust Programming Language". doc.rust-lang.org. Retrieved 22 December 2022.
  10. ^ "process.argv". Node.js v10.16.3 Documentation. Retrieved 3 October 2019.
  11. ^ "How to parse command line arguments". Node.js Foundation Documentation. Retrieved 3 October 2019.
This page was last edited on 14 March 2024, at 11:03
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.