PHP: command line arguments snippet

PHOTO EMBED

Tue Jun 23 2020 08:23:07 GMT+0000 (Coordinated Universal Time)

Saved by @samiharoon

<?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
content_copyCOPY

https://www.php.net/manual/en/function.getopt.php