// Bail on old versions of PHP, or if composer has not been run yet to install
// dependencies. Using dirname( __FILE__ ) here because __DIR__ is PHP5.3+.
+// @codingStandardsIgnoreStart MediaWiki.Usage.DirUsage.FunctionFound
require_once dirname( __FILE__ ) . '/../includes/PHPVersionCheck.php';
+// @codingStandardsIgnoreEnd
wfEntryPointCheck( 'cli' );
/**
*/
private $config;
+ /**
+ * Used to read the options in the order
+ * they were passed. Useful for option
+ * chaining. (Ex. dumpBackup.php)
+ *
+ * This is an array of arrays where
+ * 0 => the option and 1 => parameter value
+ *
+ * @var array
+ */
+ public $orderedOptions = array();
+
/**
* Default constructor. Children should call this *first* if implementing
* their own constructors
* @param bool $required Is the param required?
* @param bool $withArg Is an argument required with this option?
* @param string $shortName Character to use as short name
+ * @param bool $multiOccurrence Can this option be passed multiple times?
*/
protected function addOption( $name, $description, $required = false,
- $withArg = false, $shortName = false
+ $withArg = false, $shortName = false, $multiOccurrence = false
) {
$this->mParams[$name] = array(
'desc' => $description,
'require' => $required,
'withArg' => $withArg,
- 'shortName' => $shortName
+ 'shortName' => $shortName,
+ 'multiOccurrence' => $multiOccurrence
);
if ( $shortName !== false ) {
}
/**
- * Get an option, or return the default
+ * Get an option, or return the default.
+ *
+ * If the option was added to support multiple occurrences,
+ * this will return an array.
+ *
* @param string $name The name of the param
* @param mixed $default Anything you want, default null
* @return mixed
$options = array();
$args = array();
+ $this->orderedOptions = array();
# Parse arguments
for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
} elseif ( substr( $arg, 0, 2 ) == '--' ) {
# Long options
$option = substr( $arg, 2 );
- if ( array_key_exists( $option, $options ) ) {
- $this->error( "\nERROR: $option parameter given twice\n" );
- $this->maybeHelp( true );
- }
if ( isset( $this->mParams[$option] ) && $this->mParams[$option]['withArg'] ) {
$param = next( $argv );
if ( $param === false ) {
$this->error( "\nERROR: $option parameter needs a value after it\n" );
$this->maybeHelp( true );
}
- $options[$option] = $param;
+
+ $this->setParam( $options, $option, $param );
} else {
$bits = explode( '=', $option, 2 );
if ( count( $bits ) > 1 ) {
} else {
$param = 1;
}
- $options[$option] = $param;
+
+ $this->setParam( $options, $option, $param );
}
} elseif ( $arg == '-' ) {
# Lonely "-", often used to indicate stdin or stdout.
if ( !isset( $this->mParams[$option] ) && isset( $this->mShortParamsMap[$option] ) ) {
$option = $this->mShortParamsMap[$option];
}
- if ( array_key_exists( $option, $options ) ) {
- $this->error( "\nERROR: $option parameter given twice\n" );
- $this->maybeHelp( true );
- }
+
if ( isset( $this->mParams[$option]['withArg'] ) && $this->mParams[$option]['withArg'] ) {
$param = next( $argv );
if ( $param === false ) {
$this->error( "\nERROR: $option parameter needs a value after it\n" );
$this->maybeHelp( true );
}
- $options[$option] = $param;
+ $this->setParam( $options, $option, $param );
} else {
- $options[$option] = 1;
+ $this->setParam( $options, $option, 1 );
}
}
} else {
$this->mInputLoaded = true;
}
+ /**
+ * Helper function used solely by loadParamsAndArgs
+ * to prevent code duplication
+ *
+ * This sets the param in the options array based on
+ * whether or not it can be specified multiple times.
+ *
+ * @since 1.27
+ * @param array $options
+ * @param string $option
+ * @param mixed $value
+ */
+ private function setParam( &$options, $option, $value ) {
+ $this->orderedOptions[] = array( $option, $value );
+
+ if ( isset( $this->mParams[$option] ) ) {
+ $multi = $this->mParams[$option]['multiOccurrence'];
+ $exists = array_key_exists( $option, $options );
+ if ( $multi && $exists ) {
+ $options[$option][] = $value;
+ } elseif ( $multi ) {
+ $options[$option] = array( $value );
+ } elseif ( !$exists ) {
+ $options[$option] = $value;
+ } else {
+ $this->error( "\nERROR: $option parameter given twice\n" );
+ $this->maybeHelp( true );
+ }
+ }
+ }
+
/**
* Run some validation checks on the params, etc
*/