Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.4.0

Request #2428 Parameters: where are they ?
Submitted: 2004-10-01 10:00 UTC
From: toggg Assigned: scottmattocks
Status: Closed Package: Console_Getargs
PHP Version: 4.3.8 OS: FC2
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 34 - 25 = ?

 
 [2004-10-01 10:00 UTC] toggg
Description: ------------ Getargs is great ! But one thing is puzzling me: What is the "parameters" (as defined in man getopt) ? eg. Unix diff accepts options as getargs can handle but NEEDS 2 parameters (directs, not over option) the 2 file to compare names. I should be able to say: php -q diff.php -b file1 file2 Perhaps such thing allready exists, but I couldn't pick it... Else, one suggestion could be, in order to keep up the compatibility, to introduce an empty indexed option like: ... // Emtpty option deals for the parameters '' => array('short' => 'diff', 'min' => 2, 'max' => 2, 'desc' => 'PHP clone of the Unix diff command'), ... OK, another question arises then: can I insert the parameters among options ? Say after an option without argument. ("parameter shuffling" from man getopt) Thanks for taking over the maintenance ! Bye ! bertrand Gugger

Comments

 [2004-10-01 13:00 UTC] scottmattocks
I just want to clarify what you are asking for. You want anything on the command line that cannot be directly assigned to an option to be added as one of some set of default arguments? Currently, to pass the verbose level of 5 and two files to a script you do this: <code> $ php -q example.php --verbose 5 --files file1 file2 </code> You want to be able to have anything that isn't assigned to something else default to files? Like this: <code> $ php -q example.php --verbose 5 file1 file2 </code> or <code> $ php -q example.php file1 file2 --verbose 5 </code> or even <code> $ php -q example.php file1 --verbose 5 file2 </code> And then with all four of the examples above you would expect $args->getValue('files') to produce an array like this: array(file1, file2) Please let me know if I have the correct understanding. Thank you, Scott Mattocks
 [2004-10-01 13:50 UTC] bmansion at mamasam dot com
He maybe wants something like "implied" arguments ?
 [2004-10-01 15:02 UTC] toggg
I really mean what is current as usual OS commands Here is an example: I want to have a PHP script acting the same as Unix diff. (see diff.php underneath) # 1st try (no option): $> php -q diff.php src.tiff dest.tiff Console_Getargs Diff Example Usage: diff.php [options] -b --blank Switch to ignore blanks. Unknown argument src.tiff # 2nd try (blank option): $> php -q diff.php -b src.tiff dest.tiff Console_Getargs Diff Example Usage: diff.php [options] -b --blank Switch to ignore blanks. Argument blank does not take any value ************************** Here is the diff.php file: ************************** <?php /** * Diff Example for Console_Getargs class * * $Id: diff.php derived from example.php */ require_once('Console/Getargs.php'); $config = array( // Option is a switch 'blank' => array('short' => 'b', 'max' => 0, 'desc' => 'Switch to ignore blanks.'), ); $args =& Console_Getargs::factory($config); if (PEAR::isError($args)) { $header = "Console_Getargs Diff Example\n". //BG: $_SERVER['SCRIPT_NAME'] not allways set better use __FILE__ 'Usage: '.basename(__FILE__)." [options]\n\n"; if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { echo Console_Getargs::getHelp($config, $header, $args->getMessage())."\n"; } else if ($args->getCode() === CONSOLE_GETARGS_HELP) { echo Console_Getargs::getHelp($config, $header)."\n"; } exit; } // here handle file1 & file2 (diff) according to options ?> ****************** bye. bertrand
 [2004-10-06 12:26 UTC] scott at crisscott dot com
I committed a fix for this to CVS yesterday. Before I close out the bug I would appreciate it if someone could run a few tests and let me know if they have any problems. The way I implemented this is to try and assign any arguments that can't be assigned to another option to the option named parameters. It is a regular option just like any other but it doesn't need to be named specifically in the command line. For example, if diff.php defines the option parameters to take exactly two arguments you would run this: $ php -q diff.php file1 file2 If you didn't define the parameters option, that command would give you the error that file1 is an unknown argument just like it would in the previous version. If I don't hear anything by tomorrow (Oct. 7, 2004) I will assume there are no problems and close out the bug. Thanks, Scott Mattocks
 [2004-10-06 12:41 UTC] toggg
hello Scott! I took your 1.4 from CVS this morning, seems not to work: (the last test you suggest: # Debug, set verbose level 5, pass two files, and pass two application parameters php -q example.php -dv5 -f src.tiff dest.tiff fooX foo2 Verbose: 5 Formats: jpegbig, jpegsmall Files: src.tiff, dest.tiff Filters: undefined Width: Debug: YES Parameters: foo2 I don't get the parameters fooX ? I'm perhaps wrong in my test... regards bertrand (Gugger)
 [2004-10-06 12:58 UTC] bmansion at mamasam dot com
I suggest that instead of using the keyword 'parameters', you use no key like so: $config = array( array('min' => 0, 'max' => 2, 'desc' => 'File Path', 'default' => 'foo1') ); Then you will probably have to rework the way the help text is generated :) This information could be made available like this: Usage: myscript [options] [File Path] [File Path] or easier : Usage: myscript [options] File Path... What you think ?
 [2004-10-06 13:07 UTC] scott at crisscott dot com
If you don't have any option name then how would you get the value back? Would $args->getValue() return the "parameters" by default? Plus without an option name you can't really write: Usage: myscript [options] File Path... because you don't know if they are file paths. I agree that a keyword is not the best solution. Maybe the "parameters" option could be set in the config array? Scott
 [2004-10-06 13:17 UTC] toggg
OK, we need to have a keyword : (or we land on some mixed array associative or not) Suggestions: 1) 'parameters' is not so fine, because no more option named 'parameters' 2) '' is not so fine, because no more option with only short definition even ' ' or ' '... 3) '-' that cannot be a real option! it's perhaps nice, but must be skipped in the function buildMaps() as no real option What you mean ? bertrand Gugger
 [2004-10-12 12:22 UTC] scott at crisscott dot com
It took me a while but I finally tracked down the unexpected behavior experience with the example script. The problem is with the way the option is set up. The "parameters" option is set up as a default-if-set option which is not what I had in mind. I wanted an option that would take zero, one or two options. The problem is that default-if-set options, which are defined by a max >= 1 and a min == zero, don't behave quite right. The default-if-set options are skipping the first argument for some reason and there is no loop in the code for adding multiple arguments to this type of option. If you switch the example script to have min == 1 or min == 2 for the parameters option, you will see the results you would normally expect. I think I inadvertantly tripped over a new bug. If someone can test this out and confirm it, I think we can close this feature request and open a new bug. Scott Mattocks
 [2004-10-14 20:23 UTC] scottmattocks
I have heard no oposition to my last comment so I am closing this feature request. The exact implementation details may not have been agreed upon (ex: the name of the default option) but there is a working version in CVS so I think it is fair to consider this closed. After I have more information about what is going on with the default-if-set problem I will open a new bug for it. Thanks for everyone's help. Scott Mattocks