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:
#!/usr/bin/php
<?php
print_r(arguments($argv));
function arguments ( $args )
{
array_shift( $args );
$endofoptions = false;
$ret = array
(
'commands' => array(),
'options' => array(),
'flags' => array(),
'arguments' => array(),
);
while ( $arg = array_shift($args) )
{
// if we have reached end of options,
//we cast all remaining argvs as arguments
if ($endofoptions)
{
$ret['arguments'][] = $arg;
continue;
}
// Is it a command? (prefixed with --)
if ( substr( $arg, 0, 2 ) === '--' )
{
// is it the end of options flag?
if (!isset ($arg[3]))
{
$endofoptions = true;; // end of options;
continue;
}
$value = "";
$com = substr( $arg, 2 );
// is it the syntax '--option=argument'?
if (strpos($com,'='))
list($com,$value) = split("=",$com,2);
// is the option not followed by another option but by arguments
elseif (strpos($args[0],'-') !== 0)
{
while (strpos($args[0],'-') !== 0)
$value .= array_shift($args).' ';
$value = rtrim($value,' ');
}
$ret['options'][$com] = !empty($value) ? $value : true;
continue;
}
// Is it a flag or a serial of flags? (prefixed with -)
if ( substr( $arg, 0, 1 ) === '-' )
{
for ($i = 1; isset($arg[$i]) ; $i++)
$ret['flags'][] = $arg[$i];
continue;
}
// finally, it is not option, nor flag, nor argument
$ret['commands'][] = $arg;
continue;
}
if (!count($ret['options']) && !count($ret['flags']))
{
$ret['arguments'] = array_merge($ret['commands'], $ret['arguments']);
$ret['commands'] = array();
}
return $ret;
}
exit (0)
/* vim: set expandtab tabstop=2 shiftwidth=2: */
?>
PHP をコマンドラインから使用する
4.3 以降で PHP は Command Line Interface を意味する CLI という名前の新しい SAPI 型 (Server Application Programming Interface)をサポートします。 名前から分かるように、この SAPI 型は、 PHP によるシェル(またはデスクトップ)アプリケーションの開発を 主な対象としています。 CLI SAPI と他の SAPI の間には、 いくつかの違いがあります。本章では、これらについて詳細を説明します。 CLI と CGI はその振る舞いの多くが 共通であるにもかかわらず、違う SAPI であることに留意してください。
CLI SAPI は、当初 PHP 4.2.0 でリリースされましたが、 この時点では実験的なステータスにあったため、 ./configure を実行する際に、明示的に --enable-cli を指定することにより、 有効とする必要がありました。PHP 4.3.0 以降、 CLI SAPI はもはや実験的なステータスではなくなり、 --enable-cli はデフォルトでオンとなりました。 --disable-cli によりこれを無効とする ことも可能です。
PHP 4.3.0 以降、システムにPHPがどのようにインストールされているかによって CLI/CGI バイナリの名前、位置、存在が異なります。デフォルトでは make を実行すると CGI と CLI の両方がビルドされ ソースディレクトリの sapi/cgi/php と sapi/cli/php にそれぞれ配置されます。両方とも php という名前であることに注意してください make install でどうなるかは configure に因ります。 configure で例えば apxs のような SAPI モジュールが選択された場合、または --disable-cgi が指定された場合、 make install によって CLI が {PREFIX}/bin/php にコピーされます。さもなければ CGI がそこにコピーされます。 既にインストールされている CGI バイナリを上書きしたい場合には、 make install の後に make install-cli を実行してください。あるいは configure で --disable-cgi を指定することもできます。
注意: --enable-cli と --enable-cgi の両方がデフォルトで有効になっています。そのため、configure で --enable-cli を指定したからといって、make install で {PREFIX}/bin/php にコピーされるのが必ずしも CLI になるとは限りません。
PHP 4.2.0 から PHP 4.2.3 までの Windows パッケージでは CLI は CGI php.exe と同じフォルダに php-cli.exe として配布されていました。 PHP 4.3.0 からは Windows パッケージでは CLI は cli という 別のフォルダに php.exe として配布されます。 したがって cli/php.exe となります。 PHP 5 以降、CLI はメインフォルダ内で php.exe という名前で配布されます。CGI バージョンは、php-cgi.exe として配布されます。
PHP 5 では、新しく php-win.exe というファイルが 配布されます。これは CLI バージョンとほぼ同じですが、php-win は何も 出力しないため、コンソールを必要としません(画面に「DOS 窓」が表示 されません)。この振る舞いは、php-gtk と似ています。このためには --enable-cli-win32 オプションを 含めて configure を行います。
注意: 自分の SAPI は何か? シェルで php -v をタイプすると、 php が CGI なのか CLI なのかわかります。 php_sapi_name() と定数 PHP_SAPI も参照してください。
注意: Unix の man ページが PHP 4.3.2 で追加されました。 シェル環境から man php とすることで見ることができます。
CLI SAPI を他の SAPI と比べた時の 大きな違いを以下に示します。
-
CGI SAPI と異なり、ヘッダが出力されません。
CGI SAPI は HTTP ヘッダの出力を抑制する機能を 提供していますが、等価な機能は CLI SAPI では サポートされていません。
デフォルトでは CLI は静寂モードで起動されます。古い CGI スクリプトと互換性を 保って使えるように -q スイッチが残されています。
動作するディレクトリはスクリプトの場所に変更されることはありません (-C および --no-chdir スイッチは 互換性のために残されています)。
エラーメッセージはプレーンテキストで表示されます (HTML でフォーマットされません)。
-
以下に示すいくつかの php.ini ディレクティブは、CLI SAPI により上書きされます。これは、シェル環境では意味が ないためです。
上書きされる php.ini のディレクティブ ディレクティブ CLI SAPI のデフォルト値 コメント html_errors FALSE エラーメッセージに含まれる HTML タグは シェル上では意味がなく、可読性をかなり低下させるため、この ディレクティブはデフォルトで FALSE となっています。 implicit_flush TRUE print(), echo() および 関連するものによる全ての出力は、直ちに出力され、バッファに キャッシュされないことが望ましいと言えます。この場合でも、 標準出力を保留または操作したい場合には、 output buffering を 使用することが可能です。 max_execution_time 0 (unlimited) シェル環境では、PHP を際限なく使用できる ようにするために、最大実行時間の制限は無しに設定されています。 Web 用アプリケーションは数秒単位で実行されるよう作られて いますが、シェルアプリケーションの実行時間は、これよりかなり 長くなる傾向があります。 register_argc_argv TRUE CLI SAPI を使用している場合、グローバル PHP 変数 $argc (アプリケーションに渡される引数の数)と $argv (引数の値の配列)は常に登録され、 適切な値が代入されます。
PHP 4.3.0以降、CLI SAPI を使用するときに PHP の $argc 変数と $argv 変数が登録され、適切な値がセットされます。 このバージョンより前では、CGI や MODULE がこれらの変数を生成するには PHP の register_globals ディレクティブがオンになっている必要がありました。 バージョンや register_globals の設定がどうであろうと、 $_SERVER または $HTTP_SERVER_VARS は常に使用可能です。 例:$_SERVER['argv']
注意: これらのディレクティブは、設定ファイル php.ini またはカスタム 設定ファイル(指定した場合のみ)で他の値に初期化できません。 この制限は、これらのデフォルト値が全ての設定ファイルをパースした後に 適用されるためです。しかし、これらの値は実行時に変更することが 可能です (上記のディレクティブの全てについてこれが当てはまるわけでは ありません。例えば、register_argc_argv)。
-
シェル環境での動作を容易とするために、以下の定数が定義されています。
CLI 固有の定数 定数 説明 STDIN stdin へのオープン済みのストリーム。 これにより、以下のようにオープンする必要がなくなります。 stdin から1行読み込みたい場合、以下のようにします。<?php
$stdin = fopen('php://stdin', 'r');
?><?php
$line = trim(fgets(STDIN)); // STDIN から 1 行読み込む
fscanf(STDIN, "%d\n", $number); // STDIN から数値を読み込む
?>STDOUT stdout へのオープン済みのストリーム。 これにより、以下のようにオープンする必要がなくなります。 <?php
$stdout = fopen('php://stdout', 'w');
?>STDERR stderr へのオープン済みのストリーム。 これにより、以下のようにオープンする必要がなくなります。 <?php
$stderr = fopen('php://stderr', 'w');
?>上記のように、stderr のようなストリームを自分で オープンする必要はなく、以下のようにストリームリソースの代わりに 定数を使用するだけでかまいません。
これらのストリームを明示的に閉じる必要はありません。これは、 PHP により自動的に行われます。php -r 'fwrite(STDERR, "stderr\n");'
注意: これらの定数は、PHP スクリプトを stdin から読み込んだ場合は使用できません。
-
CLI SAPI は、実行されるスクリプトのディレクトリに カレントディレクトリを変更しません !
CGI SAPI との違いを示す例を以下に示します。
<?php
// シンプルなテストアプリケーション
echo getcwd(), "\n";
?>CGI 版により実行した場合、出力は以下のようになります。
$ pwd /tmp $ php-cgi -f another_directory/test.php /tmp/another_directory
これは、PHP が実行するスクリプトのディレクトリに カレントディレクトリを変更することを明らかに示しています。
CLI SAPI を使用した場合の出力は次のようになります。
$ pwd /tmp $ php -f another_directory/test.php /tmp
これにより、PHP でシェルツールを書く際の柔軟性をより大きくすることができます。
注意: CGI SAPI は、この CLI SAPI の動作をコマンドライン実行時のスイッチ -C によりサポートしています。
PHP バイナリにより提供されるコマンドラインオプションの一覧は、 -h スイッチを指定して PHP を実行することにより いつでも調べることができます。
Usage: php [options] [-f] <file> [--] [args...] php [options] -r <code> [--] [args...] php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...] php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...] php [options] -- [args...] php [options] -a -a Run interactively -c <path>|<file> Look for php.ini file in this directory -n No php.ini file will be used -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -f <file> Parse and execute <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -s Display colour syntax highlighted source. -v Version number -w Display source with stripped comments and whitespace. -z <file> Load Zend extension <file>. args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin --ini Show configuration file names --rf <name> Show information about function <name>. --rc <name> Show information about class <name>. --re <name> Show information about extension <name>. --ri <name> Show configuration for extension <name>.
CLI SAPI は、実行する PHP コードを 取得するために三種類の異なる手段をサポートしています。
-
PHP に特定のファイルの実行を指示する。
php my_script.php php -f my_script.php
上記の方法は共に(-f スイッチの使用の如何に関らず) 指定したファイル my_script.php を実行します。 実行ファイルとしてあらゆるファイルを指定することができ、 PHP スクリプトは拡張子 .php で終わる必要がなく、任意の名前や拡張子を 使用することができます。
注意: -f スイッチを使用している時にスクリプトに引数を渡したい場合は、 最初の引数として -- を渡す必要があります。
-
実行する PHP コードをコマンドラインで直接指定する。
php -r 'print_r(get_defined_constants());'
シェル変数の置換と引用符の使用については特に注意してください。
注意: この例をよくみてください。開始/終了タグがありません! -r スイッチを使用した場合、これらのタグは不要と なります。これらのタグを使用するとパーサエラーを発生します。
-
実行する PHP コードを標準入力 (stdin)で指定する。
これは強力な機能で、以下の(仮想的な)例に示すように、動的に PHP コードを生成し、実行バイナリに入力すること ができます。
$ some_application | some_filter | php | sort -u >final_output.txt
他のシェルアプリケーションのように、PHP バイナリに 引数を指定することができるだけでなく、PHP スクリプトが この引数を取得することも可能です。スクリプトに指定できる 引数の数は PHP による制限を受けません (シェルは指定可能な文字数の最大値を設定しています。通常、この制限値を 越えることはできません)。スクリプトに指定した引数は、グローバル配列 $argv でアクセス可能です。 添字 0 は、常にスクリプト名が含まれています (PHP コードが標準入力またはコマンドラインスイッチ -r により指定された場合、スクリプト名は - となります)。 登録される第 2 のグローバル変数は $argc で (スクリプトに指定された引数の数ではなく )、配列 $argv の要素数が含まれます。
スクリプトに指定する引数が文字 - で始まっていない 限り、特に留意すべきことはありません。スクリプトに指定する引数が文字 - で始まる場合、PHP 自体がこれを パースする必要があるとみなすため、問題を発生します。 これを防止するため、引数リストセパレータ -- を 使用してください。PHP にパースされる引数の後に このセパレータを置くと、その後の全ての引数はそのままパースされずに スクリプトに渡されます。
# これは、指定したコードを実行せずに PHP の使用法を示します $ php -r 'var_dump($argv);' -h Usage: php [options] [-f] <file> [args...] [...] # これは '-h' を引数として解釈し、PHP の使用法を表示しません $ php -r 'var_dump($argv);' -- -h array(2) { [0]=> string(1) "-" [1]=> string(2) "-h" }
また、PHP をシェルスクリプトとして使用する他の手段があります。 最初の行が #!/usr/bin/php で始まり、 PHP の開始/終了タグの中に通常の PHP コードが続くスクリプトを書き、適当なファイル 実行属性を設定する(例: chmod +x test)ことが可能です。 この方法は、通常のシェル/Perl スクリプトと同様に実行することができます。
例1 シェルスクリプトとしての PHP スクリプトの実行
#!/usr/bin/php
<?php
var_dump($argv);
?>
このファイルの名前が test で、カレントディレクトリに あるとすると、以下のように実行することができます。
$ chmod +x test $ ./test -h -- foo array(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo" }
見て分かるように、- で始まるスクリプトのパラメータを 指定する際に、特に注意する必要はありません。
長いオプション指定は、PHP 4.3.3 以降で利用可能です。
| オプション | 長いオプション | 説明 |
|---|---|---|
| -a | --interactive |
PHP を対話的に実行します。PHP のコンパイル時に Readline 拡張モジュール(Windows 版では利用できません)を含めた場合、補完(例: 変数名の一部を 入力して TAB キーを押すと、PHP が完全な変数名に変換する)・ 矢印キーによるコマンド履歴 などの高度な機能が利用できます。コマンドの履歴は ~/.php_history ファイルに保存されます。
|
| -c | --php-ini |
このオプションを使用することにより、php.ini を探すディレクトリを 指定したり、カスタマイズされた INI ファイル (php.ini という名前である必要はありません)を直接指定する ことが可能です。例:
このオプションを指定しない場合、ファイルは、 デフォルトの位置 で探索されます。 |
| -n | --no-php-ini |
php.ini を完全に無視します。このスイッチは、PHP 4.3.0 以降で 利用可能です。 |
| -d | --define |
このオプションにより php.ini で指定できる設定ディレクティブに カスタム値を設定することができます。構文は以下のようになります。
例 (レイアウト上の理由により、長い行が折りたたまれています):
|
| -e | --profile-info |
デバッガ/プロファイラ用の拡張情報を出力します。 |
| -f | --file |
-f オプションに指定したファイル名をパースし、 実行します。このスイッチはオプションで省略することもできます。 実行するスクリプトを指定するだけで充分です。
|
| -h と -? | --help と --usage | このオプションを使用すると、実際の一連のコマンドラインオプションと 各1行の説明が情報を取得できます。 |
| -i | --info | このコマンドラインオプションは、phpinfo() をコールし、 結果を出力します。PHP が正しく動作していない場合、 php -i を実行し、情報テーブルの前または中に 出力されるエラーメッセージを調べることをお勧めします。 出力は、HTML 形式で行なわれるため、かなり 量が多くなることに注意してください。 |
| -l | --syntax-check |
このオプションにより、指定した PHP コードの 構文チェックのみを簡単に行なうことができます。成功した場合、 テキスト No syntax errors detected in <filename> が標準出力に書き込まれ、リターンコードは 0 となります。失敗した場合、テキスト Errors parsing <filename> に加え、内部パーサエラーメッセージ が標準出力に書き込まれ、シェルリターンコードは、 255 となります。 このオプションは、(未定義の関数のような)致命的なエラー(fatal error) はみつけません。致命的なエラーについても調べたい場合は、 -f を使用してください。
|
| -m | --modules |
このオプションを使用すると、PHP に組み込まれた (そしてロードされた) Zend モジュールを出力します。
|
| -r | --run |
このオプションにより、コマンドラインのみで PHP の実行ができるようになります。 PHP の開始および終了タグ (<?php および ?>) は不要で、これらを付けると パーサエラーとなります。
|
| -B | --process-begin |
標準入力を処理する前に実行する PHP コードを指定します。 PHP 5 で追加されました。 |
| -R | --process-code |
それぞれの入力行に対して実行する PHP コードを指定します。 PHP 5 で追加されました。 このモードには 2 つの特別な変数 $argn と $argi が用意されています。 $argn は PHP がその瞬間に処理している行を含み、 $argi はその行番号を含んでいます。 |
| -F | --process-file |
全ての入力行に対して実行する PHP ファイルを指定します。 PHP 5 で追加されました。 |
| -E | --process-end |
入力を処理した後に実行する PHP コードを指定します。 PHP 5 で追加されました。 例2 とあるプロジェクトの行数をカウントするための -B, -R そして -E オプションの使用例
|
| -s | --syntax-highlight と --syntax-highlight |
カラー構文ハイライト表示されたソースを表示します。 このオプションは、ファイルをパースし、HTML ハイライト表示版のファイルを生成し、標準出力に書き出す内部機 構を使用します。行うのは <code> [...] </code> のブロックを 生成することだけで、HTML ヘッダは 出力されないことに注意してください。
|
| -v | --version |
PHP, PHP SAPI, Zend のバージョンを標準出力に出力します。例:
|
| -w | --strip |
コメントと空白文字を削除してソースを表示します。
|
| -z | --zend-extension |
Zend エクステンションをロードします。ファイル名のみが指定された場合、 PHP はこの拡張をカレントのシステムのデフォルトライブラリパスから ロードしようとします (Linux システムの場合は /etc/ld.so.conf で 指定されています)。 ファイル名を絶対パスで指定した場合、システムのライブラリサーチパスを 使用しません。ディレクトリ情報を有する相対ファイル名を 指定すると、PHP は カレントのディレクトリの相対パスから拡張モジュールをロードする ことのみを行ないます。 |
| --ini |
設定ファイルの名前、設定ファイルを検索するディレクトリを表示します。 PHP 5.2.3 以降で使用可能です。 例3 --ini の例 $ php --ini Configuration File (php.ini) Path: /usr/dev/php/5.2/lib Loaded Configuration File: /usr/dev/php/5.2/lib/php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none) |
|
| --rf | --rfunction |
指定した関数あるいはクラスメソッドについての情報 (たとえばパラメータの数と名前など) を表示します。 PHP 5.1.2 以降で使用可能です。 このオプションは、PHP が リフレクション のサポートつきでコンパイルされている場合にのみ使用可能です。
例4 基本的な --rf の使用法
$ php --rf var_dump
Function [ <internal> public function var_dump ] {
- Parameters [2] {
Parameter #0 [ <required> $var ]
Parameter #1 [ <optional> $... ]
}
}
|
| --rc | --rclass |
指定したクラスについての情報 (定数、プロパティおよびメソッドの一覧) を表示します。PHP 5.1.2 以降で使用可能です。 このオプションは、PHP が リフレクション のサポートつきでコンパイルされている場合にのみ使用可能です。
例5 --rc の例
$ php --rc Directory
Class [ <internal:standard> class Directory ] {
- Constants [0] {
}
- Static properties [0] {
}
- Static methods [0] {
}
- Properties [0] {
}
- Methods [3] {
Method [ <internal> public method close ] {
}
Method [ <internal> public method rewind ] {
}
Method [ <internal> public method read ] {
}
}
}
|
| --re | --rextension |
指定した拡張モジュールについての情報 (php.ini オプション、 定義されている関数、定数およびクラスの一覧) を表示します。 PHP 5.1.2 以降で使用可能です。 このオプションは、PHP が リフレクション のサポートつきでコンパイルされている場合にのみ使用可能です。
例6 --re の例
$ php --re json
Extension [ <persistent> extension #19 json version 1.2.1 ] {
- Functions {
Function [ <internal> function json_encode ] {
}
Function [ <internal> function json_decode ] {
}
}
}
|
| --ri | --rextinfo |
指定した拡張モジュールについての設定情報 (phpinfo() が返す情報と同じもの) を表示します。 PHP 5.2.2 以降で使用可能です。コア機能に関する設定情報は、 "main" というモジュール名で取得できます。
例7 --ri の例 $ php --ri date date date/time support => enabled "Olson" Timezone Database Version => 2007.5 Timezone Database => internal Default timezone => Europe/Oslo Directive => Local Value => Master Value date.timezone => Europe/Oslo => Europe/Oslo date.default_latitude => 59.22482 => 59.22482 date.default_longitude => 11.018084 => 11.018084 date.sunset_zenith => 90.583333 => 90.583333 date.sunrise_zenith => 90.583333 => 90.583333 |
PHP 実行バイナリは、Web サーバから完全に独立して PHP スクリプトを 実行するために使用することができます。Unix システムを使用している場合、 実行可能とするためには PHP スクリプトの先頭に特別な一行を追加する必要が あります。これにより、システムがそのスクリプトを実行するプログラムを 知ることができます。 Windows 環境では、.php ファイルのダブルクリック オプションに php.exe を関連づけることができます。 または、PHP によりスクリプトを実行するバッチファイルを作成することも 可能です。Unix 上で動作させるためにスクリプトに追加された先頭行は、 Windows 環境での動作に悪影響を与えません。このため、この手法により、 クロスプラットフォーム環境で動作するプログラムを書くことができます。 コマンドライン PHP プログラムの書方の簡単な例を以下に示します。
例8 コマンドラインから実行されることを意図したスクリプト(script.php)
#!/usr/bin/php
<?php
if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>
これは、ひとつのオプションをとるコマンドラインの PHP スクリプトです。
使用法:
<?php echo $argv[0]; ?> <option>
<option> は出力したい単語です。
--help, -help, -h, あるいは -? を指定すると、
このヘルプが表示されます。
<?php
} else {
echo $argv[1];
}
?>
上のスクリプトでは、特殊な先頭行が用いられており、このファイルが PHP により実行されることを示しています。ここでは CLI 版を使用しているため、 HTTP ヘッダは出力されません。PHP で コマンドラインアプリケーションを使用する際には、2 つの変数 $argc および $argv を使用することが できます。 最初の変数は、引数の数に 1 (実行中のスクリプトの名前)を加えたものです。 2 番目の変数は、引数を保持する配列で、スクリプト名を有する 要素 0 ($argv[0]) から始まっています。
上のプログラムでは、引数が 1 より少ないかまたは多いかを調べています。 また、引数が --help, -help, -h または -? の場合、 ヘルプメッセージを出力し、動的にスクリプト名を出力します。 他の引数を受け取った場合、これを出力します。
上のスクリプトを Unix で実行する場合、実行可能とした後、 script.php echothis または script.php -h とする必要があります。 Windows では、この処理を行なう以下のようなバッチファイルを作成することが できます。
例9 コマンドライン PHP スクリプトを実行するバッチファイル(script.bat)
@C:\php\php.exe script.php %1 %2 %3 %4
上のプログラムが script.php という名前であるとし、 c:\php\php.exe に php.exe があるとすると、このバッチファイルは、追加オプション script.bat echothis または script.bat -h を指定して、スクリプトを実行します。
PHP のコマンドラインアプリケーションを拡張するために使用できる その他の関数については、拡張モジュール Readline に関する ドキュメントも参照してください。
PHP をコマンドラインから使用する
15-Jun-2008 06:08
07-May-2008 04:08
If a module SAPI is chosen during configure, such as apxs, or the --disable-cgi option is used, the CLI is copied to {PREFIX}/bin/php during make install otherwise the CGI is placed there.
versus
Changed CGI install target to php-cgi and 'make install' to install CLI when CGI is selected. (changelog for 5.2.3)
http://www.php.net/ChangeLog-5.php#5.2.3
29-Feb-2008 10:32
When you want to get inputs from STDIN, you may use this function if you like using C coding style.
<?php
// up to 8 variables
function scanf($format, &$a0=NULL, &$a1=NULL, &$a2=NULL, &$a3=NULL,
&$a4=NULL, &$a5=NULL, &$a6=NULL, &$a7=NULL)
{
$num_args = func_num_args();
if($num_args > 1) {
$inputs = fscanf(STDIN, $format);
for($i=0; $i<$num_args-1; $i++) {
$arg = 'a'.$i;
$$arg = $inputs[$i];
}
}
}
scanf("%d", $number);
?>
17-Feb-2008 01:29
I find regex and manually breaking up the arguments instead of havingon $_SERVER['argv'] to do it more flexiable this way.
cli_test.php asdf asdf --help --dest=/var/ -asd -h --option mew arf moo -z
Array
(
[input] => Array
(
[0] => asdf
[1] => asdf
)
[commands] => Array
(
[help] => 1
[dest] => /var/
[option] => mew arf moo
)
[flags] => Array
(
[0] => asd
[1] => h
[2] => z
)
)
<?php
function arguments ( $args )
{
array_shift( $args );
$args = join( $args, ' ' );
preg_match_all('/ (--\w+ (?:[= ] [^-]+ [^\s-] )? ) | (-\w+) | (\w+) /x', $args, $match );
$args = array_shift( $match );
/*
Array
(
[0] => asdf
[1] => asdf
[2] => --help
[3] => --dest=/var/
[4] => -asd
[5] => -h
[6] => --option mew arf moo
[7] => -z
)
*/
$ret = array(
'input' => array(),
'commands' => array(),
'flags' => array()
);
foreach ( $args as $arg ) {
// Is it a command? (prefixed with --)
if ( substr( $arg, 0, 2 ) === '--' ) {
$value = preg_split( '/[= ]/', $arg, 2 );
$com = substr( array_shift($value), 2 );
$value = join($value);
$ret['commands'][$com] = !empty($value) ? $value : true;
continue;
}
// Is it a flag? (prefixed with -)
if ( substr( $arg, 0, 1 ) === '-' ) {
$ret['flags'][] = substr( $arg, 1 );
continue;
}
$ret['input'][] = $arg;
continue;
}
return $ret;
}
print_r( arguments( $argv ) );
?>
12-Feb-2008 10:21
Here's an update to the script a couple of people gave below to read arguments from $argv of the form --name=VALUE and -flag. Changes include:
Don't use $_ARG - $_ is generally considered reserved for the engine.
Don't use regex where a string operation will do just as nicely
Don't overwrite --name=VALUE with -flag when 'name' and 'flag' are the same thing
Allow for VALUE that has an equals sign in it
function arguments($argv) {
$ARG = array();
foreach ($argv as $arg) {
if (strpos($arg, '--') === 0) {
$compspec = explode('=', $arg);
$key = str_replace('--', '', array_shift($compspec));
$value = join('=', $compspec);
$ARG[$key] = $value;
} elseif (strpos($arg, '-') === 0) {
$key = str_replace('-', '', $arg);
if (!isset($ARG[$key])) $ARG[$key] = true;
}
}
return $ARG;
}
29-Oct-2007 07:51
Here's <losbrutos at free dot fr> function modified to support unix like param syntax like <B Crawford> mentions:
<?php
function arguments($argv) {
$_ARG = array();
foreach ($argv as $arg) {
if (preg_match('#^-{1,2}([a-zA-Z0-9]*)=?(.*)$#', $arg, $matches)) {
$key = $matches[1];
switch ($matches[2]) {
case '':
case 'true':
$arg = true;
break;
case 'false':
$arg = false;
break;
default:
$arg = $matches[2];
}
/* make unix like -afd == -a -f -d */
if(preg_match("/^-([a-zA-Z0-9]+)/", $matches[0], $match)) {
$string = $match[1];
for($i=0; strlen($string) > $i; $i++) {
$_ARG[$string[$i]] = true;
}
} else {
$_ARG[$key] = $arg;
}
} else {
$_ARG['input'][] = $arg;
}
}
return $_ARG;
}
?>
Sample:
eromero@ditto ~/workspace/snipplets $ foxogg2mp3.php asdf asdf --help --dest=/var/ -asd -h
Array
(
[input] => Array
(
[0] => /usr/local/bin/foxogg2mp3.php
[1] => asdf
[2] => asdf
)
[help] => 1
[dest] => /var/
[a] => 1
[s] => 1
[d] => 1
[h] => 1
)
23-Oct-2007 05:11
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:
#!/usr/bin/php -q
<?php
function inKey($vals) {
$inKey = "";
While(!in_array($inKey,$vals)) {
$inKey = trim(`read -s -n1 valu;echo \$valu`);
}
return $inKey;
}
function echoAT($Row,$Col,$prompt="") {
// Display prompt at specific screen coords
echo "\033[".$Row.";".$Col."H".$prompt;
}
// Display prompt at position 10,10
echoAT(10,10,"Opt : ");
// Define acceptable responses
$options = array("1","2","3","4","X");
// Get user response
$key = inKey($options);
// Display user response & exit
echoAT(12,10,"Pressed : $key\n");
?>
Hope this helps someone.
22-Oct-2007 10:01
I have not seen in this thread any code snippets that support the full *nix style argument parsing. Consider this:
<?php
print_r(getArgs($_SERVER['argv']));
function getArgs($args) {
$out = array();
$last_arg = null;
for($i = 1, $il = sizeof($args); $i < $il; $i++) {
if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ) {
$parts = explode("=", $match[1]);
$key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
if(isset($parts[1])) {
$out[$key] = $parts[1];
}
else {
$out[$key] = true;
}
$last_arg = $key;
}
else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ) {
for( $j = 0, $jl = strlen($match[1]); $j < $jl; $j++ ) {
$key = $match[1]{$j};
$out[$key] = true;
}
$last_arg = $key;
}
else if($last_arg !== null) {
$out[$last_arg] = $args[$i];
}
}
return $out;
}
/*
php file.php --foo=bar -abc -AB 'hello world' --baz
produces:
Array
(
[foo] => bar
[a] => true
[b] => true
[c] => true
[A] => true
[B] => hello world
[baz] => true
)
*/
?>
27-Sep-2007 08:54
an another "another variant" :
<?php
function arguments($argv)
{
$_ARG = array();
foreach ($argv as $arg)
{
if (preg_match('#^-{1,2}([a-zA-Z0-9]*)=?(.*)$#', $arg, $matches))
{
$key = $matches[1];
switch ($matches[2])
{
case '':
case 'true':
$arg = true;
break;
case 'false':
$arg = false;
break;
default:
$arg = $matches[2];
}
$_ARG[$key] = $arg;
}
else
{
$_ARG['input'][] = $arg;
}
}
return $_ARG;
}
?>
$php myscript.php arg1 -arg2=val2 --arg3=arg3 -arg4 --arg5 -arg6=false
Array
(
[input] => Array
(
[0] => myscript.php
[1] => arg1
)
[arg2] => val2
[arg3] => arg3
[arg4] => true
[arg5] => true
[arg5] => false
)
17-Aug-2007 03:24
For those who was unable to clear the windows screen trying to run CLS command:
CLS is not an windows executable file! It is an option from command.com!
So, the rigth command is
system("command /C cls");
23-Jul-2007 02:04
Just another variant of previous script that group arguments doesn't starts with '-' or '--'
function arguments($argv) {
$_ARG = array();
foreach ($argv as $arg) {
if (ereg('--([^=]+)=(.*)',$arg,$reg)) {
$_ARG[$reg[1]] = $reg[2];
} elseif(ereg('^-([a-zA-Z0-9])',$arg,$reg)) {
$_ARG[$reg[1]] = 'true';
} else {
$_ARG['input'][]=$arg;
}
}
return $_ARG;
}
$ php myscript.php --user=nobody /etc/apache2/*
Array
(
[input] => Array
(
[0] => myscript.php
[1] => /etc/apache2/apache2.conf
[2] => /etc/apache2/conf.d
[3] => /etc/apache2/envvars
[4] => /etc/apache2/httpd.conf
[5] => /etc/apache2/mods-available
[6] => /etc/apache2/mods-enabled
[7] => /etc/apache2/ports.conf
[8] => /etc/apache2/sites-available
[9] => /etc/apache2/sites-enabled
)
[user] => nobody
)
26-Jun-2007 02:02
In 5.1.2 (and others, I assume), the -f form silently drops the first argument after the script name from $_SERVER['argv']. I'd suggest avoiding it unless you need it for a special case.
04-Jun-2007 08:16
Just a variant of previous script to accept arguments with '=' also
<?php
function arguments($argv) {
$_ARG = array();
foreach ($argv as $arg) {
if (ereg('--([^=]+)=(.*)',$arg,$reg)) {
$_ARG[$reg[1]] = $reg[2];
} elseif(ereg('-([a-zA-Z0-9])',$arg,$reg)) {
$_ARG[$reg[1]] = 'true';
}
}
return $_ARG;
}
?>
$ php myscript.php --user=nobody --password=secret -p --access="host=127.0.0.1 port=456"
Array
(
[user] => nobody
[password] => secret
[p] => true
[access] => host=127.0.0.1 port=456
)
13-May-2007 05:55
While working with command line scripts it is tedious to handle the arguments in a numerated array.
The following code will:
If the argument is of the form –NAME=VALUE it will be represented in the array as an element with the key NAME and the value VALUE. I the argument is a flag of the form -NAME it will be represented as a boolean with the name NAME with a value of true in the associative array.
<?php
function arguments($argv) {
$_ARG = array();
foreach ($argv as $arg) {
if (ereg('--[a-zA-Z0-9]*=.*',$arg)) {
$str = split("=",$arg); $arg = '';
$key = ereg_replace("--",'',$str[0]);
for ( $i = 1; $i < count($str); $i++ ) {
$arg .= $str[$i];
}
$_ARG[$key] = $arg;
} elseif(ereg('-[a-zA-Z0-9]',$arg)) {
$arg = ereg_replace("-",'',$arg);
$_ARG[$arg] = 'true';
}
}
return $_ARG;
}
?>
Example:
<?php print_r(arguments($argv)); ?>
# php5 myscript.php --user=nobody --password=secret -p
Array
(
[user] => nobody
[password] => secret
[p] => true
)
09-Apr-2007 06:27
I had a problem with PHP 5.2.0 (cli) (winXP) that no output was printed when I tried to run any file. Using the -n switch solved the problem.
Apparently the interpreter can't always find php.ini, even though both exist in the same folder and the PATH variable is set correctly. No error messages were printed either.
24-Mar-2007 03:48
i use emacs in c-mode for editing. in 4.3, starting a cli script like so:
#!/usr/bin/php -q /* -*- c -*- */
<?php
told emacs to drop into c-mode automatically when i loaded the file for editing. the '-q' flag didn't actually do anything (in the older cgi versions, it suppressed html output when the script was run) but it caused the commented mode line to be ignored by php.
in 5.2, '-q' has apparently been deprecated. replace it with '--' to achieve the 4.3 invocation-with-emacs-mode-line behavior:
#!/usr/bin/php -- /* -*- c -*- */
<?php
don't go back to your 4.3 system and replace '-q' with '--'; it seems to cause php to hang waiting on STDIN...
09-Mar-2007 11:14
To display colored text when it is actually supported :
<?php
echo "\033[31m".$myvar; // red foreground
echo "\033[41m".$myvar; // red background
?>
To reset these settings :
<?php
echo "\033[0m";
?>
More fun :
<?php
echo "\033[5;30m;\033[48mWARNING !"; // black blinking text over red background
?>
More info here : http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
06-Mar-2007 12:03
python coders might miss this construct when working in PHP:
if __name__=='__main__':
# handle direct invocation from command line
it's a great way to embed little bits of test code (or a full-on cli for that matter),
while keeping the source file usable in other contexts.
Far as I can tell, this is the closest approximation available in PHP5:
if ('cli'===php_sapi_name() &&
__FILE__===realpath(
getcwd().DIRECTORY_SEPARATOR.$_SERVER['argv'][0]
)) {
// handle direct invocation from command line
}
27-Nov-2006 12:46
Hi,
This function clears the screen, like "clear screen"
<?php
function clearscreen($out = TRUE) {
$clearscreen = chr(27)."[H".chr(27)."[2J";
if ($out) print $clearscreen;
else return $clearscreen;
}
?>
14-Nov-2006 04:57
An addition to my previous post (you can replace it)
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 you still get the "Command not found."
Just try to run it as ./myscript.php , with the "./"
if it works - it means your current directory is not in the executable search path.
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.
\Alon
It seems like 'max_execution_time' doesn't work on CLI.
<?php
php -d max_execution_time