rick645 Posted August 7, 2023 Share Posted August 7, 2023 ```` $ 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? Quote Link to comment https://forums.phpfreaks.com/topic/317160-pearconsole_commandline-third-level-verbosity/ Share on other sites More sharing options...
requinix Posted August 7, 2023 Share Posted August 7, 2023 print_r() will display a boolean true as 1... Quote Link to comment https://forums.phpfreaks.com/topic/317160-pearconsole_commandline-third-level-verbosity/#findComment-1610972 Share on other sites More sharing options...
kicken Posted August 7, 2023 Share Posted August 7, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317160-pearconsole_commandline-third-level-verbosity/#findComment-1610974 Share on other sites More sharing options...
rick645 Posted August 8, 2023 Author Share Posted August 8, 2023 $ 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 ;) Quote Link to comment https://forums.phpfreaks.com/topic/317160-pearconsole_commandline-third-level-verbosity/#findComment-1610983 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.