Search in sources :

Example 11 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project hudson-2.x by hudson.

the class Job method getBuildForCLI.

@CLIResolver
public RunT getBuildForCLI(@Argument(required = true, metaVar = "BUILD#", usage = "Build number") String id) throws CmdLineException {
    try {
        int n = Integer.parseInt(id);
        RunT r = getBuildByNumber(n);
        if (r == null)
            throw new CmdLineException(null, "No such build '#" + n + "' exists");
        return r;
    } catch (NumberFormatException e) {
        throw new CmdLineException(null, id + "is not a number");
    }
}
Also used : ExtensionPoint(hudson.ExtensionPoint) CmdLineException(org.kohsuke.args4j.CmdLineException) CLIResolver(hudson.cli.declarative.CLIResolver)

Example 12 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project hudson-2.x by hudson.

the class Main method _main.

/**
     * Main without the argument handling.
     */
public static void _main(String[] args) throws IOException, InterruptedException, CmdLineException {
    // by overwriting the security manager.
    try {
        System.setSecurityManager(null);
    } catch (SecurityException e) {
    // ignore and move on.
    // some user reported that this happens on their JVM: http://d.hatena.ne.jp/tueda_wolf/20080723
    }
    Main m = new Main();
    CmdLineParser p = new CmdLineParser(m);
    p.parseArgument(args);
    if (m.args.size() != 2)
        throw new CmdLineException("two arguments required, but got " + m.args);
    if (m.urls.isEmpty())
        throw new CmdLineException("At least one -url option is required.");
    m.main();
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 13 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gitblit by gitblit.

the class GitBlitServer method main.

public static void main(String... args) {
    GitBlitServer server = new GitBlitServer();
    // filter out the baseFolder parameter
    List<String> filtered = new ArrayList<String>();
    String folder = "data";
    for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        if (arg.equals("--baseFolder")) {
            if (i + 1 == args.length) {
                System.out.println("Invalid --baseFolder parameter!");
                System.exit(-1);
            } else if (!".".equals(args[i + 1])) {
                folder = args[i + 1];
            }
            i = i + 1;
        } else {
            filtered.add(arg);
        }
    }
    Params.baseFolder = folder;
    Params params = new Params();
    CmdLineParser parser = new CmdLineParser(params);
    try {
        parser.parseArgument(filtered);
        if (params.help) {
            server.usage(parser, null);
        }
    } catch (CmdLineException t) {
        server.usage(parser, t);
    }
    if (params.stop) {
        server.stop(params);
    } else {
        server.start(params);
    }
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) ArrayList(java.util.ArrayList) Constraint(org.eclipse.jetty.util.security.Constraint) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 14 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gitblit by gitblit.

the class AddIndexedBranch method main.

public static void main(String... args) {
    Params params = new Params();
    CmdLineParser parser = new CmdLineParser(params);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException t) {
        System.err.println(t.getMessage());
        parser.printUsage(System.out);
        return;
    }
    // create a lowercase set of excluded repositories
    Set<String> exclusions = new TreeSet<String>();
    for (String exclude : params.exclusions) {
        exclusions.add(exclude.toLowerCase());
    }
    // determine available repositories
    File folder = new File(params.folder);
    List<String> repoList = JGitUtils.getRepositoryList(folder, false, true, -1, null);
    int modCount = 0;
    int skipCount = 0;
    for (String repo : repoList) {
        boolean skip = false;
        for (String exclusion : exclusions) {
            if (StringUtils.fuzzyMatch(repo, exclusion)) {
                skip = true;
                break;
            }
        }
        if (skip) {
            System.out.println("skipping " + repo);
            skipCount++;
            continue;
        }
        try {
            // load repository config
            File gitDir = FileKey.resolve(new File(folder, repo), FS.DETECTED);
            Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
            StoredConfig config = repository.getConfig();
            config.load();
            Set<String> indexedBranches = new LinkedHashSet<String>();
            // add all local branches to index
            if (params.addAllLocalBranches) {
                List<RefModel> list = JGitUtils.getLocalBranches(repository, true, -1);
                for (RefModel refModel : list) {
                    System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", refModel.getName(), repo));
                    indexedBranches.add(refModel.getName());
                }
            } else {
                // add only one branch to index ('default' if not specified)
                System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", params.branch, repo));
                indexedBranches.add(params.branch);
            }
            String[] branches = config.getStringList("gitblit", null, "indexBranch");
            if (!ArrayUtils.isEmpty(branches)) {
                for (String branch : branches) {
                    indexedBranches.add(branch);
                }
            }
            config.setStringList("gitblit", null, "indexBranch", new ArrayList<String>(indexedBranches));
            config.save();
            modCount++;
        } catch (Exception e) {
            System.err.println(repo);
            e.printStackTrace();
        }
    }
    System.out.println(MessageFormat.format("updated {0} repository configurations, skipped {1}", modCount, skipCount));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RefModel(com.gitblit.models.RefModel) CmdLineParser(org.kohsuke.args4j.CmdLineParser) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) CmdLineException(org.kohsuke.args4j.CmdLineException) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) TreeSet(java.util.TreeSet) File(java.io.File) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 15 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gitblit by gitblit.

the class BaseCommand method parseCommandLine.

/**
	 * Parses the command line argument, injecting parsed values into fields.
	 * <p>
	 * This method must be explicitly invoked to cause a parse.
	 *
	 * @param options
	 *            object whose fields declare Option and Argument annotations to
	 *            describe the parameters of the command. Usually {@code this}.
	 * @throws UnloggedFailure
	 *             if the command line arguments were invalid.
	 * @see Option
	 * @see Argument
	 */
protected void parseCommandLine(Object options) throws UnloggedFailure {
    final CmdLineParser clp = newCmdLineParser(options);
    try {
        clp.parseArgument(argv);
    } catch (IllegalArgumentException err) {
        if (!clp.wasHelpRequestedByOption()) {
            throw new UnloggedFailure(1, "fatal: " + err.getMessage());
        }
    } catch (CmdLineException err) {
        if (!clp.wasHelpRequestedByOption()) {
            throw new UnloggedFailure(1, "fatal: " + err.getMessage());
        }
    }
    if (clp.wasHelpRequestedByOption()) {
        CommandMetaData meta = getClass().getAnnotation(CommandMetaData.class);
        String title = meta.name().toUpperCase() + ": " + meta.description();
        String b = com.gitblit.utils.StringUtils.leftPad("", title.length() + 2, '═');
        StringWriter msg = new StringWriter();
        msg.write('\n');
        msg.write(b);
        msg.write('\n');
        msg.write(' ');
        msg.write(title);
        msg.write('\n');
        msg.write(b);
        msg.write("\n\n");
        msg.write("USAGE\n");
        msg.write("─────\n");
        msg.write(' ');
        msg.write(commandName);
        msg.write('\n');
        msg.write("  ");
        clp.printSingleLineUsage(msg, null);
        msg.write("\n\n");
        String txt = getUsageText();
        if (!StringUtils.isEmpty(txt)) {
            msg.write(txt);
            msg.write("\n\n");
        }
        msg.write("ARGUMENTS & OPTIONS\n");
        msg.write("───────────────────\n");
        clp.printUsage(msg, null);
        msg.write('\n');
        String examples = usage().trim();
        if (!StringUtils.isEmpty(examples)) {
            msg.write('\n');
            msg.write("EXAMPLES\n");
            msg.write("────────\n");
            msg.write(examples);
            msg.write('\n');
        }
        throw new UnloggedFailure(1, msg.toString());
    }
}
Also used : CmdLineParser(com.gitblit.utils.cli.CmdLineParser) StringWriter(java.io.StringWriter) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

CmdLineException (org.kohsuke.args4j.CmdLineException)100 CmdLineParser (org.kohsuke.args4j.CmdLineParser)75 IOException (java.io.IOException)16 File (java.io.File)14 ArrayList (java.util.ArrayList)11 PrintStream (java.io.PrintStream)7 StringWriter (java.io.StringWriter)6 List (java.util.List)5 FileOutputStream (java.io.FileOutputStream)4 Path (java.nio.file.Path)4 CmdLineParser (com.google.gerrit.util.cli.CmdLineParser)3 FeatureExtractors (io.anserini.ltr.feature.FeatureExtractors)3 Qrels (io.anserini.util.Qrels)3 Directory (org.apache.lucene.store.Directory)3 FSDirectory (org.apache.lucene.store.FSDirectory)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Project (com.google.gerrit.reviewdb.client.Project)2 OrmException (com.google.gwtorm.server.OrmException)2 Hudson (hudson.model.Hudson)2