public function testParser3()
 {
     $appspecs = new OptionCollection();
     $appspecs->add('v|verbose');
     $appspecs->add('c|color');
     $appspecs->add('d|debug');
     $cmdspecs = new OptionCollection();
     $cmdspecs->add('n|name:=string');
     $cmdspecs->add('p|phone:=string');
     $cmdspecs->add('a|address:=string');
     $subcommands = array('subcommand1', 'subcommand2', 'subcommand3');
     $subcommand_specs = array('subcommand1' => $cmdspecs, 'subcommand2' => $cmdspecs, 'subcommand3' => $cmdspecs);
     $subcommand_options = array();
     $arguments = array();
     $argv = explode(' ', 'program -v -d -c subcommand1 --name=c9s --phone=123123123 --address=somewhere arg1 arg2 arg3');
     $parser = new ContinuousOptionParser($appspecs);
     $app_options = $parser->parse($argv);
     while (!$parser->isEnd()) {
         if (@$subcommands[0] && $parser->getCurrentArgument() == $subcommands[0]) {
             $parser->advance();
             $subcommand = array_shift($subcommands);
             $parser->setSpecs($subcommand_specs[$subcommand]);
             $subcommand_options[$subcommand] = $parser->continueParse();
         } else {
             $arguments[] = $parser->advance();
         }
     }
     $this->assertCount(3, $arguments);
     $this->assertEquals('arg1', $arguments[0]);
     $this->assertEquals('arg2', $arguments[1]);
     $this->assertEquals('arg3', $arguments[2]);
     $this->assertNotNull($subcommand_options['subcommand1']);
     $this->assertEquals('c9s', $subcommand_options['subcommand1']->name);
     $this->assertEquals('123123123', $subcommand_options['subcommand1']->phone);
     $this->assertEquals('somewhere', $subcommand_options['subcommand1']->address);
 }
예제 #2
0
파일: moosh.php 프로젝트: tmuras/moosh
    echo implode("\n\t", array_keys($subcommands));
    echo "\n";
    echo "Global options:\n";
    //$appspecs->printOptions();
    $printer = new GetOptionKit\OptionPrinter\ConsoleOptionPrinter();
    echo $printer->render($appspecs);
    echo "\n";
    exit(1);
}
if (!$subcommand && $possible_matches) {
    foreach ($possible_matches as $match) {
        echo $match . "\n";
    }
    exit(1);
}
$parser->setSpecs($subcommand_specs[$subcommand]);
try {
    $subcommand_options[$subcommand] = $parser->continueParse();
} catch (Exception $e) {
    echo $e->getMessage() . "\n";
    die("Moosh global options should be passed before command not after it.");
}
while (!$parser->isEnd()) {
    $arguments[] = $parser->advance();
}
// Read config file if available.
$moodlerc = NULL;
$home_dir = home_dir();
if (file_exists($home_dir . DIRECTORY_SEPARATOR . ".mooshrc.php")) {
    $moodlerc = $home_dir . DIRECTORY_SEPARATOR . ".mooshrc.php";
} elseif (file_exists("/etc/moosh/mooshrc.php")) {
예제 #3
0
 /**
  * @param array $argv Array of arguments passed to script
  * @example
  * ```php
  * $binary = new Bin([
  *     __FILE__,
  *     '--help'
  * ]);
  *
  * return $binary->invoke();
  * ```
  */
 public function __construct($argv)
 {
     $parser = new ContinuousOptionParser(static::getArgumentSpecifications());
     $subCommandSpecs = static::getSubCommandsSpecs();
     $subCommands = array_keys((array) $subCommandSpecs);
     //Parse specification arguments from given arguments
     $this->arguments = $parser->parse($argv);
     $arguments = [];
     $subCommandOptions = new \stdClass();
     while (!$parser->isEnd()) {
         if (@$subCommands[0] && $parser->getCurrentArgument() == $subCommands[0]) {
             $parser->advance();
             $subCommand = array_shift($subCommands);
             $parser->setSpecs($subCommandSpecs->{$subCommand});
             $subCommandOptions->{$subCommand} = $parser->continueParse();
         } else {
             $arguments[] = $parser->advance();
         }
     }
     $this->subCommandOptions = $subCommandOptions;
 }
예제 #4
0
 /**
  * Run application with
  * list argv
  *
  * @param Array $argv
  *
  * @return bool return true for success, false for failure. the returned
  *              state will be reflected to the exit code of the process.
  * */
 public function run(array $argv)
 {
     $this->setProgramName($argv[0]);
     $currentCmd = $this;
     // init application,
     // before parsing options, we have to known the registered commands.
     $currentCmd->_init();
     // use getoption kit to parse application options
     $getopt = new ContinuousOptionParser($currentCmd->optionSpecs);
     // parse the first part options (options after script name)
     // option parser should stop before next command name.
     //
     //    $ app.php -v -d next
     //                  |
     //                  |->> parser
     //
     //
     $appOptions = $getopt->parse($argv);
     $currentCmd->setOptions($appOptions);
     if (false === $currentCmd->prepare()) {
         return false;
     }
     $command_stack = array();
     $arguments = array();
     // get command list from application self
     while (!$getopt->isEnd()) {
         $a = $getopt->getCurrentArgument();
         // if current command is in subcommand list.
         if ($currentCmd->hasCommands()) {
             $a = $getopt->getCurrentArgument();
             if (!$currentCmd->hasCommand($a)) {
                 if (!$appOptions->noInteract && ($guess = $currentCmd->guessCommand($a)) !== NULL) {
                     $a = $guess;
                 } else {
                     throw new CommandNotFoundException($currentCmd, $a);
                 }
             }
             $getopt->advance();
             // advance position
             // get command object
             $currentCmd = $currentCmd->getCommand($a);
             $getopt->setSpecs($currentCmd->optionSpecs);
             // parse options for command.
             $currentCmd->setOptions($getopt->continueParse());
             $command_stack[] = $currentCmd;
             // save command object into the stack
         } else {
             $a = $getopt->advance();
             $arguments[] = $a;
         }
     }
     foreach ($command_stack as $cmd) {
         if (false === $cmd->prepare()) {
             return false;
         }
     }
     // get last command and run
     if ($last_cmd = array_pop($command_stack)) {
         $return = $last_cmd->executeWrapper($arguments);
         $last_cmd->finish();
         while ($cmd = array_pop($command_stack)) {
             // call finish stage.. of every command.
             $cmd->finish();
         }
     } else {
         // no command specified.
         return $this->executeWrapper($arguments);
     }
     $currentCmd->finish();
     $this->finish();
     return true;
 }