Example #1
0
 /**
  * This is it, we parse the given arguments.
  *
  * @throws \Exception
  */
 public function parse()
 {
     list($shortOptions, $longOptions) = $this->buildOptions();
     $results = getopt($shortOptions, $longOptions);
     foreach ($this->arguments as $argument) {
         $name = $argument->name;
         $value = '';
         if (isset($results[$argument->prefix]) || isset($results[$argument->longPrefix])) {
             $value = isset($results[$argument->prefix]) ? $results[$argument->prefix] : $results[$argument->longPrefix];
         } else {
             /**
              * If we set the default value for this argument we also add it to
              * the result array or it will fail the argument has the option required by mistake.
              */
             if ($argument->defaultValue) {
                 $value = $argument->defaultValue;
                 $this->manager->setHasDefaultValue($name, true);
                 $results[$argument->name] = $value;
             } else {
                 if ($argument->required === true) {
                     throw new \Exception('The following arguments are required: ' . print_r($argument->name, true) . '.');
                 }
             }
         }
         $this->manager->set($name, $value);
     }
 }
Example #2
0
 /**
  * This test will ensure that \Redbox\Cli\Arguments\Manager::get returns false
  * if an argument is unknown (or not parsed).
  */
 public function test_if_get_returns_false_on_unknown_argument()
 {
     $manager = new Arguments\Manager();
     $this->assertFalse($manager->get('non_existing'));
 }