简体   繁体   中英

Getting Arguments for PHP Script within CLI

I'm trying to pass arguments to a PHP script as I run it in CLI. I've been using the following code to do so...

// Set argument variables
// A is the filetype
// B is the filename and directory
$arguments = getopt("a:b:");

// If we're importing pies...
if($arguments['a'] == 'pies') {
    echo 'File Conversion and Import of PIES Data Beginning Now...' . "\n\n";
}

And the command would look something like this...:

php fileimport.php -a pies -b /filepath/filename.xml

I've seen arguments passed in other ways though (for other scripts) such as...

php fileimport --type=pies --path=filepath/filename.xml

Personally I prefer that method but I don't know how to code my PHP script to collect these arguments, let alone expect them. I'm sure it is ridiculously easy and I'm sorry if I've missed the obvious, but I'm hoping to get some direction here.

Thanks!

As described in the PHP documentation :

$shortopts  = "";

$longopts  = array(
    "type:",
    "path:",
);
// Get command line arguments
$options = getopt($shortopts, $longopts);
echo 'Input Options: ', PHP_EOL;
var_dump($options);

try this

<?php
    $a=$argv[1];
    $b=$argv[2];

?>

and your CLI Call should be

php fileimport.php TEST1 TEST2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM