PHP Cli arguments

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-28-2007
The87Boy
 
Posts: n/a
Default PHP Cli arguments

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
Reply With Quote
  #2 (permalink)  
Old 12-28-2007
Iván Sánchez Ortega
 
Posts: n/a
Default Re: PHP Cli arguments

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žžžžžÍž...
Reply With Quote
  #3 (permalink)  
Old 12-28-2007
shimmyshack
 
Posts: n/a
Default Re: PHP Cli arguments

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
Reply With Quote
  #4 (permalink)  
Old 12-28-2007
The87Boy
 
Posts: n/a
Default Re: PHP Cli arguments

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
Reply With Quote
  #5 (permalink)  
Old 12-28-2007
The87Boy
 
Posts: n/a
Default Re: PHP Cli arguments

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 --)?
Reply With Quote
  #6 (permalink)  
Old 12-28-2007
Iván Sánchez Ortega
 
Posts: n/a
Default Re: PHP Cli arguments

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

Reply With Quote
  #7 (permalink)  
Old 12-28-2007
The87Boy
 
Posts: n/a
Default Re: PHP Cli arguments

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
Reply With Quote
  #8 (permalink)  
Old 12-28-2007
Csaba Gabor
 
Posts: n/a
Default Re: PHP Cli arguments

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
Reply With Quote
  #9 (permalink)  
Old 12-28-2007
The87Boy
 
Posts: n/a
Default Re: PHP Cli arguments

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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:32 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0