This is a discussion on PHP Cli arguments within the PHP Language forums, part of the PHP Programming Forums category; I am trying to make a PHP Cli program, but there is something I do not know what to do ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I am trying to make a PHP Cli program, but there is something I do not
know what to do If we take an example: php5 test.php --add Hello World I am great --delete World great --sort test.txt I want to have an array with the commands, that look like this: Array ( [--add] => Array('Hello', 'World', 'I', 'am', 'great'), [--delete] => Array('World', 'great'), ['--sort'] => Array() ); I have made 2 arrays; one with commands that takes arguments and another with commands that takes no arguments. And in the earlier example, the --sort command takes no argument. Now I do not now how to make the finished array |
|
|||
|
The87Boy wrote:
> I am trying to make a PHP Cli program, but there is something I do not > know what to do > > If we take an example: > php5 test.php --add Hello World I am great --delete World great --sort > test.txt Why don't you escape the parameters? php5 test.php --add 'Hello World I am great' --delete 'World great' \ --sort test.txt It's standard practice, and it will ease your life when iterating through $argv... Cheers, -- ---------------------------------- Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org- Menos mal que la linea no da errÍ... |
|
|||
|
On Dec 28, 2:33 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.org> wrote: > The87Boy wrote: > > I am trying to make a PHP Cli program, but there is something I do not > > know what to do > > > If we take an example: > > php5 test.php --add Hello World I am great --delete World great --sort > > test.txt > > Why don't you escape the parameters? > > php5 test.php --add 'Hello World I am great' --delete 'World great' \ > --sort test.txt > > It's standard practice, and it will ease your life when iterating through > $argv... > > Cheers, > -- > ---------------------------------- > Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org- > > Menos mal que la linea no da errÍ... yes excatly escape the commands using " or ' and then explode by space (" ") once you have the commands as a string |
|
|||
|
On Dec 28, 3:33 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.org> wrote: > Why don't you escape the parameters? > > php5 test.php --add 'Hello World I am great' --delete 'World great' \ > --sort test.txt > > It's standard practice, and it will ease your life when iterating through > $argv... The reason why I have done it, is that it should be 5 seperate words, and it should work with and without escapes |
|
|||
|
On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gmail.com> wrote:
> yes excatly escape the commands using " or ' and then explode by > space (" ") once you have the commands as a string Yes, but how do I split it up commands (the arguments that starts with - or --)? |
|
|||
|
The87Boy wrote:
> On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gmail.com> wrote: >> yes excatly escape the commands using " or ' and then explode by >> space (" ") once you have the commands as a string > > Yes, but how do I split it up commands (the arguments that starts with > - or --)? They are already in $argv !! http://php.net/manual/en/features.commandline.php -- ---------------------------------- Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org- Proudly running Debian Linux with 2.6.22-3-amd64 kernel, KDE 3.5.8, and PHP 5.2.4-2 generating this signature. Uptime: 16:19:29 up 36 days, 2:35, 4 users, load average: 0.64, 0.93, 0.98 |
|
|||
|
On Dec 28, 4:21 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.org> wrote: > The87Boy wrote: > > On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gmail.com> wrote: > >> yes excatly escape the commands using " or ' and then explode by > >> space (" ") once you have the commands as a string > > > Yes, but how do I split it up commands (the arguments that starts with > > - or --)? > > They are already in $argv !! > > http://php.net/manual/en/features.commandline.php That is not the question, but how I can split up, when there is an argument starting with - or --, so the array's key is the argument that starts with - or -- and the array's values is the arguments, that follows the argument |
|
|||
|
On Dec 28, 6:21 am, The87Boy <the87...@gmail.com> wrote:
> I am trying to make a PHP Cli program, but there is something I do not > know what to do > > If we take an example: > php5 test.php --add Hello World I am great --delete World great --sort > test.txt > > I want to have an array with the commands, that look like this: > Array ( > [--add] => Array('Hello', 'World', 'I', 'am', 'great'), > [--delete] => Array('World', 'great'), > ['--sort'] => Array() > ); How about this (untested): $aArgs = array(); foreach ($argv as $arg) { if (substr($arg,0,2)=="--") $aArgs[$a=$arg] = array(); else $aArgs[$a][] = $arg; } Csaba Gabor from Vancouver |
|
|||
|
On Dec 28, 4:38 pm, Csaba Gabor <dans...@gmail.com> wrote:
> On Dec 28, 6:21 am, The87Boy <the87...@gmail.com> wrote: > > > I am trying to make a PHP Cli program, but there is something I do not > > know what to do > > > If we take an example: > > php5 test.php --add Hello World I am great --delete World great --sort > > test.txt > > > I want to have an array with the commands, that look like this: > > Array ( > > [--add] => Array('Hello', 'World', 'I', 'am', 'great'), > > [--delete] => Array('World', 'great'), > > ['--sort'] => Array() > > ); > > How about this (untested): > > $aArgs = array(); > foreach ($argv as $arg) { > if (substr($arg,0,2)=="--") $aArgs[$a=$arg] = array(); > else $aArgs[$a][] = $arg; } You are right, but I thought, I could validate if the argument starting with - or --, was in the array with arguments |
![]() |
| Thread Tools | |
| Display Modes | |
|
|