请输入您要查询的百科知识:

 

词条 Command-line argument parsing
释义

  1. Programming languages

     C  Java  Perl  AWK  PHP  Python  Racket 

  2. References

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

An example of C argument parsing would be:

  1. include

int main (int argc, char *argv[])

{
    int count;    for (count=0; count

}

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

}

Perl

Perl uses $ARGV.

foreach $arg (@ARGV)

{

}

or

foreach $argnum (0 .. $#ARGV)

{

}

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.[3][4] 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($args) {

    foreach($args as $arg) {        $tmp = explode(':', $arg, 2);        if ($arg[0] === '-') {            $args[substr($tmp[0], 1)] = $tmp[1];        }    }    return $args;

}

function getArg($args, $arg) {

    if (isset($args[$arg])) {        return $args[$arg];    }    return false;

}

PHP can also use getopt().[5]

Python

Python uses sys.argv, e.g.:

import sys

for arg in sys.argv:

Python also has a module called argparse in the standard library for parsing command-line arguments.[6]

Racket

Racket uses a current-command-line-arguments parameter, and provides a racket/cmdline[7] library for parsing these arguments. Example:

  1. 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  for the eyes" (eyes char)])

(printf "~a~a~a\"

        (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-(

References

1. ^{{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}}
2. ^An example of parsing C arguments and options
3. ^{{cite web|url=http://php.net/manual/en/reserved.variables.argv.php |title=PHP Manual |publisher=PHP |date= |accessdate=2010-05-31}}
4. ^PHP Programming/CLI
5. ^https://php.net/getopt
6. ^{{cite web|title=argparse — Parser for command-line options, arguments and sub-commands|url=https://docs.python.org/library/argparse.html|work=Python v2.7.2 documentation|accessdate=7 March 2012}}
7. ^The Racket reference manual, Command-Line Parsing

5 : Command shells|Articles with example Java code|Articles with example PHP code|Articles with example Python code|Articles with example Racket code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/14 13:41:36