addOption(['h', 'help', GetOpt::NO_ARGUMENT, 'Show this help and quit']); foreach ($argumentNames as $argumentName => $argumentType) { $normalizedOptions[$argumentName] = null; // Adds an option for an argument using a long option name only. $getOpt->addOption( [ null, $argumentName, $argumentType, ArgumentNames::$ARGUMENTS_TO_DESCRIPTIONS[$argumentName] ] ); if ($argumentType === GetOpt::REQUIRED_ARGUMENT) { $numRequiredArguments++; } } // Parse arguments and catch exceptions. try { $getOpt->process(); } catch (ArgumentException $exception) { // When there are any errors regarding arguments, such as invalid argument names, or // specifying required arguments but not providing values, 'ArgumentException' will // be thrown. Show the help text in these cases. echo PHP_EOL . $getOpt->getHelpText(); throw $exception; } // Show help text when requested. if (!is_null($getOpt->getOption('help'))) { $this->printHelpMessageAndExit($getOpt); // Help text is printed, so no arguments are passed. The below line is reached only // in tests. return []; } $numPassedRequiredArguments = 0; foreach ($getOpt->getOptions() as $optionName => $optionValue) { if ($argumentNames[$optionName] === GetOpt::REQUIRED_ARGUMENT) { $numPassedRequiredArguments++; } $normalizedOptions[$optionName] = $optionValue; } // Don't allow the case when optional arguments are passed, but required arguments are not. if ( count($getOpt->getOptions()) > 0 && $numPassedRequiredArguments !== $numRequiredArguments ) { echo PHP_EOL . $getOpt->getHelpText(); throw new InvalidArgumentException( 'All required arguments must be specified.' . PHP_EOL ); } return $normalizedOptions; } /** * Print the help message and exit the program. * * @param GetOpt $getOpt the GetOpt object to print its help text */ public function printHelpMessageAndExit(GetOpt $getOpt) { echo PHP_EOL . $getOpt->getHelpText(); exit; } }