FAQ
Edit report at http://pear.php.net/bugs/bug.php?id=16914&edit=1

ID: 16914
Comment by: RQuadling@GMail.com
Reported By: izi at php dot net
Summary: Password action doesn't work as expected
Status: Open
Type: Bug
Package: Console_CommandLine
Operating System: linux
Package Version: CVS
PHP Version: Irrelevant
Roadmap Versions:
New Comment:

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...


Previous Comments:
------------------------------------------------------------------------

[2009-12-17 18:32:05] izi

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

------------------------------------------------------------------------

[2009-12-17 18:09:02] rquadling

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 16:27:38] izi

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

------------------------------------------------------------------------


--
Edit this bug report at http://pear.php.net/bugs/bug.php?id=16914&edit=1

Search Discussions

  • RQuadling at Mar 25, 2010 at 4:01 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=16914&edit=1

    ID: 16914
    Comment by: RQuadling@GMail.com
    Reported By: izi at php dot net
    Summary: Password action doesn't work as expected
    Status: Open
    Type: Bug
    Package: Console_CommandLine
    Operating System: linux
    Package Version: CVS
    PHP Version: Irrelevant
    Roadmap Versions:
    New Comment:

    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.


    Previous Comments:
    ------------------------------------------------------------------------

    [2010-03-25 15:41:59] rquadling

    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...

    ------------------------------------------------------------------------

    [2009-12-17 18:32:05] izi

    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

    ------------------------------------------------------------------------

    [2009-12-17 18:09:02] rquadling

    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 16:27:38] izi

    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

    ------------------------------------------------------------------------


    --
    Edit this bug report at http://pear.php.net/bugs/bug.php?id=16914&edit=1
  • Izi at Mar 26, 2010 at 10:24 pm
    Edit report at http://pear.php.net/bugs/bug.php?id=16914&edit=1

    ID: 16914
    Updated by: izi@php.net
    Reported By: izi at php dot net
    Summary: Password action doesn't work as expected
    Status: Open
    Type: Bug
    Package: Console_CommandLine
    Operating System: linux
    Package Version: CVS
    PHP Version: Irrelevant
    Roadmap Versions:
    New Comment:

    Hi Richard,

    the patch looks ok, if all tests pass and the bug is fixed then commit
    away !

    thanks,

    --
    David


    Previous Comments:
    ------------------------------------------------------------------------

    [2010-03-25 17:04:02] rquadling

    Added #patch
    bug:16914;patch:HandleUndespatchedActions.patch;revision:1269536642;.

    ------------------------------------------------------------------------

    [2010-03-25 16:56:58] rquadling

    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 15:41:59] rquadling

    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...

    ------------------------------------------------------------------------

    [2009-12-17 18:32:05] izi

    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

    ------------------------------------------------------------------------

    [2009-12-17 18:09:02] rquadling

    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());

    ------------------------------------------------------------------------

    The remainder of the comments for this report are too long. To view
    the rest of the comments, please view the bug report online at
    http://pear.php.net/bugs/bug.php?id=16914

    --
    Edit this bug report at http://pear.php.net/bugs/bug.php?id=16914&edit=1
  • RQuadling at Mar 29, 2010 at 9:46 am
    Edit report at http://pear.php.net/bugs/bug.php?id=16914&edit=1

    ID: 16914
    Comment by: RQuadling@GMail.com
    Reported By: izi at php dot net
    Summary: Password action doesn't work as expected
    Status: Open
    Type: Bug
    Package: Console_CommandLine
    Operating System: linux
    Package Version: CVS
    PHP Version: Irrelevant
    Roadmap Versions:
    New Comment:

    Committed patch. Not sure what needs to change in package.xml nor how
    to close this bug - don't think I can.


    Previous Comments:
    ------------------------------------------------------------------------

    [2010-03-26 23:19:41] izi

    Hi Richard,

    the patch looks ok, if all tests pass and the bug is fixed then commit
    away !

    thanks,

    --
    David

    ------------------------------------------------------------------------

    [2010-03-25 17:04:02] rquadling

    Added #patch
    bug:16914;patch:HandleUndespatchedActions.patch;revision:1269536642;.

    ------------------------------------------------------------------------

    [2010-03-25 16:56:58] rquadling

    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 15:41:59] rquadling

    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...

    ------------------------------------------------------------------------

    [2009-12-17 18:32:05] izi

    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

    ------------------------------------------------------------------------

    The remainder of the comments for this report are too long. To view
    the rest of the comments, please view the bug report online at
    http://pear.php.net/bugs/bug.php?id=16914

    --
    Edit this bug report at http://pear.php.net/bugs/bug.php?id=16914&edit=1

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppear-bugs @
categoriesphp
postedMar 25, '10 at 2:46p
activeMar 29, '10 at 9:46a
posts4
users2
websitepear.php.net

2 users in discussion

RQuadling: 3 posts Izi: 1 post

People

Translate

site design / logo © 2021 Grokbase