diff --git "a/include/class-cli\\Opts.php" "b/include/class-cli\\Opts.php" new file mode 100644 index 0000000..c337c08 --- /dev/null +++ "b/include/class-cli\\Opts.php" @@ -0,0 +1,161 @@ +. + +# command line interface +namespace cli; + +/** + * Register command line parameters and read their arguments + */ +class Opts { + + /** + * All the registered parameters + * + * @var array + */ + private $params; + + /** + * Internal flag cache + * + * @var bool + */ + private $read = false; + + /** + * Constructor + * + * @param $params array Parameters + */ + public function __construct( $params = [] ) { + foreach( $params as $param ) { + $this->add( $param ); + } + } + + /** + * Register a new parameter + * + * @param $param object Parameter + * @return self + */ + public function add( Param $param ) { + $this->params[] = $param; + $this->read = false; // invalidate the cache + return $this; + } + + /** + * Populate all parameters with their arguments + * + * @see getopt() + * @return self + */ + public function populate() { + if( ! $this->read ) { + // prepare getopt() syntax + $shorts = ''; + $longs = []; + foreach( $this->params as $opt ) { + $shorts .= $opt->getShortOptFormat(); + if( $opt->hasLongName() ) { + $longs[] = $opt->getLongOptFormat(); + } + } + + // get command line arguments + $args = getopt( $shorts, $longs ); + + // fill all parameters with their arguments + foreach( $this->params as $opt ) { + + // try with the short name + if( $opt->hasShortName() ) { + $name = $opt->getShortName(); + if( isset( $args[ $name ] ) ) { + $opt->setValue( $opt->isFlag() ? true : $args[ $name ] ); + continue; + } + } + + // try with the long name + if( $opt->hasLongName() ) { + $name = $opt->getLongName(); + if( isset( $args[ $name ] ) ) { + $opt->setValue( $opt->isFlag() ? true : $args[ $name ] ); + } + } + + } + + $this->read = true; + } + return $this; + } + + /** + * Find a registered param from a whatever name, the long or short on + * + * @param $name string Parameter name (long or short) + * @return Param + */ + protected function findParamFromName( $name ) { + foreach( $this->params as $opt ) { + if( $opt->isName( $name ) ) { + return $opt; + } + } + throw new \InvalidArgumentException( 'unregistered parameter ' . $name ); + } + + /** + * Get all the registered parameters + * + * @return array + */ + public function getParams() { + return $this->params; + } + + /** + * Get a single argument + * + * @param $name string Param name (short or long) + * @param $default string Default param value + * @return string|null + */ + public function getArg( $name, $default = null ) { + return $this->populate()->findParamFromName( $name )->getValue( $default ); + } + + /** + * Get all the unnamed arguments + * + * @return array + */ + public static function unnamedArguments() { + $args = []; + foreach( $GLOBALS[ 'argv' ] as $arg ) { + if( '-' !== $arg[ 0 ] ) { + $args[] = $arg; + } + } + array_shift( $args ); + return $args; + } +} diff --git "a/include/class-cli\\Param.php" "b/include/class-cli\\Param.php" new file mode 100644 index 0000000..65eeddb --- /dev/null +++ "b/include/class-cli\\Param.php" @@ -0,0 +1,297 @@ +. + +# command line interface +namespace cli; + +/** + * A command line parameter + * + * A difference between a 'parameter' and an argument is that a + * 'parameter' can have a value and that value is its 'argument'. + */ +class Param { + + /** + * To specify that the parameter has no any value + */ + const NO_VALUE = 1; + + /** + * To specify that the parameter has an optional value + */ + const OPTIONAL_VALUE = 2; + + /** + * To specify that the parameter has a mandatory value + */ + const REQUIRED_VALUE = 4; + + /** + * Param long name + * + * @var string|null + */ + private $longName; + + /** + * Param short name (one letter) + * + * @var string|null + */ + private $shortName; + + /** + * Can be Param::NO_VALUE|Param::OPTIONAL_VALUE|Param::REQUIRED_VALUE + * + * @var int + */ + private $valueKind = 1; + + /** + * Parameter argument + * + * @var mixed + */ + private $value; + + /** + * Description + * + * @var string|null + */ + private $description; + + /** + * Constructor + * + * @param $long_name string Long name + * @param $short_name string Short name + * @param $value_kind string + */ + public function __construct( $long_name, $short_name, $value_kind, $description ) { + $this->longName = $long_name; + $this->setShortName( $short_name ); + $this->setValueKind( $value_kind ); + $this->setDescription( $description ); + } + + /** + * Set the short name + * + * @param $short_name string|null + */ + public function setShortName( $short_name ) { + if( strlen( $short_name ) > 1 ) { + throw new \InvalidArgumentException( 'short option must be 1 characters long' ); + } + $this->shortName = $short_name; + } + + /** + * Set the value kind + * + * @param $value_kind + */ + public function setValueKind( $value_kind ) { + $value_kind = (int) $value_kind; + switch( $value_kind ) { + case self::NO_VALUE: + case self::OPTIONAL_VALUE: + case self::REQUIRED_VALUE: + $this->valueKind = $value_kind; + break; + default: + throw new \InvalidArgumentException( 'unexpected value kind' ); + } + } + + /** + * Has a long name? + * + * @return bool + */ + public function hasLongName() { + return null !== $this->getLongName(); + } + + /** + * Get the long name + * + * @return string|null + */ + public function getLongName() { + return $this->longName; + } + + /** + * Has a short name? + * + * @return bool + */ + public function hasShortName() { + return null !== $this->getShortName(); + } + + /** + * Get the short name + * + * @return string|null + */ + public function getShortName() { + return $this->shortName; + } + + /** + * Check if a name belongs to this parameter + * + * @param string $name Long or short parameter name + * @return bool + */ + public function isName( $name ) { + return $this->getLongName() === $name || $this->getShortName() === $name; + } + + /** + * Get the kind of the value + * + * @return int + */ + public function getValueKind() { + return $this->valueKind; + } + + /** + * Get the parameter's argument or a default one + * + * @param $default string Default value + * @return string|null + */ + public function getValue( $default = null ) { + return $this->value ? $this->value : $default; + } + + /** + * Set the parameter's argument + * + * @param $value string|null + */ + public function setValue( $value ) { + $this->value = $value; + } + + /** + * Check if the description exists + * + * @return bool + */ + public function hasDescription() { + return null !== $this->getDescription(); + } + + /** + * Get the parameter's argument + * + * @param $description string + */ + public function getDescription() { + return $this->description; + } + + /** + * Set the parameter's argument + * + * @param $description string + */ + public function setDescription( $description ) { + $this->description = $description; + } + + /** + * Is the value of a certain kind? + * + * @param $kind int + * @return bool + */ + protected function isValueKind( $kind ) { + return $this->getValueKind() === $kind; + } + + /** + * Is the value optional? + * + * @return bool + */ + public function isValueOptional() { + return $this->isValueKind( self::OPTIONAL_VALUE ); + } + + /** + * Is the value required? + * + * @return bool + */ + public function isValueRequired() { + return $this->isValueKind( self::REQUIRED_VALUE ); + } + + /** + * Check if it can't have a value + * + * @return bool + */ + public function isFlag() { + return $this->isValueKind( self::NO_VALUE ); + } + + /** + * Get the long option format + * + * @return string + */ + public function getLongOptFormat() { + $v = ''; + if( $this->hasLongName() ) { + $v = $this->getLongName(); + if( $this->isValueKind( self::OPTIONAL_VALUE ) ) { + $v .= ':'; + } elseif( $this->isValueKind( self::REQUIRED_VALUE ) ) { + $v .= '::'; + } + } + return $v; + } + + /** + * Get the short option format + * + * @return string + */ + public function getShortOptFormat() { + $v = ''; + if( $this->hasShortName() ) { + $v = $this->getShortName(); + if( $this->isValueKind( self::OPTIONAL_VALUE ) ) { + $v .= ':'; + } elseif( $this->isValueKind( self::REQUIRED_VALUE ) ) { + $v .= '::'; + } + } + return $v; + } + +}