PHP Cli arguments

PHP Cli arguments

am 28.12.2007 15:21:11 von The87Boy

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

Re: PHP Cli arguments

am 28.12.2007 15:33:00 von ivansanchez-alg

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žžžžžÍž...

Re: PHP Cli arguments

am 28.12.2007 15:54:26 von shimmyshack

On Dec 28, 2:33 pm, Iv=E1n S=E1nchez Ortega 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=E1n S=E1nchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
>
> Menos mal que la linea no da erržžžžžÍ=9E...

yes excatly escape the commands using " or ' and then explode by
space (" ") once you have the commands as a string

Re: PHP Cli arguments

am 28.12.2007 16:08:06 von The87Boy

On Dec 28, 3:33 pm, Iv=E1n S=E1nchez Ortega 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

Re: PHP Cli arguments

am 28.12.2007 16:10:32 von The87Boy

On Dec 28, 3:54 pm, shimmyshack 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 --)?

Re: PHP Cli arguments

am 28.12.2007 16:21:33 von ivansanchez-alg

The87Boy wrote:

> On Dec 28, 3:54 pm, shimmyshack 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

Re: PHP Cli arguments

am 28.12.2007 16:27:11 von The87Boy

On Dec 28, 4:21 pm, Iv=E1n S=E1nchez Ortega escomposlinux.-.punto.-.org> wrote:
> The87Boy wrote:
> > On Dec 28, 3:54 pm, shimmyshack 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

Re: PHP Cli arguments

am 28.12.2007 16:38:16 von Csaba Gabor

On Dec 28, 6:21 am, 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
>
> 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

Re: PHP Cli arguments

am 28.12.2007 17:13:36 von The87Boy

On Dec 28, 4:38 pm, Csaba Gabor wrote:
> On Dec 28, 6:21 am, 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
>
> > 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