Jump to content

Recommended Posts

````
$ cat composer.json
{
    "require": {
        "pear/console_commandline": "^1.2"
    }
}

$ cat main.php
<?php

require_once 'vendor/autoload.php';

$parser = new Console_CommandLine;

$parser->addOption('verbose', array(
    'multiple'  => true,
    'short_name'  => '-v',
    'long_name'   => '--verbose',
    'action'      => 'StoreTrue',
));

try {
    $result = $parser->parse();
    print_r($result->options);
} catch (Exception $exc) {
    $parser->displayError($exc->getMessage());
}

$ php main.php -v | grep verbose
    [verbose] => 1
````

OK, but...

````
$ php main.php -vvv | grep verbose
    [verbose] => 1
````

I would have expected (third level verbosity)

````
    [verbose] => 3
````

Can you do something to solve?

8 hours ago, rick645 said:

I would have expected (third level verbosity)

Why?  You set the action as StoreTrue, which if you look at the documentation,  

Quote

tells the parser to store the value true in the result object if the option is present in the command line

Adding the flag multiple times just stores the value true multiple times.  There's no such thing as "more true" and "even more true".

If you keep reading that documentation, you'll find there's a different action type Counter which is what you want.  It is documented as:

Quote

This action tells the parser to increment the value in the result object each time it encounters the option in the command line, for example:

The shown example is exactly what you are trying to do.

 

$ cat main.php 
<?php
require_once 'vendor/autoload.php';

$parser = new Console_CommandLine;
$parser->addOption('verbose', [
    'short_name'  => '-v',
    'long_name'   => '--verbose',
    'action'      => 'Counter',
]);

$result = $parser->parse();
var_dump($result->options['verbose']);

$ php main.php -vv --verbose
int(3)
$ php main.php 
NULL


Very good!!!
Thanks ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.