php arguments from console
Managing command line arguments
Arguments are for those pieces of information that your program absolutely, positively requires to run.
A good user interface should have as few absolute requirements as possible. If your program requires 17 distinct pieces of information in order to run successfully, it doesn’t much matter how you get that information from the user, most people will give up and walk away before they successfully run the program. This applies whether the user interface is a command-line, a configuration file, or a GUI: if you make that many demands on your users, most of them will simply give up.
In short, try to minimize the amount of information that users are absolutely required to supply and use sensible defaults whenever possible. Of course, you also want to make your programs reasonably flexible. That’s what options are for.
Adding arguments with Console_CommandLine
To add arguments to your parser, just create the parser as explained in the previous section and use the Console_CommandLine::addArgument() method.
the arguments parameters: an array of various informations as explained below.
name | type | required | description | example |
---|---|---|---|---|
description | string | no, but recommended for the help message | a description for the argument | entry list of input files separated by spaces |
multiple | boolean | no, default to FALSE | tells the parser that the argument expects multiples values | TRUE |
optional | boolean | no, default to FALSE | tells the parser that the argument is optional | TRUE |
help_name | string | no, if not given it will default to the argument name | the name to display in the argument help line | files |
Adding commandline arguments
require_once ‘Console/CommandLine.php’ ;
$parser = new Console_CommandLine ();
Консольные команды на PHP
У многих, равно как и у меня, периодически возникает потребность в реализации каких-то небольших задач. Например распарсить сайт/API и сохранить данные в xml/json/csv, произвести какие-либо расчеты/пересчеты, перегнать данные из одного формата в другой, собрать статистику и т.д. и т.п. Замечу, что речь о задачах не связанных с текущими проектами.
Собирать тяжелый фреймворк ради удобных фич, лень, а реализовывать в рамках кода текущих проектов как-то не эстетично. Поэтому для экономии своего времени приходится создавать скрипт, копипастить в него куски кода из предыдущих наработок, подключать разнообразные библиотеки и запускать скрипт из консоли. При этом часто требуется некоторая интерактивность работы скрипта: обработка опций/аргументов, а то и диалоговое взаимодействие. Здесь главное чтобы не было настроения, которое хорошо описывается выражением «Аппетит приходит во время еды», тогда вообще не понятно к чему приведет работа над простой задачкой =)
В такие моменты я вспоминал удобную симфоническую консоль, к которой успел привыкнуть работая с проектами на
Symfony 2. Не в обиду другим консолям (zend, yii, django, ror etc), все хороши, просто так сложилось.
Когда в очередной раз потребовалось что-то распарсить, я опять вспомнил про консоль Symfony (Console Component) и тот факт, что это независимый компонент все больше подтолкнул меня к мысли использовать ее возможности.
С помощью Сomposer-а создаем новый проект:
У меня Composer установлен глобально, поэтому он всегда доступен. Если вы им еще не пользуетесь, то для проверки примера его необходимо установить.
После скачивания приложения и всех зависимостей переходим в созданную директорию:
Если видим справочную информацию и список доступных команд значит все ок. Выглядит это так:
Теперь создадим шаблон класса команды, которую мы планируем использовать для реализации задачи:
В появившемся диалоге указываем название будущего класса:
В ответ получим уведомление:
Собственно все, команда готова, она появилась в списке доступных команд:
Но пока она не делает того что нужно (здесь можно открыть созданный класс в IDE или любимом редакторе и написать код команды).
Так как для нашего примера необходимо получать внешний контент и нам нравится ООП, поставим еще одну библиотеку:
Buzz — легкий HTTP клиент на PHP5.3. Будем использовать его для выполнения запросов к сервису новостей.
Создадим отдельный класс — YandexRSSNewsParser, который будет предоставлять классу команде подготовленный контент:
И отредактируем класс команды, для вывода в консоль заголовков последних новостей, рубрики «Интернет»:
Теперь выполним подготовленную команду:
Результат:
Получился очень простой, а за счет symfony/console и composer-а гибкий и удобный инструмент для организации консольных команд на PHP.
PHP, pass parameters from command line to a PHP script
I want to pass parameters from PHP Command Line Interface, and then read in the values using PHP script, something like this:
I pass the variable from CLI like this:
The above works, I get Robby as the output.
But I want to do something like this:
So that the user is well informed to enter the correct parameters in the correct places. What is the appropriate way to parse these parameters?
8 Answers 8
Will give the following output
To do what you want, lets say
You would need to explode the argument on the equals sign:-
That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.
I use this fairly concise method:
Which would handle a call such as:
For commentary, this is doing the following:
While the answer is correct and you could do the parsing by hand, PHP also offers the getopt() function that might actually provide useful here.
There’s also object-oriented alternatives (written in PHP, available in a number of libraries) that might turn out to be what you need. Googling for «php getopt» will yield helpful results.
and in your PHP file
The getopt() function is probably the most correct answer in the case of the question. Especially since it was made platform independent with PHP 5.3. In the particular case of this question and parsing multiple parameters, one way to leverage this function would be as follows:
Explanation
I’m trying to run PHP from the command line under Windows XP.
That works, except for the fact that I am not able to provide parameters to my PHP script.
I want to call this as:
But I never get the script to accept my parameter.
The result I get from the above script:
PHP Notice: Undefined variable: param in C:\test.php on line 2
I am trying this using PHP 5.2.6. Is this a bug in PHP 5?
The parameter passing is handled in the online help:
This seemed to be working under PHP 4, but not under PHP 5.
Under PHP 4 I could use the same script that could run on the server without alteration on the command line. This is handy for local debugging, for example, saving the output in a file, to be studied.
11 Answers 11
Why do you have any expectation that param will be set to the value?
You’re responsible for parsing the command line in the fashion you desire, from the $argv array.
You can use the getopt() function.
But PHP still doesn’t parse those arguments. It just passes them to the script in the $argv array.
The only reason for the — is so that PHP can tell which arguments are meant for the PHP executable and which arguments are meant for your script.
Использование PHP в командной строке
Содержание
User Contributed Notes 35 notes
?>
It behaves exactly like you’d expect with cgi-php.
Even better, instead of putting that line in every file, take advantage of PHP’s auto_prepend_file directive. Put that line in its own file and set the auto_prepend_file directive in your cli-specific php.ini like so:
It will be automatically prepended to any PHP file run from the command line.
When you’re writing one line php scripts remember that ‘php://stdin’ is your friend. Here’s a simple program I use to format PHP code for inclusion on my blog:
Just a note for people trying to use interactive mode from the commandline.
The purpose of interactive mode is to parse code snippits without actually leaving php, and it works like this:
I noticed this somehow got ommited from the docs, hope it helps someone!
If your php script doesn’t run with shebang (#!/usr/bin/php),
and it issues the beautifull and informative error message:
«Command not found.» just dos2unix yourscript.php
et voila.
If your php script doesn’t run with shebang (#/usr/bin/php),
and it issues the beautifull and informative message:
«Invalid null command.» it’s probably because the «!» is missing in the the shebang line (like what’s above) or something else in that area.
Parsing commandline argument GET String without changing the PHP script (linux shell):
URL: index.php?a=1&b=2
Result: output.html
(no need to change php.ini)
Ok, I’ve had a heck of a time with PHP > 4.3.x and whether to use CLI vs CGI. The CGI version of 4.3.2 would return (in browser):
—
No input file specified.
—
And the CLI version would return:
—
500 Internal Server Error
—
It appears that in CGI mode, PHP looks at the environment variable PATH_TRANSLATED to determine the script to execute and ignores command line. That is why in the absensce of this environment variable, you get «No input file specified.» However, in CLI mode the HTTP headers are not printed. I believe this is intended behavior for both situations but creates a problem when you have a CGI wrapper that sends environment variables but passes the actual script name on the command line.
By modifying my CGI wrapper to create this PATH_TRANSLATED environment variable, it solved my problem, and I was able to run the CGI build of 4.3.2
If you want to be interactive with the user and accept user input, all you need to do is read from stdin.
Parsing command line: optimization is evil!
One thing all contributors on this page forgotten is that you can suround an argv with single or double quotes. So the join coupled together with the preg_match_all will always break that 🙂
Here is a proposal:
$ret = array
(
‘commands’ => array(),
‘options’ => array(),
‘flags’ => array(),
‘arguments’ => array(),
);
/* vim: set expandtab tabstop=2 shiftwidth=2: */
?>
i use emacs in c-mode for editing. in 4.3, starting a cli script like so:
Just another variant of previous script that group arguments doesn’t starts with ‘-‘ or ‘—‘
If you edit a php file in windows, upload and run it on linux with command line method. You may encounter a running problem probably like that:
Or you may encounter some other strange problem.
Care the enter key. In windows environment, enter key generate two binary characters ‘0D0A’. But in Linux, enter key generate just only a ‘OA’.
I wish it can help someone if you are using windows to code php and run it as a command line program on linux.
How to change current directory in PHP script to script’s directory when running it from command line using PHP 4.3.0?
(you’ll probably need to add this to older scripts when running them under PHP 4.3.0 for backwards compatibility)
Here’s what I am using:
chdir(preg_replace(‘/\\/[^\\/]+$/’,»»,$PHP_SELF));
Note: documentation says that «PHP_SELF» is not available in command-line PHP scripts. Though, it IS available. Probably this will be changed in future version, so don’t rely on this line of code.
an another «another variant» :
[arg2] => val2
[arg3] => arg3
[arg4] => true
[arg5] => true
[arg5] => false
)
Spawning php-win.exe as a child process to handle scripting in Windows applications has a few quirks (all having to do with pipes between Windows apps and console apps).
// We will run php.exe as a child process after creating
// two pipes and attaching them to stdin and stdout
// of the child process
// Define sa struct such that child inherits our handles
SECURITY_ATTRIBUTES sa = < sizeof(SECURITY_ATTRIBUTES) >;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
// Create the handles for our two pipes (two handles per pipe, one for each end)
// We will have one pipe for stdin, and one for stdout, each with a READ and WRITE end
HANDLE hStdoutRd, hStdoutWr, hStdinRd, hStdinWr;
// Now we have two pipes, we can create the process
// First, fill out the usage structs
STARTUPINFO si = < sizeof(STARTUPINFO) >;
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hStdoutWr;
si.hStdInput = hStdinRd;
// And finally, create the process
CreateProcess (NULL, «c:\\php\\php-win.exe», NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
// Close the handles we aren’t using
CloseHandle(hStdoutWr);
CloseHandle(hStdinRd);
// When we’re done writing to stdin, we close that pipe
CloseHandle(hStdinWr);
// Reading from stdout is only slightly more complicated
int i;
std::string processed(«»);
char buf[128];
I modified the PATHEXT environment variable in Windows XP, from the » ‘system’ control panel applet->’Advanced’ tab->’Environment Variables’ button-> ‘System variables’ text area».
Then from control panel «Folder Options» applet-> ‘File Types’ tab, I added a new file extention (php3), using the button ‘New’ and typing php3 in the window that pops up.
Then in the ‘Details for php3 extention’ area I used the ‘Change’ button to look for the Php.exe executable so that the php3 file extentions are associated with the php executable.
You have to modify also the ‘PATH’ environment variable, pointing to the folder where the php executable is installed
Hope this is useful to somebody
For those of you who want the old CGI behaviour that changes to the actual directory of the script use:
chdir(dirname($_SERVER[‘argv’][0]));
at the beginning of your scripts.
This posting is not a php-only problem, but hopefully will save someone a few hours of headaches. Running on MacOS (although this could happen on any *nix I suppose), I was unable to get the script to execute without specifically envoking php from the command line:
[macg4:valencia/jobs] tim% test.php
./test.php: Command not found.
However, it worked just fine when php was envoked on the command line:
[macg4:valencia/jobs] tim% php test.php
Well, here we are. Now what?
Was file access mode set for executable? Yup.
And you did, of course, remember to add the php command as the first line of your script, yeah? Of course.
Aaahhh. in BBEdit check how the file is being saved! Mac? Unix? or Dos? Bingo. It had been saved as Dos format. Change it to Unix:
NB: If you’re editing your php files on multiple platforms (i.e. Windows and Linux), make sure you double check the files are saved in a Unix format. those \r’s and \n’s ‘ll bite cha!
You can also call the script from the command line after chmod’ing the file (ie: chmod 755 file.php).
Adding a pause() function to PHP waiting for any user input returning it:
To hand over the GET-variables in interactive mode like in HTTP-Mode (e.g. your URI is myprog.html?hugo=bla&bla=hugo), you have to call
php myprog.html ‘&hugo=bla&bla=hugo’
dunno if this is on linux the same but on windows evertime
you send somthing to the console screen php is waiting for
the console to return. therefor if you send a lot of small
short amounts of text, the console is starting to be using
more cpu-cycles then php and thus slowing the script.
now this is just a small example but if you are writing an
app that is outputting a lot to the console, i.e. a text
based screen with frequent updates, then its much better
to first cach all output, and output is as one big chunk of
text instead of one char a the time.
ouput buffering is ideal for this. in my script i outputted
almost 4000chars of info and just by caching it first, it
speeded up by almost 400% and dropped cpu-usage.
because what is being displayed doesn’t matter, be it 2
chars or 40.0000 chars, just the call to output takes a
great deal of time. remeber that.
maybe someone can test if this is the same on unix-based
systems. it seems that the STDOUT stream just waits for
the console to report ready, before continueing execution.
In the above example, you would use: #!/usr/local/bin/php
I was looking for a way to interactively get a single character response from user. Using STDIN with fread, fgets and such will only work after pressing enter. So I came up with this instead:
For example you can do this code:
This will just output each line of the input file without doing anything to it.