app.php [--app-options] [subcommand --subcommand-options] [subcommand --subcommand-options] [subcommand --subcommand-options] [arguments] ContinuousOptionParser is for the process flow: init app options, parse app options while not end if stop at command shift command parse command options else if stop at arguments shift arguments execute current command with the arguments. Example code: subcommand stack $subcommands = array('subcommand1','subcommand2','subcommand3'); different command has its own options $subcommand_specs = array( 'subcommand1' => $cmdspecs, 'subcommand2' => $cmdspecs, 'subcommand3' => $cmdspecs, ); for saved options $subcommand_options = array(); command arguments $arguments = array(); $argv = explode(' ','-v -d -c subcommand1 -a -b -c subcommand2 -c subcommand3 arg1 arg2 arg3'); parse application options first $parser = new ContinuousOptionParser( $appspecs ); $app_options = $parser->parse( $argv ); while( ! $parser->isEnd() ) { if( $parser->getCurrentArgument() == $subcommands[0] ) { $parser->advance(); $subcommand = array_shift( $subcommands ); $parser->setSpecs( $subcommand_specs[$subcommand] ); $subcommand_options[ $subcommand ] = $parser->continueParse(); } else { $arguments[] = $parser->advance(); } }
Inheritance: extends OptionParser
Example #1
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;
 }
 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);
 }
Example #3
0
require_once $moosh_dir . '/includes/functions.php';
require_once $moosh_dir . '/includes/default_options.php';
use GetOptionKit\ContinuousOptionParser;
use GetOptionKit\OptionCollection;
@error_reporting(E_ALL | E_STRICT);
@ini_set('display_errors', '1');
define('MOOSH_VERSION', '0.23');
define('MOODLE_INTERNAL', true);
$appspecs = new OptionCollection();
$spec_verbose = $appspecs->add('v|verbose', "be verbose");
$appspecs->add('p|moodle-path:', "Moodle directory.");
$appspecs->add('u|user:'******'n|no-user-check', "Don't check if Moodle data is owned by the user running script");
$appspecs->add('t|performance', "Show performance infomation including timings");
$appspecs->add('h|help', "Show global help.");
$parser = new ContinuousOptionParser($appspecs);
$app_options = $parser->parse($argv);
if ($app_options->has('moodle-path')) {
    $top_dir = $app_options['moodle-path']->value;
} else {
    $top_dir = find_top_moodle_dir($cwd);
}
$moodle_version = moosh_moodle_version($top_dir);
$local_dir = home_dir() . DIRECTORY_SEPARATOR . '.moosh';
$viable_versions = moosh_generate_version_list($moodle_version);
$viable_versions[] = 'Generic';
$namespaced_commands = moosh_load_all_commands($moosh_dir, $viable_versions);
$namespaced_commands_extra = moosh_load_all_commands($local_dir, $viable_versions);
if ($namespaced_commands_extra) {
    $namespaced_commands = array_merge($namespaced_commands, $namespaced_commands_extra);
    $loader->set(false, $local_dir);
 /**
  * @expectedException LogicException
  */
 public function testAdvancedOutOfBounds()
 {
     $options = new OptionCollection();
     $options->add("v|verbose");
     $parser = new ContinuousOptionParser($options);
     $result = $parser->parse(array('app', '-v'));
     $parser->advance();
 }
Example #5
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;
 }