/**
     * `psysh` command line executable.
     *
     * @return Closure
     */
    function bin()
    {
        return function () {
            $usageException = null;
            $input = new ArgvInput();
            try {
                $input->bind(new InputDefinition(array(new InputOption('help', 'h', InputOption::VALUE_NONE), new InputOption('config', 'c', InputOption::VALUE_REQUIRED), new InputOption('version', 'v', InputOption::VALUE_NONE), new InputOption('cwd', null, InputOption::VALUE_REQUIRED), new InputArgument('include', InputArgument::IS_ARRAY))));
            } catch (\RuntimeException $e) {
                $usageException = $e;
            }
            $config = array();
            // Handle --config
            if ($configFile = $input->getOption('config')) {
                $config['configFile'] = $configFile;
            }
            $shell = new Shell(new Configuration($config));
            // Handle --help
            if ($usageException !== null || $input->getOption('help')) {
                if ($usageException !== null) {
                    echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
                }
                $version = $shell->getVersion();
                $name = basename(reset($_SERVER['argv']));
                echo <<<EOL
{$version}

Usage:
  {$name} [--version] [--help] [files...]

Options:
  --help     -h Display this help message.
  --config   -c Use an alternate PsySH config file location.
  --cwd         Use an alternate working directory.
  --version  -v Display the PsySH version.

EOL;
                exit($usageException === null ? 0 : 1);
            }
            // Handle --version
            if ($input->getOption('version')) {
                echo $shell->getVersion() . PHP_EOL;
                exit(0);
            }
            // Pass additional arguments to Shell as 'includes'
            $shell->setIncludes($input->getArgument('include'));
            try {
                // And go!
                $shell->run();
            } catch (Exception $e) {
                echo $e->getMessage() . PHP_EOL;
                // TODO: this triggers the "exited unexpectedly" logic in the
                // ForkingLoop, so we can't exit(1) after starting the shell...
                // fix this :)
                // exit(1);
            }
        };
    }
 /**
  * We need to add the profile enable option to all commands if we are in
  * the parameter mode.
  *
  * Inspired by
  * http://php-and-symfony.matthiasnoback.nl/2013/11/symfony2-add-a-global-option-to-console-commands-and-generate-pid-file/
  *
  * @param ConsoleCommandEvent $event
  * @return mixed
  */
 private function isProfileOption(ConsoleCommandEvent $event)
 {
     $inputDefinition = $event->getCommand()->getApplication()->getDefinition();
     $inputDefinition->addOption(new InputOption($this->optionName, null, InputOption::VALUE_NONE, '<info>JnsXhprofBundle</info>: Whether to profile this command with xhprof', null));
     // merge the application's input definition
     $event->getCommand()->mergeApplicationDefinition();
     $input = new ArgvInput();
     // we use the input definition of the command
     $input->bind($event->getCommand()->getDefinition());
     return $input->getOption($this->optionName);
 }
 public function testParameters()
 {
     $cw = $this->buildCommandWrapper();
     $originaDefinition = clone $cw->getDefinition();
     $appIndexDefinition = $this->buildInputDefinition();
     $this->mergeInputDefinitions($cw, $appIndexDefinition);
     $input = new ArgvInput(array('cmd', 'uno_value', 'name_value', '--go'));
     $input->bind($cw->getDefinition());
     $input = $cw->filterByOriginalDefinition($input, $appIndexDefinition);
     $input->validate();
     $this->assertEquals(count($originaDefinition->getArguments()), count($input->getArguments()));
     $this->assertEquals(1, count($input->getArguments()));
     $this->assertEquals(array('name' => 'name_value'), $input->getArguments());
     $this->assertEquals(array('go' => true), $input->getOptions());
 }
 /**
  * @param ConsoleCommandEvent $event
  */
 public function onConsoleCommand(ConsoleCommandEvent $event)
 {
     $command = $event->getCommand();
     /** @var Application $application */
     $application = $command->getApplication();
     $inputDefinition = $command->getDefinition();
     if ($command instanceof HelpCommand) {
         $input = new ArgvInput();
         $input->bind($inputDefinition);
         $command = $application->find($input->getFirstArgument());
     }
     if ($command instanceof BaseCommand) {
         $this->setInputDefinition($inputDefinition);
     }
 }
Exemple #5
0
 public function onCommand(ConsoleCommandEvent $event)
 {
     $output = $event->getOutput();
     $input = new ArgvInput();
     try {
         $input->bind($this->getDefinition());
     } catch (\RuntimeException $e) {
     }
     $delay = filter_var($input->getOption('delay'), FILTER_VALIDATE_INT);
     if ($delay > 0) {
         $wakeupAt = time() + mt_rand(1, $delay);
         $output->writeln('<comment>Waiting until ' . date('Y-m-d H:i:s', $wakeupAt) . ' eRepublik time.</comment>');
         time_sleep_until($wakeupAt);
     }
     $this->configPath = $input->getOption('config');
     $this->loadConfig();
 }
Exemple #6
0
 /**
  * @return \Symfony\Component\DependencyInjection\ContainerBuilder
  */
 protected function getContainer()
 {
     if ($this->container) {
         return $this->container;
     }
     // Load cli options:
     $input = new ArgvInput();
     $input->bind($this->getDefaultInputDefinition());
     $configPath = $input->getOption('config');
     // Make sure to set the full path when it is declared relative
     // This will fix some issues in windows.
     $filesystem = new Filesystem();
     if (!$filesystem->isAbsolutePath($configPath)) {
         $configPath = getcwd() . DIRECTORY_SEPARATOR . $configPath;
     }
     // Build the service container:
     $this->container = ContainerFactory::buildFromConfiguration($configPath);
     return $this->container;
 }
 public function onConsoleCommand(ConsoleCommandEvent $event)
 {
     /**
      * Skip if command is not a daemon-command
      */
     if (!$event->getCommand() instanceof ContainerAwareDaemonCommand) {
         return true;
     }
     if (true !== $this->mergeDefinitions($event)) {
         throw new ExtendedConfigurationException('No definitions');
     }
     $input = new ArgvInput();
     $input->bind($event->getCommand()->getDefinition());
     if (true !== $this->addSignals()) {
         throw new \Exception('No signals!');
     }
     if (true === $input->getOption('log-memory')) {
         if (true !== $this->addWatchers()) {
             throw new \Exception('No watchers!');
         }
     }
     if (true === $input->getOption('daemonize') & function_exists('pcntl_fork')) {
         $processId = pcntl_fork();
         if ($processId === -1) {
             throw new \Exception('The process failed to fork.');
         } else {
             if ($processId) {
                 $this->logger->info($event->getCommand()->getName() . ' correctly started as a daemon');
                 exit(0);
             }
         }
         if (posix_setsid() == -1) {
             throw new \Exception("Unable to detach from the terminal window.");
         }
     } else {
         $event->getOutput()->writeln('<comment>Use --daemonize option to run the process in background.</comment>');
     }
     return true;
 }
 public function testParser()
 {
     $input = new ArgvInput(array('cli.php', 'foo'));
     $input->bind(new InputDefinition(array(new InputArgument('name'))));
     $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
     $input->bind(new InputDefinition(array(new InputArgument('name'))));
     $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
     $input = new ArgvInput(array('cli.php', '--foo'));
     $input->bind(new InputDefinition(array(new InputOption('foo'))));
     $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value');
     $input = new ArgvInput(array('cli.php', '--foo=bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
     $input = new ArgvInput(array('cli.php', '--foo', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)');
     try {
         $input = new ArgvInput(array('cli.php', '--foo'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
         $this->fail('->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
         $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     }
     $input = new ArgvInput(array('cli.php', '-f'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
     $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value');
     $input = new ArgvInput(array('cli.php', '-fbar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)');
     $input = new ArgvInput(array('cli.php', '-f', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)');
     $input = new ArgvInput(array('cli.php', '-f', ''));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => ''), $input->getOptions(), '->parse() parses short options with an optional empty value');
     $input = new ArgvInput(array('cli.php', '-f', '', 'foo'));
     $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => ''), $input->getOptions(), '->parse() parses short options with an optional empty value followed by an argument');
     $input = new ArgvInput(array('cli.php', '-f', '', '-b'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
     $this->assertEquals(array('foo' => '', 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional empty value followed by an option');
     $input = new ArgvInput(array('cli.php', '-f', '-b', 'foo'));
     $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
     $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
     try {
         $input = new ArgvInput(array('cli.php', '-f'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
         $this->fail('->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
         $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     }
     try {
         $input = new ArgvInput(array('cli.php', '-ffoo'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
         $this->fail('->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
         $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
     }
     try {
         $input = new ArgvInput(array('cli.php', '--foo=bar'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
         $this->fail('->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
         $this->assertEquals('The "--foo" option does not accept a value.', $e->getMessage(), '->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
     }
     try {
         $input = new ArgvInput(array('cli.php', 'foo', 'bar'));
         $input->bind(new InputDefinition());
         $this->fail('->parse() throws a \\RuntimeException if too many arguments are passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if too many arguments are passed');
         $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \\RuntimeException if too many arguments are passed');
     }
     try {
         $input = new ArgvInput(array('cli.php', '--foo'));
         $input->bind(new InputDefinition());
         $this->fail('->parse() throws a \\RuntimeException if an unknown long option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if an unknown long option is passed');
         $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \\RuntimeException if an unknown long option is passed');
     }
     try {
         $input = new ArgvInput(array('cli.php', '-f'));
         $input->bind(new InputDefinition());
         $this->fail('->parse() throws a \\RuntimeException if an unknown short option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if an unknown short option is passed');
         $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \\RuntimeException if an unknown short option is passed');
     }
     $input = new ArgvInput(array('cli.php', '-fb'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
     $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
     $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has a required value');
     $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value');
     $input = new ArgvInput(array('cli.php', '-fbbar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator');
     $input = new ArgvInput(array('cli.php', '-fbbar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => 'bbar', 'bar' => null), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and one of them takes a value');
     try {
         $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
         $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
     } catch (\RuntimeException $e) {
         $this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
     }
     $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name=baz'));
     $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
     $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option=value" syntax)');
     $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', 'baz'));
     $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
     $this->assertEquals(array('name' => array('foo', 'bar', 'baz')), $input->getOptions(), '->parse() parses array options ("--option value" syntax)');
     $input = new ArgvInput(array('cli.php', '--name=foo', '--name=bar', '--name='));
     $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY))));
     $this->assertSame(array('name' => array('foo', 'bar', null)), $input->getOptions(), '->parse() parses empty array options as null ("--option=value" syntax)');
     $input = new ArgvInput(array('cli.php', '--name', 'foo', '--name', 'bar', '--name', '--anotherOption'));
     $input->bind(new InputDefinition(array(new InputOption('name', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY), new InputOption('anotherOption', null, InputOption::VALUE_NONE))));
     $this->assertSame(array('name' => array('foo', 'bar', null), 'anotherOption' => true), $input->getOptions(), '->parse() parses empty array options as null ("--option value" syntax)');
     try {
         $input = new ArgvInput(array('cli.php', '-1'));
         $input->bind(new InputDefinition(array(new InputArgument('number'))));
         $this->fail('->parse() throws a \\RuntimeException if an unknown option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
         $this->assertEquals('The "-1" option does not exist.', $e->getMessage(), '->parse() parses arguments with leading dashes as options without having encountered a double-dash sequence');
     }
     $input = new ArgvInput(array('cli.php', '--', '-1'));
     $input->bind(new InputDefinition(array(new InputArgument('number'))));
     $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
     $input = new ArgvInput(array('cli.php', '-f', 'bar', '--', '-1'));
     $input->bind(new InputDefinition(array(new InputArgument('number'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses arguments with leading dashes as options before having encountered a double-dash sequence');
     $this->assertEquals(array('number' => '-1'), $input->getArguments(), '->parse() parses arguments with leading dashes as arguments after having encountered a double-dash sequence');
     $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
     $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
 }
 public function testParseEmptyStringArgument()
 {
     $input = new ArgvInput(array('cli.php', '-f', 'bar', ''));
     $input->bind(new InputDefinition(array(new InputArgument('empty'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('empty' => ''), $input->getArguments(), '->parse() parses empty string arguments');
 }
 public function testParser()
 {
     $input = new ArgvInput(array('cli.php', 'foo'));
     $input->bind(new InputDefinition(array(new InputArgument('name'))));
     $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() parses required arguments');
     $input->bind(new InputDefinition(array(new InputArgument('name'))));
     $this->assertEquals(array('name' => 'foo'), $input->getArguments(), '->parse() is stateless');
     $input = new ArgvInput(array('cli.php', '--foo'));
     $input->bind(new InputDefinition(array(new InputOption('foo'))));
     $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses long options without a value');
     $input = new ArgvInput(array('cli.php', '--foo=bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a = separator)');
     $input = new ArgvInput(array('cli.php', '--foo', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses long options with a required value (with a space separator)');
     try {
         $input = new ArgvInput(array('cli.php', '--foo'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
         $this->fail('->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
         $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     }
     $input = new ArgvInput(array('cli.php', '-f'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'))));
     $this->assertEquals(array('foo' => true), $input->getOptions(), '->parse() parses short options without a value');
     $input = new ArgvInput(array('cli.php', '-fbar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with no separator)');
     $input = new ArgvInput(array('cli.php', '-f', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => 'bar'), $input->getOptions(), '->parse() parses short options with a required value (with a space separator)');
     $input = new ArgvInput(array('cli.php', '-f', '-b', 'foo'));
     $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b'))));
     $this->assertEquals(array('foo' => null, 'bar' => true), $input->getOptions(), '->parse() parses short options with an optional value which is not present');
     try {
         $input = new ArgvInput(array('cli.php', '-f'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_REQUIRED))));
         $this->fail('->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
         $this->assertEquals('The "--foo" option requires a value.', $e->getMessage(), '->parse() throws a \\RuntimeException if no value is passed to an option when it is required');
     }
     try {
         $input = new ArgvInput(array('cli.php', '-ffoo'));
         $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_NONE))));
         $this->fail('->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
         $this->assertEquals('The "-o" option does not exist.', $e->getMessage(), '->parse() throws a \\RuntimeException if a value is passed to an option which does not take one');
     }
     try {
         $input = new ArgvInput(array('cli.php', 'foo', 'bar'));
         $input->bind(new InputDefinition());
         $this->fail('->parse() throws a \\RuntimeException if too many arguments are passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if too many arguments are passed');
         $this->assertEquals('Too many arguments.', $e->getMessage(), '->parse() throws a \\RuntimeException if too many arguments are passed');
     }
     try {
         $input = new ArgvInput(array('cli.php', '--foo'));
         $input->bind(new InputDefinition());
         $this->fail('->parse() throws a \\RuntimeException if an unknown long option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if an unknown long option is passed');
         $this->assertEquals('The "--foo" option does not exist.', $e->getMessage(), '->parse() throws a \\RuntimeException if an unknown long option is passed');
     }
     try {
         $input = new ArgvInput(array('cli.php', '-f'));
         $input->bind(new InputDefinition());
         $this->fail('->parse() throws a \\RuntimeException if an unknown short option is passed');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->parse() throws a \\RuntimeException if an unknown short option is passed');
         $this->assertEquals('The "-f" option does not exist.', $e->getMessage(), '->parse() throws a \\RuntimeException if an unknown short option is passed');
     }
     $input = new ArgvInput(array('cli.php', '-fb'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b'))));
     $this->assertEquals(array('foo' => true, 'bar' => true), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one');
     $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_REQUIRED))));
     $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has a required value');
     $input = new ArgvInput(array('cli.php', '-fb', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value');
     $input = new ArgvInput(array('cli.php', '-fbbar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => true, 'bar' => 'bar'), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and the last one has an optional value with no separator');
     $input = new ArgvInput(array('cli.php', '-fbbar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputOption('bar', 'b', InputOption::VALUE_OPTIONAL))));
     $this->assertEquals(array('foo' => 'bbar', 'bar' => null), $input->getOptions(), '->parse() parses short options when they are aggregated as a single one and one of them takes a value');
     try {
         $input = new ArgvInput(array('cli.php', 'foo', 'bar', 'baz', 'bat'));
         $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::IS_ARRAY))));
         $this->assertEquals(array('name' => array('foo', 'bar', 'baz', 'bat')), $input->getArguments(), '->parse() parses array arguments');
     } catch (\RuntimeException $e) {
         $this->assertNotEquals('Too many arguments.', $e->getMessage(), '->parse() parses array arguments');
     }
 }
 public function testParseSingleDashAsArgument()
 {
     $input = new ArgvInput(array('cli.php', '-'));
     $input->bind(new InputDefinition(array(new InputArgument('file'))));
     $this->assertEquals(array('file' => '-'), $input->getArguments(), '->parse() parses single dash as an argument');
 }
 public function testParseOptionWithValueOptionalGivenEmptyAndOptionalArgument()
 {
     $input = new ArgvInput(array('cli.php', '--foo=', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
     $this->assertEquals(array('foo' => null), $input->getOptions(), '->parse() parses optional options with empty value as null');
     $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
     $input = new ArgvInput(array('cli.php', '--foo=0', 'bar'));
     $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::VALUE_OPTIONAL), new InputArgument('name', InputArgument::OPTIONAL))));
     $this->assertEquals(array('foo' => '0'), $input->getOptions(), '->parse() parses optional options with empty value as null');
     $this->assertEquals(array('name' => 'bar'), $input->getArguments(), '->parse() parses optional arguments');
 }
 /**
  * Filter and set input for doctrine command line
  *
  * @param array $input
  */
 public function setInputArgs($input, $options)
 {
     $filteredOptions = $this->filterOptions($options);
     $transOption = $this->transOption($filteredOptions);
     $inputAndOptions = array_merge($input, $transOption);
     array_unshift($inputAndOptions, 'doctrine');
     $argvInput = new ArgvInput($inputAndOptions);
     $argvInput->bind($this->getDefinition());
     return $argvInput;
 }