Exemplo n.º 1
0
function retrieve_args()
{
    $opt = new Getopt([(new Option('h', 'help'))->setDescription('Prints this help'), (new Option('v', 'verbose'))->setDescription('Verbose output'), (new Option('f', 'file-title'))->setDescription('Use file name for title tag'), (new Option('x', 'sort-lex'))->setDescription('Sort files lexicographically'), (new Option('t', 'tree-dst'))->setDescription('Retain the tree structure of the source album at destination'), (new Option('p', 'drop-dst'))->setDescription('Do not create destination directory'), (new Option('r', 'reverse'))->setDescription('Copy files in reverse order (last file first)'), (new Option('e', 'file-type', Getopt::REQUIRED_ARGUMENT))->setDescription('Accept only audio files of the specified type'), (new Option('u', 'unified-name', Getopt::REQUIRED_ARGUMENT))->setDescription('Base name for everything but the "Artist" tag'), (new Option('b', 'album-num', Getopt::REQUIRED_ARGUMENT))->setDescription('Album number'), (new Option('a', 'artist-tag', Getopt::REQUIRED_ARGUMENT))->setDescription('"Artist" tag'), (new Option('g', 'album-tag', Getopt::REQUIRED_ARGUMENT))->setDescription('"Album" tag')]);
    $opt->parse();
    if ($opt->getOption('help')) {
        print $opt->getHelpText();
        exit(2);
    }
    if (count($opt->getOperands()) !== 2) {
        print "Command line syntax: <src> and <dst> operands required.\n" . $opt->getHelpText();
        exit(2);
    }
    if (!is_dir($opt->getOperand(0))) {
        print "Source directory \"{$opt->getOperand(0)}\" is not there.\n";
        exit(2);
    }
    if (!is_dir($opt->getOperand(1))) {
        print "Destination path \"{$opt->getOperand(1)}\" is not there.\n";
        exit(2);
    }
    return $opt;
}
Exemplo n.º 2
0
 /**
  * The main logic block for dispatching the CLI to the implementation
  * classes. Assuming it makes it all the way to the actual method cleanly,
  * its return code will be propagated up.  Otherwise, RuntimeExceptions exit
  * 1 (user error), LogicExceptions exit 2 (programmer error), and everything
  * else exits 3 (doom)
  *
  * @return int The intended exit status cide
  */
 public function run()
 {
     $trie = self::getCommandTrie();
     $class = Utilities::searchTrieFromArgv($trie, $this->argv);
     $cmd = new $class();
     $banner = $cmd->getBanner();
     try {
         $opt = new Getopt();
         $opt->addOptions($cmd->getOptions());
         $opt->addOptions(self::getDefaultOptions());
         if ($banner) {
             $opt->setBanner($banner . PHP_EOL);
         }
         $opt->parse(implode(' ', $this->argv));
     } catch (\UnexpectedValueException $e) {
         // Unexpected CLI arguments
         $this->console->exception($e);
         $this->console->writeLine($opt->getHelpText());
         return 1;
     } catch (\InvalidArgumentException $e) {
         // Command is broken - most likely duplicated arguments
         $this->console->exception($e);
         return 2;
     } catch (\Exception $e) {
         // Catch-all,
         $this->console->exception($e);
         return 3;
     }
     // Unfortunately we can't easily do this earlier. Native getopt() is
     // useless on all subcommands, and the class implementation screams
     // about unexpected values.
     $v = Utilities::parseVerbosity($opt['q'], $opt['v']);
     $this->console->setVerbosity($v);
     if ($opt['help']) {
         return $this->showHelp($cmd, $opt->getHelpText());
     }
     if ($opt['version']) {
         return $this->showVersion($cmd);
     }
     try {
         return $cmd->setOutput($this->console)->setOperands($opt->getOperands())->setOptionValues($opt->getOptions())->execute();
     } catch (\RuntimeException $e) {
         $this->console->exception($e);
         $this->console->writeLine($opt->getHelpText());
         return 1;
     } catch (\LogicException $e) {
         $this->console->exception($e);
         return 2;
     } catch (\Exception $e) {
         $this->console->exception($e);
         return 3;
     }
 }