Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values for options #21

Closed
kfriend opened this issue Jun 18, 2016 · 4 comments
Closed

Default values for options #21

kfriend opened this issue Jun 18, 2016 · 4 comments

Comments

@kfriend
Copy link

kfriend commented Jun 18, 2016

I cannot seem to supply a default value to an option which accepts a value. Let me know if I'm misunderstanding the command definition API.

I've tried within the anonymous function's parameter definition:

$cli->command('foo [--bar=]', function($bar = 'baz') {
    var_dump($bar); // => NULL, but would expect 'baz'
});

And also with the ->defaults() method:

$cli->command('foo [--bar=]', function($bar = 'baz') {
    // ... 
})->defaults([
    // None of these work, resulting in an exception. 
    // Showing all for brevity, but I've tried each individually.
    'bar' => 'baz',
    '--bar' => 'baz',
    'bar=' => 'baz',
    '--bar=' => 'baz',
    '[bar]' => 'baz',
    '[--bar]' => 'baz',
    '[bar=]' => 'baz',
    '[--bar=]' => 'baz',
]);

PHP 7.0.7, OS X 10.11.3, silly v1.3.0

Thanks!

@kfriend
Copy link
Author

kfriend commented Jun 18, 2016

I'm invoking the CLI application with php cliapp foo, i.e. without the --bar=val option.

@mnapoli mnapoli added the bug label Jun 19, 2016
@mnapoli
Copy link
Owner

mnapoli commented Jun 19, 2016

Thanks for the report. I had a closer look and it's possible to set default values only for arguments (not for options): https://github.com/mnapoli/silly/blob/master/src/Command/Command.php#L36

At first I thought it was a bug but it's simply not implemented. Maybe we could add support for that, the question is what would be the best way for it?

$cli->command('foo [--bar=]', function($bar) {
    // ... 
})->defaults([
    '--bar' => 'baz',
]);

maybe? (to differentiate from argument default values) Or we could also maybe have a look at the default value of the closure parameter, e.g.

$cli->command('foo [--bar=]', function($bar = 'baz') {
    // ... 
});

That could maybe work for arguments too, I don't remember if there was a reason why we didn't go this way at all for arguments default values.

@mnapoli mnapoli added enhancement and removed bug labels Jun 19, 2016
@mnapoli mnapoli changed the title [Question] Default value for an option? Default values for options Jun 19, 2016
@kfriend
Copy link
Author

kfriend commented Jun 20, 2016

Thanks for the reply @mnapoli. I think your suggestion of '--bar' => 'baz' would work well, since it would differentiate from arguments, as you mentioned, but also matches the description() method's syntax for options.

I haven't done much with the Reflection API, or PHP-DI, so I don't know of any technical limitations why using an argument's default value would not work, i.e. function($bar = 'baz'), but it would be a nice way to default options, flags, and arguments, vs defaults().

@mnapoli
Copy link
Owner

mnapoli commented Aug 31, 2016

Fixed in #25

@mnapoli mnapoli closed this as completed Aug 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants