コード例 #1
0
ファイル: OptionTest.php プロジェクト: websharks/GetOptionKit
 public function testFilter()
 {
     $opt = new Option('scope');
     $opt->filter(function ($val) {
         return preg_replace('#a#', 'x', $val);
     });
     $opt->setValue('aa');
     is('xx', $opt->value);
 }
コード例 #2
0
 /**
  * Render readable spec
  */
 public function renderOption(Option $opt)
 {
     $c1 = '';
     if ($opt->short && $opt->long) {
         $c1 = sprintf('-%s, --%s', $opt->short, $opt->long);
     } elseif ($opt->short) {
         $c1 = sprintf('-%s', $opt->short);
     } elseif ($opt->long) {
         $c1 = sprintf('--%s', $opt->long);
     }
     $c1 .= $opt->renderValueHint();
     return $c1;
 }
コード例 #3
0
 public function renderOptionValueHint(Option $opt, $assign = true)
 {
     $n = 'value';
     if ($opt->valueName) {
         $n = $opt->valueName;
     } elseif ($opt->isa) {
         $n = $opt->isa;
     }
     if ($opt->isRequired()) {
         return sprintf('%s<%s>', $assign ? '=' : ' ', $this->formatter->format($n, 'underline'));
     } elseif ($opt->isOptional()) {
         return sprintf('%s[<%s>]', $assign ? '=' : ' ', $this->formatter->format($n, 'underline'));
     }
     return '';
 }
コード例 #4
0
ファイル: CommandTestCase.php プロジェクト: JoeHorn/phpbrew
 public function setUp()
 {
     parent::setUp();
     $this->previousPhpBrewRoot = getenv('PHPBREW_ROOT');
     $this->previousPhpBrewHome = getenv('PHPBREW_HOME');
     // <env name="PHPBREW_ROOT" value=".phpbrew"/>
     // <env name="PHPBREW_HOME" value=".phpbrew"/>
     // already setup in phpunit.xml, but it seems don't work.
     putenv('PHPBREW_ROOT=' . getcwd() . '/.phpbrew');
     putenv('PHPBREW_HOME=' . getcwd() . '/.phpbrew');
     if ($options = \PhpBrew\Console::getInstance()->options) {
         $option = new Option('no-progress');
         $option->setValue(true);
         $options->set('no-progress', $option);
     }
 }
コード例 #5
0
ファイル: OptionParser.php プロジェクト: c9s/getoptionkit
 protected function pushOptionValue(Option $spec, $arg, $next)
 {
     if ($next && !$next->anyOfOptions($this->specs)) {
         $spec->pushValue($next->arg);
     }
 }
コード例 #6
0
 /**
  * Add option object
  *
  * @param Object $spec the option object.
  */
 public function addObject(Option $spec)
 {
     $this->data[$spec->getId()] = $spec;
     if ($spec->long) {
         $this->longOptions[$spec->long] = $spec;
     }
     if ($spec->short) {
         $this->shortOptions[$spec->short] = $spec;
     }
     $this->options[] = $spec;
     if (!$spec->long && !$spec->short) {
         throw new Exception('Wrong option spec');
     }
 }
コード例 #7
0
ファイル: ZshGenerator.php プロジェクト: arunahk/CLIFramework
 /**
 *
 *
 * Generate an zsh option format like this:
    
 '(-v --invert-match)'{-v,--invert-match}'[invert match: select non-matching lines]'
 
 Or:
 
 '-gcflags[flags for 5g/6g/8g]:flags'
 '-p[number of parallel builds]:number'
 
 '--cleanup=[specify how the commit message should be cleaned up]:mode:((verbatim\:"do not change the commit message at all"
                                                                         whitespace\:"remove leading and trailing whitespace lines"
                                                                         strip\:"remove both whitespace and commentary lines"
                                                                         default\:"act as '\''strip'\'' if the message is to be edited and as '\''whitespace'\'' otherwise"))' \
 */
 public function option_flag_item(Option $opt, $cmdSignature)
 {
     // TODO: Check conflict options
     $str = "";
     $optspec = $opt->flag || $opt->optional ? '' : '=';
     $optName = $opt->long ? $opt->long : $opt->short;
     if ($opt->short && $opt->long) {
         if (!$opt->multiple) {
             $str .= "'(-" . $opt->short . " --" . $opt->long . ")'";
             // conflict options
         }
         $str .= "{-" . $opt->short . ',' . '--' . $opt->long . $optspec . "}";
         $str .= "'";
     } else {
         if ($opt->long) {
             $str .= "'--" . $opt->long . $optspec;
         } else {
             if ($opt->short) {
                 $str .= "'-" . $opt->short . $optspec;
             } else {
                 throw new Exception('undefined option type');
             }
         }
     }
     // output description
     $str .= "[" . addcslashes($opt->desc, '[]:') . "]";
     $placeholder = $opt->valueName ? $opt->valueName : $opt->isa ? $opt->isa : null;
     // has anything to complete
     if ($opt->validValues || $opt->suggestions || $opt->isa) {
         $str .= ':';
         // for the value name
         if ($placeholder) {
             $str .= $placeholder;
         }
         if ($opt->validValues || $opt->suggestions) {
             if ($opt->validValues) {
                 if (is_callable($opt->validValues)) {
                     $str .= ':{' . join(' ', array($this->meta_command_name(), Utils::qq($placeholder), $cmdSignature, 'opt', $optName, 'valid-values')) . '}';
                 } elseif ($values = $opt->getValidValues()) {
                     // not callable, generate static array
                     $str .= ':(' . join(' ', Utils::array_qq($values)) . ')';
                 }
             } elseif ($opt->suggestions) {
                 if (is_callable($opt->suggestions)) {
                     $str .= ':{' . join(' ', array($this->meta_command_name(), Utils::qq($placeholder), $cmdSignature, 'opt', $optName, 'suggestions')) . '}';
                 } elseif ($values = $opt->getSuggestions()) {
                     // not callable, generate static array
                     $str .= ':(' . join(' ', Utils::array_qq($values)) . ')';
                 }
             }
         } elseif (in_array($opt->isa, array('file', 'dir', 'path'))) {
             switch ($opt->isa) {
                 case 'file':
                     $str .= ':_files';
                     break;
                 case 'dir':
                     $str .= ':_directories';
                     break;
                 case 'path':
                     $str .= ':_path_files';
                     break;
             }
             if (isset($opt->glob)) {
                 $str .= ' -g "' . $opt->glob . '"';
             }
         }
     }
     $str .= "'";
     // close quote
     return $str;
 }
コード例 #8
0
ファイル: OptionCollection.php プロジェクト: c9s/getoptionkit
 /**
  * Add option object.
  *
  * @param object $spec the option object.
  */
 public function addOption(Option $spec)
 {
     $this->data[$spec->getId()] = $spec;
     if ($spec->long) {
         $this->longOptions[$spec->long] = $spec;
     }
     if ($spec->short) {
         $this->shortOptions[$spec->short] = $spec;
     }
     $this->options[] = $spec;
     if (!$spec->long && !$spec->short) {
         throw new Exception('Neither long option name nor short name is not given.');
     }
 }