简体   繁体   English

在命令行上传递多部分变量

[英]Passing Multi-Part Variables on the Command Line

I have a php file I'm running from the command line. 我有一个从命令行运行的php文件。 This script has one required unnamed element (file.xml), but allows certain options (such as --conf LocalSettings.php). 该脚本具有一个必需的未命名元素(file.xml),但允许某些选项(例如--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). 我的问题是我的选项之一需要传递一个设置(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. 我唯一可以修改的是LocalSettings.php脚本以及如何在命令行上调用它们。

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)? 我如何才能使该命令在需要未命名部分的命令(importDump.php)和不需要此命令的部分(runJobs.php)上运行?

For reference, I get the settings input in the LocalSettings.php script using $argv[3] . 作为参考,我使用$argv[3]在LocalSettings.php脚本中获取设置输入。

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) . arg_test.php只是执行<?php var_dump($argv)

However, consider using Symfony Console or some other library that makes CLI usage a bit easier. 但是,请考虑使用Symfony Console或其他一些可以简化CLI使用的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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