hoaproject / Console

The Hoa\Console library.
https://hoa-project.net/
366 stars 32 forks source link

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

Closed shulard closed 8 years ago

shulard commented 8 years ago

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 ?

Hywan commented 8 years ago

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

shulard commented 8 years ago

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

Hywan commented 8 years ago

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

shulard commented 8 years ago

Ok thanks :smile: