简体   繁体   中英

Passing Multi-Part Variables on the Command Line

I have a php file I'm running from the command line. This script has one required unnamed element (file.xml), but allows certain options (such as --conf LocalSettings.php).

php importDump.php --conf LocalSettings.php file.xml

My problem is one of my options requires a setting to be passed (setting1). This works for most scripts, like this one:

php runJobs.php --conf LocalSettings.php setting1

But not ones that have a required unnamed element, like:

php importDump.php --conf LocalSettings.php setting1 file.xml

I tried putting parts in quotes, so it gets treated like a block of code, but it won't work.

php importDump.php --conf "LocalSettings.php setting1" file.xml

The only things I can modify is the LocalSettings.php script and how I call things on the command line.

How can I get this command to work on both commands that have required unnamed parts (importDump.php) and those that don't (runJobs.php)?

For reference, I get the settings input in the LocalSettings.php script using $argv[3] .

Not sure why putting in quotes doesn't work for you.

$ php arg_test.php --conf "test foo" file.xml
array(4) {
  [0]=>
  string(12) "arg_test.php"
  [1]=>
  string(6) "--conf"
  [2]=>
  string(8) "test foo"
  [3]=>
  string(8) "file.xml"
}

The arg_test.php simply does <?php var_dump($argv) .

However, consider using Symfony Console or some other library that makes CLI usage a bit easier.

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