By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

PHP Cli arguments

P: n/a
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
Dec 28 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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žžžžžÍž...
Dec 28 '07 #2

P: n/a
On Dec 28, 2:33 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
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
Dec 28 '07 #3

P: n/a
On Dec 28, 3:33 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
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
Dec 28 '07 #4

P: n/a
On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gmail.comwrote:
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 --)?
Dec 28 '07 #5

P: n/a
The87Boy wrote:
On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gmail.comwrote:
>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

Dec 28 '07 #6

P: n/a
On Dec 28, 4:21 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
The87Boy wrote:
On Dec 28, 3:54 pm, shimmyshack <matt.fa...@gmail.comwrote:
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
Dec 28 '07 #7

P: n/a
On Dec 28, 6:21 am, The87Boy <the87...@gmail.comwrote:
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
Dec 28 '07 #8

P: n/a
On Dec 28, 4:38 pm, Csaba Gabor <dans...@gmail.comwrote:
On Dec 28, 6:21 am, The87Boy <the87...@gmail.comwrote:
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
Dec 28 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.