getopt

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

getoptGets options from the command line argument list

Beschreibung

getopt(string $short_options, array $long_options = [], int &$rest_index = null): array|false

Parses options passed to the script.

Parameter-Liste

short_options
Each character in this string will be used as option characters and matched against options passed to the script starting with a single hyphen (-). For example, an option string "x" recognizes an option -x. Only a-z, A-Z and 0-9 are allowed.
long_options
An array of options. Each element in this array will be used as option strings and matched against options passed to the script starting with two hyphens (--). For example, an longopts element "opt" recognizes an option --opt.
rest_index
If the rest_index parameter is present, then the index where argument parsing stopped will be written to this variable.

The short_options parameter may contain the following elements:

  • Individual characters (do not accept values)
  • Characters followed by a colon (parameter requires value)
  • Characters followed by two colons (optional value)
Option values are the first argument after the string. If a value is required, it does not matter whether the value has leading white space or not. See note.

Hinweis: Optional values do not accept " " (space) as a separator.

The long_options array values may contain:

  • String (parameter does not accept any value)
  • String followed by a colon (parameter requires value)
  • String followed by two colons (optional value)

Hinweis:

The format for the short_options and long_options is almost the same, the only difference is that long_options takes an array of options (where each element is the option) whereas short_options takes a string (where each character is the option).

Rückgabewerte

This function will return an array of option / argument pairs, Bei einem Fehler wird false zurückgegeben..

Hinweis:

The parsing of options will end at the first non-option found, anything that follows is discarded.

Changelog

Version Beschreibung
7.1.0 Added the rest_index parameter.

Beispiele

Beispiel #1 getopt() example: The basics

<?php
// Script example.php
$options getopt("f:hp:");
var_dump($options);
?>
shell> php example.php -fvalue -h

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

array(2) {
  ["f"]=>
  string(5) "value"
  ["h"]=>
  bool(false)
}

Beispiel #2 getopt() example: Introducing long options

<?php
// Script example.php
$shortopts  "";
$shortopts .= "f:";  // Required value
$shortopts .= "v::"// Optional value
$shortopts .= "abc"// These options do not accept values

$longopts  = array(
    
"required:",     // Required value
    
"optional::",    // Optional value
    
"option",        // No value
    
"opt",           // No value
);
$options getopt($shortopts$longopts);
var_dump($options);
?>
shell> php example.php -f "value for f" -v -a --required value --optional="optional value" --option

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

array(6) {
  ["f"]=>
  string(11) "value for f"
  ["v"]=>
  bool(false)
  ["a"]=>
  bool(false)
  ["required"]=>
  string(5) "value"
  ["optional"]=>
  string(14) "optional value"
  ["option"]=>
  bool(false)
}

Beispiel #3 getopt() example: Passing multiple options as one

<?php
// Script example.php
$options getopt("abc");
var_dump($options);
?>
shell> php example.php -aaac

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

array(2) {
  ["a"]=>
  array(3) {
    [0]=>
    bool(false)
    [1]=>
    bool(false)
    [2]=>
    bool(false)
  }
  ["c"]=>
  bool(false)
}

Beispiel #4 getopt() example: Using rest_index

<?php
// Script example.php
$rest_index null;
$opts getopt('a:b:', [], $rest_index);
$pos_args array_slice($argv$rest_index);
var_dump($pos_args);
shell> php example.php -a 1 -b 2 -- test

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

array(1) {
  [0]=>
  string(4) "test"
}

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Wie kann man komplexe Abfragen mit SQL-Querys In MySQLi effektiv durchführen?

In diesem MySQL-Tutorial wird erklärt, wie komplexe SQL-Abfragen in MySQLi effizient durchgeführt werden können. Wir werden uns mit verschiedenen Aspekten der Datenbankabfrage beschäftigen und spezifische Methoden kennenlernen. ...

TheMax

Autor : TheMax
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum
PHP 8.3.3 - App kann auf dem PC nicht ausgeführt werden

Problem gelöst. Die php.exe hatte einen defekt. Neue heruntergeladen und fertig.

Geschrieben von Tetra1081 am 10.04.2024 16:49:14
Forum: PHP Developer Forum