473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Dec 28 '07 #1
8 2485
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Chuck Anderson | last post by:
I have a function with 7 inputs. The last three have default values. I want to call that function specifying the first four, skip two and then specify the last. I thought I could write this...
9
by: Matt Eberts | last post by:
Sorry, bad title. Anyway, is there a way to pass the arguments to an object instantiated via a constructor using the arguments object and have it expanded, so to speak, so that it doesn't appear as...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
41
by: Telmo Costa | last post by:
Hi. I have the following code: -------------------------------------- function Tunnel() { //arguments(???); } function Sum() { var sum = 0; for (i=0; i<arguments.length; i++) sum +=...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
7
by: sfeher | last post by:
Hi All, Is there a way to preserve the arguments across functions? I have: <script> function myFirstFunction() { // arguments = 'param1'
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
2
ADezii
by: ADezii | last post by:
When a call is made to a Sub or Function Procedure, you can supply Arguments in the exact order they appear in the Procedure's definition, or you can supply them in any position by name. To...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.