Option uses ConstantsTrait
Abstract base class that maps public property names to command-line option names.
This class is used by Options::getOptions() to transform the keys of the public properties of a class extending Options into CLI flags. By default, names are hyphenated via hyphenate() (e.g., "dryRun" → "dry-run").
Typical customization:
- Override Option::getCommandOption() to customize the option name.
- Override Option::getCommandPrefix() to provide a per-option prefix (e.g., "--", "-", "/opt:", etc.).
Minimal usage example with Options::getOptions():
use oihana\options\Option;
use oihana\options\Options;
class MyOption extends Option
{
// Optional: customize the name transformation
public static function getCommandOption(string $option): string
{
return parent::getCommandOption($option); // default: hyphenate
}
// Optional: set a prefix depending on the option name
public static function getCommandPrefix(string $option): ?string
{
return match ($option) {
'verbose' => '-',
default => '--',
};
}
}
class MyOptions extends Options
{
public string $host = 'localhost';
public bool $verbose = true;
}
$o = new MyOptions();
echo $o->getOptions(MyOption::class);
// → --host "localhost" -verbose
Table of Contents
Methods
- getCommandOption() : string
- Returns the option keyword derived from a property name.
- getCommandPrefix() : string|null
- Returns the prefix to use for a given option.
Methods
getCommandOption()
Returns the option keyword derived from a property name.
public
static getCommandOption(string $option) : string
Default implementation: hyphenate() which converts to kebab-case (e.g., "dryRun" → "dry-run").
By overriding this method in a subclass, you can apply a different format depending on the option name.
Parameters
- $option : string
-
Property name (e.g., "dryRun").
Return values
string —By default, transformed option name (e.g., "dry-run").
getCommandPrefix()
Returns the prefix to use for a given option.
public
static getCommandPrefix(string $option) : string|null
This value is used by Options::getOptions() and can be:
- a string (e.g., "--", "-", "/opt:");
- null to indicate not to override the prefix passed to Options::getOptions() (which can be a string or a callable).
By overriding this method in a subclass, you can apply a different prefix depending on the option name.
Parameters
- $option : string
-
Property/option name.
Return values
string|null —Prefix to use, or null to avoid overriding.