Unicornisaurous has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/261586

Change subject: Add support for specifying options multiple times in 
Maintenance scripts.
......................................................................

Add support for specifying options multiple times in Maintenance scripts.

Bug: T122588
Change-Id: I847d45684ccd4054f4a159394266dc3e5506bbdb
---
M maintenance/Maintenance.php
M tests/phpunit/maintenance/MaintenanceTest.php
2 files changed, 97 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/86/261586/1

diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php
index 7825ce9..5c0e22d 100644
--- a/maintenance/Maintenance.php
+++ b/maintenance/Maintenance.php
@@ -123,6 +123,14 @@
        private $config;
 
        /**
+        * Used to read the options in the order
+        * they were passed. Useful for option
+        * chaining. (Ex. dumpBackup.php)
+        * @var array
+        */
+       public $orderedOptions = array();
+
+       /**
         * Default constructor. Children should call this *first* if 
implementing
         * their own constructors
         */
@@ -186,13 +194,14 @@
         * @param string $shortName Character to use as short name
         */
        protected function addOption( $name, $description, $required = false,
-               $withArg = false, $shortName = false
+               $withArg = false, $shortName = false, $multiOccurance = false
        ) {
                $this->mParams[$name] = array(
                        'desc' => $description,
                        'require' => $required,
                        'withArg' => $withArg,
-                       'shortName' => $shortName
+                       'shortName' => $shortName,
+                       'multiOccurance' => $multiOccurance
                );
 
                if ( $shortName !== false ) {
@@ -673,6 +682,7 @@
 
                $options = array();
                $args = array();
+               $this->orderedOptions = array();
 
                # Parse arguments
                for ( $arg = reset( $argv ); $arg !== false; $arg = next( $argv 
) ) {
@@ -687,17 +697,14 @@
                        } 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 ) {
@@ -706,7 +713,8 @@
                                        } else {
                                                $param = 1;
                                        }
-                                       $options[$option] = $param;
+
+                                       $this->setParam( &$options, $option, 
$param );
                                }
                        } elseif ( $arg == '-' ) {
                                # Lonely "-", often used to indicate stdin or 
stdout.
@@ -719,19 +727,20 @@
                                        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;
+
+                                               $multi = 
$this->mParams[$option]['multiOccurance'];
+                                               $exists = array_key_exists( 
$option, $options );
+                                               $this->setParam( &$options, 
$option, $param );
+
                                        } else {
-                                               $options[$option] = 1;
+                                               $this->setParam( &$options, 
$option, 1 );
                                        }
                                }
                        } else {
@@ -746,6 +755,30 @@
        }
 
        /**
+        * 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.
+        */
+       private function setParam( &$options, $option, $value ) {
+               $this->orderedOptions[] = array( $option, $value );
+
+               $multi = $this->mParams[$option]['multiOccurance'];
+               $exists = array_key_exists( $option, $options );
+               if ( $multi && $exists ) {
+                       $options[$option][] = $value;
+               } else if ( $multi ) {
+                       $options[$option] = array( $value );
+               } else if ( !$exists ) {
+                       $options[$option] = $value;
+               } else {
+                       $this->error( "\nERROR: $option parameter given 
twice\n" );
+                       $this->maybeHelp( true );
+               }
+       }
+
+       /**
         * Run some validation checks on the params, etc
         */
        protected function validateParamsAndArgs() {
diff --git a/tests/phpunit/maintenance/MaintenanceTest.php 
b/tests/phpunit/maintenance/MaintenanceTest.php
index 5c6a6cd..e0c5257 100644
--- a/tests/phpunit/maintenance/MaintenanceTest.php
+++ b/tests/phpunit/maintenance/MaintenanceTest.php
@@ -120,6 +120,16 @@
                return call_user_func_array( array( "parent", __FUNCTION__ ), 
func_get_args() );
        }
 
+       public function addOption( $name, $description, $required = false,
+               $withArg = false, $shortName = false, $multiOccurance = false
+       ) {
+               return call_user_func_array( array( "parent", __FUNCTION__ ), 
func_get_args() );
+       }
+
+       public function getOption( $name, $default = null ) {
+               return call_user_func_array( array( "parent", __FUNCTION__ ), 
func_get_args() );
+       }
+
        // --- Requirements for getting instance of abstract class
 
        public function execute() {
@@ -829,4 +839,44 @@
                $this->m->setConfig( $conf );
                $this->assertSame( $conf, $this->m->getConfig() );
        }
+
+       function testParseArgs() {
+               global $argv;
+               $oldArgv = $argv;
+
+               $argv = array( '', '--multi', 'this1', '--multi', 'this2' );
+               $m2 = new MaintenanceFixup( $this );
+               // Create an option with an argument allowed to be specified 
multiple times
+               $m2->addOption( 'multi', 'This option does stuff', false, true, 
false, true );
+               $m2->loadParamsAndArgs();
+
+               $this->assertEquals( array( 'this1', 'this2' ), $m2->getOption( 
'multi' ) );
+               $this->assertEquals( array( array( 'multi', 'this1'), array( 
'multi', 'this2' ) ), $m2->orderedOptions );
+               $m2->simulateShutdown();
+
+
+               $argv = array( '', '--multi', '--multi' );
+               $m2 = new MaintenanceFixup( $this );
+
+               $m2->addOption( 'multi', 'This option does stuff', false, 
false, false, true );
+               $m2->loadParamsAndArgs();
+
+               $this->assertEquals( array( 1, 1 ), $m2->getOption( 'multi' ) );
+               $this->assertEquals( array( array( 'multi', 1), array( 'multi', 
1) ), $m2->orderedOptions );
+
+               $m2->simulateShutdown();
+
+               $argv = array( '', '--multi=yo' );
+               $m2 = new MaintenanceFixup( $this );
+               // Create an option with an argument allowed to be specified 
multiple times
+               $m2->addOption( 'multi', 'This option doesn\'t actually support 
multiple occurances' );
+               $m2->loadParamsAndArgs();
+
+               $this->assertEquals( 'yo', $m2->getOption( 'multi' ) );
+               $this->assertEquals( array( array( 'multi', 'yo') ), 
$m2->orderedOptions );
+
+               $m2->simulateShutdown();
+
+               $argv = $oldArgv;
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/261586
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I847d45684ccd4054f4a159394266dc3e5506bbdb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Unicornisaurous <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to