diff --git "a/include/class-cli\\Opts.php" "b/include/class-cli\\Opts.php" index c337c08..5af2132 100644 --- "a/include/class-cli\\Opts.php" +++ "b/include/class-cli\\Opts.php" @@ -1,161 +1,172 @@ . # 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 = [] ) { + $this->register( $params ); + } + + /** + * Register multiple parameters + * + * @param $params array + * @return self + */ + public function register( $params ) { foreach( $params as $param ) { $this->add( $param ); } + return $this; } /** * 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; } }