// This is the list of arguments that were actually passed
protected $mArgs = [];
+ // Allow arbitrary options to be passed, or only specified ones?
+ protected $mAllowUnregisteredOptions = false;
+
// Name of the script currently running
protected $mSelf;
*/
abstract public function execute();
+ /**
+ * Checks to see if a particular option in supported. Normally this means it
+ * has been registered by the script via addOption.
+ * @param string $name The name of the option
+ * @return bool true if the option exists, false otherwise
+ */
+ protected function supportsOption( $name ) {
+ return isset( $this->mParams[$name] );
+ }
+
/**
* Add a parameter to the script. Will be displayed on --help
* with the associated description
}
/**
- * Checks to see if a particular param exists.
- * @param string $name The name of the param
+ * Checks to see if a particular option exists.
+ * @param string $name The name of the option
* @return bool
*/
protected function hasOption( $name ) {
unset( $this->mParams[$name] );
}
+ /**
+ * Sets whether to allow unregistered options, which are options passed to
+ * a script that do not match an expected parameter.
+ * @param bool $allow Should we allow?
+ */
+ protected function setAllowUnregisteredOptions( $allow ) {
+ $this->mAllowUnregisteredOptions = $allow;
+ }
+
/**
* Set the description text.
* @param string $text The text of the description
$die = true;
}
}
+ if ( !$this->mAllowUnregisteredOptions ) {
+ # Check for unexpected options
+ foreach ( $this->mOptions as $opt => $val ) {
+ if ( !$this->supportsOption( $opt ) ) {
+ $this->error( "Unexpected option $opt!" );
+ $die = true;
+ }
+ }
+ }
if ( $die ) {
$this->maybeHelp( true );
require_once __DIR__ . '/Maintenance.php';
// phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
-global $optionsWithArgs, $optionsWithoutArgs;
+global $optionsWithArgs, $optionsWithoutArgs, $allowUnregisteredOptions;
if ( !isset( $optionsWithArgs ) ) {
$optionsWithArgs = [];
if ( !isset( $optionsWithoutArgs ) ) {
$optionsWithoutArgs = [];
}
+if ( !isset( $allowUnregisteredOptions ) ) {
+ $allowUnregisteredOptions = false;
+}
class CommandLineInc extends Maintenance {
public function __construct() {
// phpcs:ignore MediaWiki.NamingConventions.ValidGlobalName.wgPrefix
- global $optionsWithArgs, $optionsWithoutArgs;
+ global $optionsWithArgs, $optionsWithoutArgs, $allowUnregisteredOptions;
parent::__construct();
+
foreach ( $optionsWithArgs as $name ) {
$this->addOption( $name, '', false, true );
}
foreach ( $optionsWithoutArgs as $name ) {
$this->addOption( $name, '', false, false );
}
+
+ $this->setAllowUnregisteredOptions( $allowUnregisteredOptions );
}
/**