org.apache.commons.cli.Option类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(13.8k)|赞(0)|评价(0)|浏览(856)

本文整理了Java中org.apache.commons.cli.Option类的一些代码示例,展示了Option类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Option类的具体详情如下:
包路径:org.apache.commons.cli.Option
类名称:Option

Option介绍

[英]Describes a single command-line option. It maintains information regarding the short-name of the option, the long-name, if any exists, a flag indicating if an argument is required for this option, and a self-documenting description of the option.

An Option is not created independantly, but is create through an instance of Options.
[中]描述单个命令行选项。它维护有关选项的短名称、长名称(如果有)、指示此选项是否需要参数的标志以及选项的自文档描述的信息。
选项不是独立创建的,而是通过选项实例创建的。

代码示例

代码示例来源:origin: stackoverflow.com

Options options = new Options();
Option input = new Option("i", "input", true, "input file path");
input.setRequired(true);
options.addOption(input);
Option output = new Option("o", "output", true, "output file");
output.setRequired(true);
options.addOption(output);
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
  cmd = parser.parse(options, args);
} catch (ParseException e) {
  System.out.println(e.getMessage());
  formatter.printHelp("utility-name", options);
String inputFilePath = cmd.getOptionValue("input");
String outputFilePath = cmd.getOptionValue("output");

代码示例来源:origin: azkaban/azkaban

private static Options createHelpOptions() {
 final Options options = new Options();
 options.addOption(Option.builder(HELP_KEY).longOpt("help")
   .desc("print this message").build());
 return options;
}

代码示例来源:origin: apache/incubator-gobblin

private String parseConfigLocation(String[] args) {
 Options options = new Options();
 options.addOption(CLEANER_CONFIG);
 CommandLine cli;
 try {
  CommandLineParser parser = new DefaultParser();
  cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length));
 } catch (ParseException pe) {
  System.out.println("Command line parse exception: " + pe.getMessage());
  printUsage(options);
  throw new RuntimeException(pe);
 }
 return cli.getOptionValue(CLEANER_CONFIG.getOpt());
}

代码示例来源:origin: apache/rocketmq

@Override
public Options buildCommandlineOptions(final Options options) {
  Option opt = new Option("b", "brokerAddr", true, "update which broker");
  opt.setRequired(false);
  options.addOption(opt);
  opt = new Option("c", "clusterName", true, "update which cluster");
  opt.setRequired(false);
  options.addOption(opt);
  return options;
}

代码示例来源:origin: OpenHFT/Chronicle-Queue

public static void addOption(final Options options, final String opt, final String argName, final boolean hasArg,
               final String description, final boolean isRequired) {
  final Option option = new Option(opt, hasArg, description);
  option.setArgName(argName);
  option.setRequired(isRequired);
  options.addOption(option);
}

代码示例来源:origin: apache/shiro

private static int getRequiredPositiveInt(CommandLine line, Option option) {
  String iterVal = line.getOptionValue(option.getOpt());
  try {
    return Integer.parseInt(iterVal);
  } catch (NumberFormatException e) {
    String msg = "'" + option.getLongOpt() + "' value must be a positive integer.";
    throw new IllegalArgumentException(msg, e);
  }
}

代码示例来源:origin: floragunncom/search-guard

public static void main(final String[] args) {
  final Options options = new Options();
  final HelpFormatter formatter = new HelpFormatter();
  options.addOption(Option.builder("p").argName("password").hasArg().desc("Cleartext password to hash").build());
  options.addOption(Option.builder("env").argName("name environment variable").hasArg().desc("name environment variable to read password from").build());
    final CommandLine line = parser.parse(options, args);
    if(line.hasOption("p")) {
      System.out.println(hash(line.getOptionValue("p").toCharArray()));
    } else if(line.hasOption("env")) {
      final String pwd = System.getenv(line.getOptionValue("env"));
      if(pwd == null || pwd.isEmpty()) {
    formatter.printHelp("hasher.sh", options, true);
    System.exit(-1);

代码示例来源:origin: apache/zookeeper

private static TxnLogToolkit parseCommandLine(String[] args) throws TxnLogToolkitException, FileNotFoundException {
  CommandLineParser parser = new PosixParser();
  Options options = new Options();
  Option helpOpt = new Option("h", "help", false, "Print help message");
  options.addOption(helpOpt);
  Option recoverOpt = new Option("r", "recover", false, "Recovery mode. Re-calculate CRC for broken entries.");
  options.addOption(recoverOpt);
  Option quietOpt = new Option("v", "verbose", false, "Be verbose in recovery mode: print all entries, not just fixed ones.");
  options.addOption(quietOpt);
  Option dumpOpt = new Option("d", "dump", false, "Dump mode. Dump all entries of the log file with printing the content of a nodepath (default)");
  options.addOption(dumpOpt);
  Option forceOpt = new Option("y", "yes", false, "Non-interactive mode: repair all CRC errors without asking");
  options.addOption(forceOpt);
  Option chopOpt = new Option("c", "chop", false, "Chop mode. Chop txn file to a zxid.");
  Option zxidOpt = new Option("z", "zxid", true, "Used with chop. Zxid to which to chop.");
  options.addOption(chopOpt);
  options.addOption(zxidOpt);
    if (cli.getArgs().length < 1) {
      printHelpAndExit(1, options);
    if (cli.hasOption("chop") && cli.hasOption("zxid")) {
      return new TxnLogToolkit(cli.getArgs()[0], cli.getOptionValue("zxid"));

代码示例来源:origin: galenframework/galen

public static GalenActionGenerateArguments parse(String[] args) {
  args = ArgumentsUtils.processSystemProperties(args);
  Options options = new Options();
  options.addOption("e", "export", true, "Path to generated spec file");
  options.addOption("G", "no-galen-extras", false, "Disable galen-extras expressions");
  CommandLineParser parser = new PosixParser();
  CommandLine cmd;
  try {
    cmd = parser.parse(options, args);
  } catch (MissingArgumentException e) {
    throw new IllegalArgumentException("Missing value for " + e.getOption().getLongOpt(), e);
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
  GalenActionGenerateArguments arguments = new GalenActionGenerateArguments();
  arguments.setExport(cmd.getOptionValue("e"));
  arguments.setUseGalenExtras(!cmd.hasOption("G"));
  if (cmd.getArgs() == null || cmd.getArgs().length < 1) {
    throw new IllegalArgumentException("Missing page dump file");
  }
  arguments.setPath(cmd.getArgs()[0]);
  return arguments;
}

代码示例来源:origin: commons-cli/commons-cli

Option help = new Option("h", "help", false, "print this message");
Option version = new Option("v", "version", false, "print version information");
Option newRun = new Option("n", "new", false, "Create NLT cache entries only for new items");
Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only for tracker items");
Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg()
                .withValueSeparator()
                .withDescription("Set time limit for execution, in minutes")
                .create("l");
                 .create();
Options options = new Options();
options.addOption(help);
options.addOption(version);
options.addOption(newRun);
options.addOption(trackerRun);
CommandLineParser parser = new PosixParser();
  };
CommandLine line = parser.parse(options, args);
assertTrue(line.hasOption("v"));
assertEquals(line.getOptionValue("l"), "10");
assertEquals(line.getOptionValue("limit"), "10");
assertEquals(line.getOptionValue("a"), "5");
assertEquals(line.getOptionValue("age"), "5");

代码示例来源:origin: apache/hive

/**
 * Create an instance with common options (help, verbose, etc...).
 *
 * @param cliname the name of the command
 * @param includeHiveConf include "hiveconf" as an option if true
 */
@SuppressWarnings("static-access")
public CommonCliOptions(String cliname, boolean includeHiveConf) {
 this.cliname = cliname;
 // [-v|--verbose]
 OPTIONS.addOption(new Option("v", "verbose", false, "Verbose mode"));
 // [-h|--help]
 OPTIONS.addOption(new Option("h", "help", false, "Print help information"));
 if (includeHiveConf) {
  OPTIONS.addOption(OptionBuilder
    .withValueSeparator()
    .hasArgs(2)
    .withArgName("property=value")
    .withLongOpt("hiveconf")
    .withDescription("Use value for given property")
    .create());
 }
}

代码示例来源:origin: commons-cli/commons-cli

@Test
  public void testCLI18()
  {
    Options options = new Options();
    options.addOption(new Option("a", "aaa", false, "aaaaaaa"));
    options.addOption(new Option(null, "bbb", false, "bbbbbbb dksh fkshd fkhs dkfhsdk fhskd hksdks dhfowehfsdhfkjshf skfhkshf sf jkshfk sfh skfh skf f"));
    options.addOption(new Option("c", null, false, "ccccccc"));

    HelpFormatter formatter = new HelpFormatter();
    StringWriter out = new StringWriter();

    formatter.printHelp(new PrintWriter(out), 80, "foobar", "dsfkfsh kdh hsd hsdh fkshdf ksdh fskdh fsdh fkshfk sfdkjhskjh fkjh fkjsh khsdkj hfskdhf skjdfh ksf khf s", options, 2, 2, "blort j jgj j jg jhghjghjgjhgjhg jgjhgj jhg jhg hjg jgjhghjg jhg hjg jhgjg jgjhghjg jg jgjhgjgjg jhg jhgjh" + '\r' + '\n' + "rarrr", true);
  }
}

代码示例来源:origin: commons-cli/commons-cli

@Test
public void test31148() throws ParseException
{
  Option multiArgOption = new Option("o","option with multiple args");
  multiArgOption.setArgs(1);
  
  Options options = new Options();
  options.addOption(multiArgOption);
  
  Parser parser = new PosixParser();
  String[] args = new String[]{};
  Properties props = new Properties();
  props.setProperty("o","ovalue");
  CommandLine cl = parser.parse(options,args,props);
  
  assertTrue(cl.hasOption('o'));
  assertEquals("ovalue",cl.getOptionValue('o'));
}

代码示例来源:origin: galenframework/galen

public static GalenActionConfigArguments parse(String[] args) {
  args = ArgumentsUtils.processSystemProperties(args);
  Options options = new Options();
  options.addOption("g", "global", false, "Flag to create global config in user home directory");
  CommandLineParser parser = new PosixParser();
  CommandLine cmd;
  try {
    cmd = parser.parse(options, args);
  } catch (MissingArgumentException e) {
    throw new IllegalArgumentException("Missing value for " + e.getOption().getLongOpt(), e);
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
  GalenActionConfigArguments configArguments = new GalenActionConfigArguments();
  configArguments.isGlobal = cmd.hasOption("g");
  return configArguments;
}

代码示例来源:origin: commons-cli/commons-cli

@Before
public void setUp() {
  options = new Options();
  Option algorithm = new Option("a" , "algo", true, "the algorithm which it to perform executing");
  algorithm.setArgName("algorithm name");
  options.addOption(algorithm);
  Option key = new Option("k" , "key", true, "the key the setted algorithm uses to process");
  algorithm.setArgName("value");
  options.addOption(key);
  parser = new PosixParser();
}

代码示例来源:origin: apache/incubator-gobblin

/**
 * Parse command line.
 */
public static CommandLine parseCommandLine(Options options, String[] args) throws ParseException {
 CommandLineParser parser = new DefaultParser();
 CommandLine cli = parser.parse(options, args);
 if (cli.hasOption(StressTestUtils.HELP_OPT.getOpt())) {
  HelpFormatter formatter = new HelpFormatter();
  formatter.printHelp( MRStressTest.class.getSimpleName(), OPTIONS);
  System.exit(0);
 }
 return cli;
}

代码示例来源:origin: commons-cli/commons-cli

@Test
public void testPrintSortedUsageWithNullComparator()
{
  Options opts = new Options();
  opts.addOption(new Option("c", "first"));
  opts.addOption(new Option("b", "second"));
  opts.addOption(new Option("a", "third"));
  HelpFormatter helpFormatter = new HelpFormatter();
  helpFormatter.setOptionComparator(null);
  StringWriter out = new StringWriter();
  helpFormatter.printUsage(new PrintWriter(out), 80, "app", opts);
  assertEquals("usage: app [-c] [-b] [-a]" + EOL, out.toString());
}

代码示例来源:origin: code4craft/webmagic

private static Params parseCommand(String[] args) {
  try {
    Options options = new Options();
    options.addOption(new Option("l", "language", true, "language"));
    options.addOption(new Option("t", "thread", true, "thread"));
    options.addOption(new Option("f", "file", true, "script file"));
    options.addOption(new Option("i", "input", true, "input file"));
    options.addOption(new Option("s", "sleep", true, "sleep time"));
    options.addOption(new Option("g", "logger", true, "sleep time"));
    CommandLineParser commandLineParser = new PosixParser();
    CommandLine commandLine = commandLineParser.parse(options, args);
    return readOptions(commandLine);
  } catch (Exception e) {
    e.printStackTrace();
    exit();
    return null;
  }
}

代码示例来源:origin: internetarchive/heritrix3

public static void main(String [] args)
  throws ParseException, IOException, java.text.ParseException {
    Options options = new Options();
    options.addOption(new Option("h","help", false,
      "Prints this message and exits."));
    options.addOption(new Option("f","force", false,
       "Force overwrite of target file."));
    PosixParser parser = new PosixParser();
    CommandLine cmdline = parser.parse(options, args, false);
    List<String> cmdlineArgs = cmdline.getArgList();
    Option [] cmdlineOptions = cmdline.getOptions();
    HelpFormatter formatter = new HelpFormatter();
      switch(cmdlineOptions[i].getId()) {
        case 'h':
          usage(formatter, options, 0);
            + cmdlineOptions[i].getId());

代码示例来源:origin: commons-cli/commons-cli

@Test
public void test11458() throws Exception
  CommandLineParser parser = new PosixParser();
  CommandLine cmd = parser.parse(options, args);
  String[] values = cmd.getOptionValues('D');
  values = cmd.getOptionValues('p');
  assertEquals(values[2], "file3");
  Iterator<Option> iter = cmd.iterator();
  while (iter.hasNext())
    switch (opt.getId())
        assertEquals(opt.getValue(0), "JAVA_HOME");
        assertEquals(opt.getValue(1), "/opt/java");
        break;
      case 'p':
        assertEquals(opt.getValue(0), "file1");
        assertEquals(opt.getValue(1), "file2");
        assertEquals(opt.getValue(2), "file3");
        break;
      default:

相关文章