Example #1
0
 private function init($args, UserInterface $ui)
 {
     $flags = new Flags();
     $displayHelp =& $flags->bool('help', false, 'Display this help message.');
     $displayVersion =& $flags->bool('version', false, 'Display this applications version.');
     try {
         $flags->parse($args);
     } catch (\Exception $e) {
         $ui->dropError($e->getMessage(), 1, $flags->getDefaults());
         drop($e->getMessage(), 1, $flags->getDefaults());
     }
     switch (true) {
         case $displayVersion:
             self::versionMarker($ui);
             die(0);
             break;
         case $displayHelp:
         case count($flags->args()) > 1:
             $ui->dumpOptions($flags->getDefaults());
             die(1);
             break;
     }
     return $flags->args() ? current($flags->args()) : self::CONFIG_FILE;
 }
Example #2
0
 public function testGetDefaults()
 {
     $flags = new Flags();
     $longs = array(array('bool', 'foo', false, 'Enable the foos'), array('string', 'bar', 'string', 'Baz text to display'), array('int', 'baz', -10, 'How many bazs'), array('uint', 'quux', 10, 'How many quuxi'), array('float', 'thud', 10.1, 'How many thuds'), array('bool', 'xfoo', null, 'Enable the foos'), array('string', 'xbar', null, 'Baz text to display'), array('int', 'xbaz', null, 'How many bazs'), array('uint', 'xquux', null, 'How many quuxi'), array('float', 'xthud', null, 'How many thuds'));
     foreach ($longs as $data) {
         $flags->{$data[0]}($data[1], $data[2], $data[3]);
     }
     $shorts = array(array('v', 'verbosity, more v\'s = more verbose'), array('a', 'verbosity, more v\'s = more verbose'));
     foreach ($shorts as $data) {
         $flags->short($data[0], $data[1]);
     }
     $longCount = 0;
     $shortCount = 0;
     preg_match_all('/^\\s*(?P<long>-)?-(?P<key>[a-z]+)\\s+(?:(?P<optional>\\[[a-z]+\\])|(?P<required><[a-z]+>))?\\s+(?P<msg>.*)/m', $flags->getDefaults(), $result, PREG_PATTERN_ORDER);
     for ($i = 0; $i < count($result[0]); $i += 1) {
         if ($result['long'][$i] === '-') {
             $longCount++;
             foreach ($longs as $long) {
                 if ($long[1] == $result['key'][$i]) {
                     if ($long[2] !== null) {
                         $this->assertTrue(isset($result['optional'][$i][0]) || $long[0] == 'bool');
                     } else {
                         $this->assertTrue(isset($result['required'][$i][0]) || $long[0] == 'bool');
                     }
                     $this->assertEquals($long[3], $result['msg'][$i]);
                     break;
                 }
             }
         } else {
             $shortCount++;
             foreach ($shorts as $short) {
                 if ($short[0] == $result['key'][$i]) {
                     $this->assertEquals($short[1], $result['msg'][$i]);
                     break;
                 }
             }
         }
     }
     $this->assertEquals(count($longs), $longCount);
     $this->assertEquals(count($shorts), $shortCount);
 }