You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to set the default value for a ->file() option based on the value of another option. This appears to work only if the ->file() option is the last one defined. Examples:
#!/usr/bin/env php<?phprequire_once'vendor/autoload.php';
$cmd = newCommando\Command();
$cmd->option()
->require()
->describedAs('Output directory.');
$cmd->option('t')
->aka('type')
->describedAs('Type of thing to process.');
// Hard-coded value for default() works.$cmd->option('c')
->aka('connfigfile')
->describedAs('The config file to use.')
->file()
->default('configs/foo.yml');
$cmd->option('n')
->aka('number')
->describedAs('The number of things to process.');
Running ./file_test.php -n 10 -t foo /tmp does not produce an error. But if set the default value of the ->file() option based on the value of another option:
#!/usr/bin/env php<?phprequire_once'vendor/autoload.php';
$cmd = newCommando\Command();
$cmd->option()
->require()
->describedAs('Output directory.');
$cmd->option('t')
->aka('type')
->describedAs('Type of thing to process.');
// Dynamically generated value for default() produces error.$cmd->option('c')
->aka('connfigfile')
->describedAs('The config file to use.')
->file()
->default('configs/' . $cmd['type'] . '.yml');
// Now this option is not recognized.$cmd->option('n')
->aka('number')
->describedAs('The number of things to process.');
However, if the ->file() option is the last option defined, its default value can be dynamically generated:
#!/usr/bin/env php<?phprequire_once'vendor/autoload.php';
$cmd = newCommando\Command();
$cmd->option()
->require()
->describedAs('Output directory.');
$cmd->option('t')
->aka('type')
->describedAs('Type of thing to process.');
$cmd->option('n')
->aka('number')
->describedAs('The number of things to process.');
// Dynamically generated value for default() works.$cmd->option('c')
->aka('connfigfile')
->describedAs('The config file to use.')
->file()
->default('configs/' . $cmd['type'] . '.yml');
Running ./file_test.php -n 10 -t foo /tmp does not produce an error.
Is this expected behavior, or am I missing something obvious?
The text was updated successfully, but these errors were encountered:
When you access the command values it actually parses the argument list, and sets the values for every option. At this time there is a flag preventing it from re-parsing after the first run. If your "c" option is not last than it is causing the argument list to be parsed before you create the rest of your options and define their features.
there may be a viable option to work around this, but I don't know if there is any plan to implement it.
I want to set the default value for a
->file()
option based on the value of another option. This appears to work only if the->file()
option is the last one defined. Examples:Running
./file_test.php -n 10 -t foo /tmp
does not produce an error. But if set the default value of the->file()
option based on the value of another option:running
./file_test.php -n 10 -t foo /tmp
producesERROR: Unknown option, n, specified
.However, if the
->file()
option is the last option defined, its default value can be dynamically generated:Running
./file_test.php -n 10 -t foo /tmp
does not produce an error.Is this expected behavior, or am I missing something obvious?
The text was updated successfully, but these errors were encountered: