command line - Passing an argument(filename) to a CLI PHP Script -
i wanting write script in php process text files. know of few ways accomplish this, except 1 way this. want able pass file name, traditional command-line application.
for example:
php script.php textfile.txt
or script.php textfile.txt
i assume can done, because pass arguments composer. can't find how in docs or google.
can point me in right direction? or point me language supports this(maybe python or groovy, if php not option).
in php if run command line can treat value after php argument. inside script.php
have access these arguments in $argv
array.
for following php script:
<?php // script.php print_r($argv);
then if execute file command line:
> php script.php foo bar testfile.txt
you following result
array ( [0] => script.php [1] => foo [2] => bar [3] => testfile.txt )
see php documentation more information.
this simple way create script, if want create advanced command line tool programs recommend using symfony2 console component.
the console component enable encapsulate functionality of each command inside it's own class. can create simple "console" file able execute of these commands. take @ simple example application here.
Comments
Post a Comment