Command-line argument parsing

Command-line argument parsing

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

Contents

Programming languages

C

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

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.

PHP

PHP uses argc as a count of arguments and argv as an array containing the values of the arguments.[2][3] 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()[4]

Python

Python uses sys.argv, e.g.:

import sys
for arg in sys.argv:
  print arg

Racket

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

#lang racket
(require racket/cmdline)
(define smile? #true)
(define nose?  #false)
(define eyes   ":")
(command-line
 #:program "emoticon"
 #:once-any ; the following two are mutually exclusive
 [("-s" "--smile") "smile mode" (set! smile? #true)]
 [("-f" "--frown") "frown mode" (set! smile? #false)]
 #:once-each
 [("-n" "--nose") "add a nose"  (set! nose? #true)]
 [("-e" "--eyes") char "use <char> for the eyes" (set! 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-(

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Command-line interface — Screenshot of a sample Bash session. GNOME Terminal 3, Fedora 15 …   Wikipedia

  • Command responsibility — Peace Palace in The Hague Medina standard redirects here. For Medina charter, see Constitution of Medina. Command responsibility, sometimes referred to as the Yamashita standard or the Medina standard, and also known as superior responsibility,… …   Wikipedia

  • NDoc — 1.3.1 displaying empty project Developer(s) Downs, Kackman, et al. Stable release 1.3.1 / 25 January 2005 …   Wikipedia

  • MUMPS — This article is about the programming language. For other uses, see Mumps (disambiguation). MUMPS Paradigm(s) Procedural Appeared in 1966 Designed by Neil Pappalardo Stable release ANSI X11.1 1995 (December 8, 1995; 15 years ago …   Wikipedia

  • Windows PowerShell — Screenshot of a sample PowerShell session …   Wikipedia

  • Lisp (programming language) — Infobox programming language name = Lisp paradigm = multi paradigm: functional, procedural, reflective generation = 3GL year = 1958 designer = John McCarthy developer = Steve Russell, Timothy P. Hart, and Mike Levin latest release version =… …   Wikipedia

  • PHP — This article is about the scripting language. For other uses, see PHP (disambiguation). PHP PHP: Hypertext Preprocessor Paradigm(s) imperative, object oriented, Procedural, reflective Appeared in …   Wikipedia

  • Perl — This article is about the programming language. For other uses, see Perl (disambiguation). Perl Paradig …   Wikipedia

  • Make (software) — make Original author(s) Stuart Feldman Initial release 1977 Type build automation tool In software development, Make is a utility that automatically builds executable programs and libraries from source code by rea …   Wikipedia

  • MUF (programming language) — MUF (short for Multi User Forth ) is a Forth based programming language used on TinyMUCK MUCK servers and their descendants, including Fuzzball MUCK, ProtoMUCK and GlowMUCK. MUF is the system programming language for TinyMUCK systems. Many… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”