Arguments parser

Provides direct access to individual command-line parameters in PHP CLI applications.

In PHP-Anwendungen für die Kommandozeile (Command Line Interface, CLI) ist es möglich, Befehlszeilenargumente wie für jedes andere Programm zu übergeben. Diese werden von PHP aber nicht ausgewertet, im Gegensatz zu den URL-Parametern auf Webseiten. Die hier vorgestellten Funktionen ermöglichen es, gezielt auf einzelne Parameter zuzugreifen und damit verbundene Werte abzufragen.

Compatibility: PHP Version 4.0 or newer

Hinweis: Damit das ganze funktioniert, muss die Option register_argc_argv in der PHP-Konfiguration aktiviert sein.

Kommandozeilenparameter in C#? Weiter zur CommandLineParser-Klasse…

Beispiel

Das PHP-Programm kann z. B. mit diesen Parametern aufgerufen werden:

./demoscript -c -i 12.34.56.78 --cert mydomain.crt mydomain.de

Der folgende Beispielcode zeigt die Verwendung im Programm:

if ($argc < 1)
{
    echo "Your syntax description here.\n";
    echo "\n";
    echo "Usage:\n";
    echo "  demoscript [options] <domain> [<path>]\n";
    echo "\n";
    echo "Options:\n";
    echo "  -c, --cgi          Enable CGI execution on this domain\n";
    echo "  -s, --ssl          Enable SSL access to this domain\n";
    echo "  -i, --ip <num>     Bind this domain to a specific IP address\n";
    echo "      --cert <file>  Custom SSL certificate file suffix\n";
    echo "\n";
    echo "<domain> is the full domain name\n";
    echo "<path> is the document root subdirectory for this domain\n";
    exit();
}

$known_opts = array('c', 's', 'i=');
$known_longopts = array('cgi', 'ssl', 'ip=', 'cert=');

require('arguments.inc.php');

$cgi = is_option_set('c', 'cgi');
$ssl = is_option_set('s', 'ssl');
$ip = get_option_value('i', 'ip') or $ip = 0;
$certfile = get_option_value('cert');
$domain = get_argument(1);
$path = get_argument(2);

if ($domain == '') die("No domain name specified.\n");

Download

arguments.inc.php4.8 KiBQuelltext

Licence and terms of use

This software is released under the terms of the simplified BSD licence. You can find the detailed terms and conditions in the download.

Statistic data

  • Created on 2007-05-16.