/** * Prints out the usage message to the user. * @return void */ public function usage() { $requiredArguments = $this->filter->required(); $optionalArguments = $this->filter->optional(); $allArguments = array_merge($requiredArguments, $optionalArguments); $command = $this->parser->getCommand(); $args = array(); $num_required = count($requiredArguments); $num_optional = count($optionalArguments); echo "Usage: " . $command . " "; foreach ($allArguments as $argument) { /** @var Argument $argument */ $args[] = '[' . $argument->usageInfo() . ']'; } $args = implode(' ', $args); echo $args . "\n\n"; if ($num_required) { echo "Required Arguments:\n"; foreach ($requiredArguments as $argument) { echo $argument->usageLine(); } } if ($num_required && $num_optional) { echo "\n"; } if ($num_optional) { echo "Optional Arguments:\n"; foreach ($optionalArguments as $argument) { echo $argument->usageLine(); } } echo "\n"; }
/** * Build the option arrays that could be used with getopt() * * @return array */ private function buildOptions() { $short_prefixes = $this->filter->withShortPrefix(); $long_prefixes = $this->filter->withLongPrefix(); $short = ''; $long = array(); foreach ($short_prefixes as $argument) { $short .= $argument->prefix; $short .= $argument->required == true ? ':' : '::'; } foreach ($long_prefixes as $argument) { $rule = $argument->longPrefix; $rule .= $argument->required == true ? ':' : '::'; $long[] = $rule; } return [$short, $long]; }