473,800 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2506
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...@gma il.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...@gma il.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...@gma il.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
2442
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 as : $retval = myfunction(arg1, arg2, arg3, arg4,,,arg7); .... but Php does not seem to want to let me do this. I get a parse
9
3812
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 a single argument? I'm sorry, this explanation is just atrocious, but I can't think of exactly how to word it. Maybe an example... Take for instance Function.apply. It takes 1-2 arguments, the first being the object to use as the context, and...
6
4025
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 from a user's program and use a C backend to evaluate the tree. I'm using Lex, Yacc and C. Now, there are some functions that are built into this language and others which are not. The problem I'm having is that I haven't found a way to represent...
21
4137
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
2586
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 += arguments;
9
16848
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 type='text/javascript'> function myFunc(lev) { // if (lev) return myFunc(lev-1); var aStack=; nextFunc = arguments.callee;
7
1942
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
2733
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 on it's own. However when I call the child object I get an error " has no properties (in firefox)." I simple test:
7
3227
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 bastard so posting it before I forgot again... By taking this minimum code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head>
2
12557
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 illustrate this point, I'll use a fictitious Function called fCalculateInterest() which accepts 3 Arguments (Currency, Single, and Long) and returns a value of type Currency. Public Function fCalculateInterest(curPrincipal As Currency, sngRate As Single,...
0
9695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10514
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10287
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10260
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10042
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.