hoaproject / Console

The Hoa\Console library.

Home Page:https://hoa-project.net/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why the `GetOption::REQUIRED_ARGUMENT` does not throw `Exception` when an argument is missing ?

shulard opened this issue · comments

I started to write a cli script with the code :

#!/usr/bin/env php
<?php

require_once __DIR__.'/vendor/autoload.php';

$parser = new Hoa\Console\Parser();
$parser->parse(implode(' ', $argv));

$options = new Hoa\Console\GetOption(
    [
        ['long',  Hoa\Console\GetOption::REQUIRED_ARGUMENT, 'l']
    ],
    $parser
);

while(false !== $c = $options->getOption($v)) {
    switch($c) {
        case 'l':
            $long = $v;

            break;
    }
}

I specified that the long argument is required.

When I ran the script without any argument, no exception : ./script
When I ran the script with the argument but no value, exception : ./script -l

I've checked in the code and it seems that the exception is thrown only when the parameter is checked through the getOption method.

Don't you think that the GetOption::REQUIRED_ARGUMENT flag must throw an Exception when the argument is missing ?

Maybe a new flag GetOption::REQUIRED_VALUE is more suitable for the current behaviour ?

Actually, long is an option. An option has an argument, also called a value. So the current behavior is logical if we stick on the same vocabulary ;-).

Ok, you are right... But there is no way to check if required arguments are missing in the Hoa\Console package ?

Not directly by Hoa\Console. It's up to you to do that.

Ok thanks 😄