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

Bug #16914 Password action doesn't work as expected
Submitted: 2009-12-17 21:27 UTC
From: izi Assigned: rquadling
Status: Closed Package: Console_CommandLine (version CVS)
PHP Version: Irrelevant OS: linux
Roadmaps: (Not assigned)    

 [2009-12-17 21:27 UTC] izi (David Jean Louis)
Description: ------------ When the password option is the last or that it is followed by a subcommand, the password prompt doesn't show up, leaving the option value empty. It *works* adding the stop flag "--" after, but it should also work without it. This is because of the "return" statement in Console/CommandLine.php on line 1070, but the fix is not so obvious... (Richard, if you have some time, could you look at this ?) Test script: --------------- <?php require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(); $parser->addOption('username', array( 'short_name' => '-u', 'long_name' => '--username', 'description' => 'Your username', 'action' => 'StoreString', )); $parser->addOption('password', array( 'short_name' => '-p', 'long_name' => '--password', 'description' => 'Your password', 'action' => 'Password', )); $parser->addCommand('foo', array( 'description' => 'Foo...', )); try { $result = $parser->parse(); echo $result->options['username'] . '/' . $result->options['password'] . "\n"; } catch (Exception $e) { $parser->displayError($e->getMessage()); } ?> Expected result: ---------------- $ php testcase.php -psecret -uusername username/secret $ php testcase.php -p -uusername password: username/secret $ php testcase.php -u username -p password: username/secret $ php testcase.php -u username -p foo username/secret Actual result: -------------- $ php testcase.php -psecret -uusername username/secret <- OK $ php testcase.php -p -uusername password: username/secret <- OK $ php testcase.php -u username -p username/ <- NOT OK $ php testcase.php -u username -p foo username/ <- NOT OK


 [2009-12-17 23:09 UTC] rquadling (Richard Quadling)
A super quick hack would be to append -- to the array returned by CommandLine::getArgcArgv() At least until a better workaround is thought of. Index: Console/CommandLine.php =================================================================== --- Console/CommandLine.php (revision 292191) +++ Console/CommandLine.php (working copy) @@ -1179,13 +1179,17 @@ } } } + $argv[] = '--'; return array(count($argv), $argv); } if (isset($argc) && isset($argv)) { // case of register_argv_argc = 1 + $argv[] = '--'; return array($argc, $argv); } if (isset($_SERVER['argc']) && isset($_SERVER['argv'])) { + $_SERVER['argv'][] = '--'; + ++$_SERVER['argc']; return array($_SERVER['argc'], $_SERVER['argv']); } return array(0, array());
 [2009-12-17 23:32 UTC] izi (David Jean Louis)
Richard, Good idea, but I tested quickly the patch and it breaks some tests (need to find out why...), and imho we should test if '--' is not already present in the argv array (eg. if (!in_array('--', $argv)) ...). -- David
 [2010-03-25 19:41 UTC] rquadling (Richard Quadling)
I think the bug relates to the value of $argc. I suspect that the $this (Console_CommandLine) needs to know the number of parameters for $this parse. e.g. -u username -p foo is actually -u username -p ($this with argc=3) and foo ($this->subcommand with an argc of 1) Not exactly, yet, sure how to implement this. I think ... if line 954... $last = $argc === 0; was something like ... $last = ($argc === 0 || the next token is a subcommand); then that would work just fine. Trying to work that out...
 [2010-03-25 20:56 UTC] rquadling (Richard Quadling)
I've got it working. After all the tokens have been parsed, parse a null token. When parsing a null token, don't save it in $args. And only dispatchactions if the token is not null OR the action is Password. All tests pass and so do your tests. It is a bit hacky though. Richard. P.S. Patch incoming. If you like I will commit.
 [2010-03-25 21:04 UTC] rquadling (Richard Quadling)
 [2010-03-27 03:19 UTC] izi (David Jean Louis)
Hi Richard, the patch looks ok, if all tests pass and the bug is fixed then commit away ! thanks, -- David
 [2010-03-29 14:41 UTC] rquadling (Richard Quadling)
Committed patch. Not sure what needs to change in package.xml nor how to close this bug - don't think I can.
 [2010-04-10 13:55 UTC] izi (David Jean Louis)
-Status: Open +Status: Closed -Assigned To: +Assigned To: rquadling
Hey Richard, I've closed the bug and released a 1.1.2 version. I've also changed your karma to "co-lead" dev on Console_CommandLine, so you'll be able to do everything next time (close bugs, release etc...). Regards, -- David