下面列出了org.apache.commons.cli.HelpFormatter#printUsage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void showUsage(HelpFormatter format, PrintWriter err, String name, Command command) {
Options options = mergeOptions(OPTIONS, command.getOptions());
String[] usage = command.getUsageDescription();
String ustr = name + " [option...]";
if (usage != null && usage.length >= 1) {
ustr = ustr + " " + usage[0];
}
format.printWrapped(err, WIDTH, name + ":\n" + command.getCommandDescription());
format.printUsage(err, WIDTH, ustr);
format.printWrapped(err, WIDTH, LEFT_PAD, "where option is one of:");
format.printOptions(err, WIDTH, options, LEFT_PAD, DESC_PAD);
if (usage != null && usage.length >= 2) {
for (int i = 1; i < usage.length; ++i) {
format.printWrapped(err, WIDTH, LEFT_PAD, usage[i]);
}
}
}
public static void showUsage(HelpFormatter format, PrintWriter err, String name, Command command) {
Options options = mergeOptions(OPTIONS, command.getOptions());
String[] usage = command.getUsageDescription();
String ustr = name + " [option...]";
if (usage != null && usage.length >= 1) {
ustr = ustr + " " + usage[0];
}
format.printUsage(err, WIDTH, ustr);
format.printWrapped(err, WIDTH, LEFT_PAD, "where option is one of:");
format.printOptions(err, WIDTH, options, LEFT_PAD, DESC_PAD);
if (usage != null && usage.length >= 2) {
for (int i = 1; i < usage.length; ++i) {
format.printWrapped(err, WIDTH, LEFT_PAD, usage[i]);
}
}
}
private static String getOptionsHelpForUsage() {
HelpFormatter formatter = new HelpFormatter();
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter( stringWriter );
formatter.printUsage( printWriter, 999, "", options );
return stripOff( stringWriter.toString(), "usage: " ); // Strip off the "usage:" so it can be localized
}
private static void printUsage(final Options options) {
final HelpFormatter formatter = new HelpFormatter();
final String syntax = "mlplan";
final PrintWriter pw = new PrintWriter(System.out);
formatter.printUsage(pw, 400, syntax, options);
pw.println("use -h or --help for help");
pw.flush();
}
private static void usage(HelpFormatter help, PrintWriter pw, CommandLineParser cli, Options opts) {
StringWriter sw = new StringWriter();
PrintWriter upw = new PrintWriter(sw);
help.printUsage(upw, HelpFormatter.DEFAULT_WIDTH, "", opts);
if (cli instanceof TreeParser) {
upw.print(" ");
TreeParser tp = (TreeParser) cli;
String[] keys = tp.getParsers().keySet().toArray(new String[tp.getParsers().size()]);
if (keys.length > 0 && !tp.isRequired()) {
upw.print("[ ");
}
for (int i = 0; i < keys.length; ++i) {
String key = keys[i];
ParserOptions value = tp.getParsers().get(key);
upw.print("(" + key);
usage(help, upw, value.getParser(), value.getOptions());
upw.print(")");
if (i != keys.length - 1) {
upw.print(" | ");
}
}
if (keys.length > 0 && !tp.isRequired()) {
upw.print(" ]");
}
Argument[] args = tp.getArgs().toArray(new Argument[tp.getArgs().size()]);
if (args.length > 0) {
String[] argss = new String[tp.getArgs().size()];
for (int i = 0; i < args.length; ++i) {
Argument arg = args[i];
argss[i] = arg.isRequired() ? "<" + arg.getName() + ">" : "[" + arg.getName() + "]";
}
upw.print(" " + String.join(" ", argss));
}
}
pw.println(sw.toString().replaceAll("usage:( )?", "").replace("\n", ""));
}
private static String getOptionsHelpForUsage() {
HelpFormatter formatter = new HelpFormatter();
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter( stringWriter );
formatter.printUsage( printWriter, 999, "", options );
return stripOff( stringWriter.toString(), "usage: " ); // Strip off the "usage:" so it can be localized
}
private void printUsage(final Options options) {
final HelpFormatter formatter = new HelpFormatter();
formatter.printUsage(this.writer, CHARACTERS_PER_LINE, SingleTaskRunnerMain.class.getSimpleName(), options);
}
/**
* Prints the command line usage to the std error output
*
* @param e
* The error that raised the help message
* @param cmdLine
* The command line usage
* @param options
* The options expected
*/
public static void printUsage(ParseException e, String cmdLine,
Options options) {
System.err.println(e.getMessage());
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
PrintWriter pw = new PrintWriter(System.err);
formatter.printUsage(pw, cmdLine.length() + 7, cmdLine, options);
pw.flush();
}