Exemple #1
0
 * This is a command-line version
 *
 * @author Igor Gariev <*****@*****.**>
 * @copyright Copyright (c) 2013 Igor Gariev
 * @licence http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */
$includeDir = "@php_dir@" == "@" . "php_dir@" ? dirname(__FILE__) . "/.." : "@php_dir@/XRef";
require_once "{$includeDir}/XRef.class.php";
// command-line arguments
XRef::registerCmdOption('o:', "output=", '-o, --output=TYPE', "either 'text' (default) or 'json'");
XRef::registerCmdOption('r:', "report-level=", '-r, --report-level=', "either 'error', 'warning' or 'notice'");
XRef::registerCmdOption('', "no-cache", '--no-cache', "don't use lint cache, if any");
XRef::registerCmdOption('', "init", '--init', "create a config file, init cache");
XRef::registerCmdOption('', "git", '--git', "git pre-commit mode: find new errors in modified tracked files");
XRef::registerCmdOption('', "git-cached", '--git-cached', "git pre-commit mode: compare files cached for commit with HEAD");
XRef::registerCmdOption('', "git-rev=", '--git-rev=<rev> or --git-rev=<from>:<to> ', array("", "find errors in revision <rev>, or find errors added since rev <from> to rev <to>"));
try {
    list($options, $arguments) = XRef::getCmdOptions();
} catch (Exception $e) {
    error_log($e->getMessage());
    error_log("See 'xref-lint --help'");
    exit(1);
}
if (XRef::needHelp()) {
    $program = basename($argv[0]);
    XRef::showHelpScreen("xref-lint - tool to find problems in PHP source code", "{$program} [options] [files to check]");
    exit(1);
}
if (isset($options['init'])) {
    $xref = new XRef();
    $xref->init();
Exemple #2
0
 public function testCommandLineParser()
 {
     // basic test
     $args = array("script.php", "--help");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["help"]));
     $this->assertTrue($options["help"] === true);
     // check that the options were cached since last call
     list($options, $arguments) = XRef::getCmdOptions();
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["help"]));
     $this->assertTrue($options["help"] === true);
     $this->assertTrue(XRef::needHelp());
     // check that the options are reset
     // WARNING: here the real comman-line options will be read
     // TODO: real options are unknown (phpunit dependent?)
     // so no reliable test can be made (?)
     //
     // list($options, $arguments) = XRef::getCmdOptions(false);
     // print_r( $options );
     // print_r( $arguments );
     // check empty options
     $args = array("script.php");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 0);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(!isset($options["help"]));
     // check short --> long options
     $args = array("script.php", "-h", "foo");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["help"]));
     $this->assertTrue($options["help"] === 'foo');
     // check custom options with arguments
     XRef::registerCmdOption('a:', "foo-bar=", '...', "some help");
     $args = array("script.php", "-a", "baz");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["foo-bar"]));
     $this->assertTrue($options["foo-bar"] === "baz");
     $args = array("script.php", "--foo-bar", "baz");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["foo-bar"]));
     $this->assertTrue($options["foo-bar"] === "baz");
     $args = array("script.php", "--foo-bar=baz");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["foo-bar"]));
     $this->assertTrue($options["foo-bar"] === "baz");
     // check -d (--define) option
     $args = array("script.php", "-d", "foo=bar", "--define", "bz");
     list($options, $arguments) = XRef::getCmdOptions($args);
     $this->assertTrue(count($options) == 1);
     $this->assertTrue(count($arguments) == 0);
     $this->assertTrue(isset($options["define"]));
     $this->assertTrue(is_array($options["define"]));
     $this->assertTrue(count($options["define"]) == 2);
     $this->assertTrue($options["define"][0] == "foo=bar");
     $this->assertTrue($options["define"][1] == "bz");
     // reset and cache to empty (default) values
     list($options, $arguments) = XRef::getCmdOptions(array());
 }