[PEAR-BUG] Req #18170 [Opn->Fbk]: Allow several options to work with the same name

[email protected]
Newsgroups php.pear.bugs
Message-ID <[email protected]>
Edit report at http://pear.php.net/bugs/bug.php?id=18170&edit=1

 ID:               18170
 Updated by:       [email protected]
 Reported By:      clicky at erebot dot net
 Summary:          Allow several options to work with the same name
-Status:           Open
+Status:           Feedback
 Type:             Feature/Change Request
 Package:          Console_CommandLine
 Operating System: Irrelevant
 Package Version:  1.1.3
 PHP Version:      5.3.3
-Assigned To:      
+Assigned To:      izi
 Roadmap Versions: 
 New Comment:

-Status:      Open
+Status:      Feedback
-Assigned To:
+Assigned To: izi
Hi there (and sorry for the very long delay),

I see your point, but IMO this will complicate the code a lot (and
probably break BC) 
for a use case that is pretty rare. Furthermore the solution you propose
is not that 
"hackish", if you can live with it, I'll close this as "Wont Fix".

Regards, 

-- 
David


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

[2011-01-03 03:00:18] looksup

Description:
------------
Currently, each you define a new option, you associate it with a name
which can be used to retrieve the option's value once parsing has taken
place.
If you try to define two options with the same "name", the second will
overwrite the first one.

I have a use-case where I want to define two options that should modify
the same variable. I think a concrete example is necessary.

My script can be run as a daemon (in the background). This is an option
that can be set in a configuration file. Command-line options can also
be used to override the behaviour defined in the configuration file (one
option forces the code to run in the background, the other one forces it
to run in the foreground).

For the time being, I managed to do that by using my own action for one
of the options, which overrides Console_CommandLine_Action::setResult()
so that the other option's value is changed. Eg. the "normal" action
does a StoreTrue on "daemon" and my action (named "no_daemon") does the
same thing as StoreFalse except that it acts on "daemon" instead of
"no_daemon".
I also had to create a class that inherits from
Console_CommandLine_Option just to override expectsArgument().
This all sounds like a big hack to me though.

Test script:
---------------
<?php
    // Run three times, using the following options:
    // $ php foo.php -d
    // $ php foo.php -n
    // $ php foo.php

    $parser = new Console_CommandLine(array(
        'name'                  => 'Foo',
        'description'           => 'Bar',
    ));

    $parser->addOption('daemon', array(
        'short_name'        => '-d',
        'long_name'         => '--daemon',
        'description'       =>  'Run in the background (daemon).',
        'action'            => 'StoreTrue',
    ));

    $parser->addOption('daemon', array(
        'short_name'        => '-n',
        'long_name'         => '--no-daemon',
        'description'       =>  'Do not run in the background.',
        'action'            => 'StoreFalse',
    ));

    try {
        $parsed = $parser->parse();
    }
    catch (Exception $exc) {
        $parser->displayError($exc->getMessage());
        exit(1);
    }
    var_dump($parsed->options);

    /*
      Here, I'd probably do something like:
      if ($parsed->options['daemon'] === NULL)
         $parsed->options['daemon'] = $config->mustRunAsDaemon();
      And then check $parsed->options['daemon'] to see if I must
      "daemonize" the code or not, regardless of where the order
      comes from.
    */
?>

Expected result:
----------------
    // $ php foo.php -d
array(1) {
  ["daemon"]=>
  bool(true)
}

    // $ php foo.php -n
array(1) {
  ["daemon"]=>
  bool(false)
}

    // $ php foo.php
array(1) {
  ["daemon"]=>
  NULL
}


Actual result:
--------------
    // $ php foo.php -d
Error: Unknown option "-d".
Type "foo.php --help" to get help.

    // $ php foo.php -n
array(1) {
  ["daemon"]=>
  bool(false)
}

    // $ php foo.php
array(1) {
  ["daemon"]=>
  NULL
}

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


-- 
Edit this bug report at http://pear.php.net/bugs/bug.php?id=18170&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.