下面列出了怎么用org.apache.commons.cli2.util.HelpFormatter的API类实例代码及写法,或者点击链接到github查看源代码。
protected HelpFormatter getAppHelpFormatter() {
CliHelpFormatter hf = CliHelpFormatter.create();
StringBuffer sep = new StringBuffer(hf.getPageWidth());
while (sep.length() < hf.getPageWidth()) {
sep.append("-");
}
hf.setHeader(getVersionString());
hf.setDivider(sep.toString());
hf.setShellCommand(" " + getShellCommand() + " [options] <command> [arg1 [arg2 [arg3] ..]]");
hf.setGroup(getApplicationCLGroup());
hf.setSkipToplevel(true);
hf.getFullUsageSettings().removeAll(DisplaySetting.ALL);
hf.getDisplaySettings().remove(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
hf.getDisplaySettings().remove(DisplaySetting.DISPLAY_PARENT_CHILDREN);
hf.getDisplaySettings().add(DisplaySetting.DISPLAY_OPTIONAL);
hf.getLineUsageSettings().add(DisplaySetting.DISPLAY_PROPERTY_OPTION);
hf.getLineUsageSettings().add(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
hf.getLineUsageSettings().add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
return hf;
}
protected HelpFormatter getCmdHelpFormatter(CliCommand cmd) {
CliHelpFormatter hf = CliHelpFormatter.create();
if (cmd != null) {
hf.setCmd(cmd);
hf.getLineUsageSettings().add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
} else {
hf.setGroup(getCommandsGroup());
hf.setShowUsage(false);
hf.getDisplaySettings().remove(DisplaySetting.DISPLAY_PARENT_CHILDREN);
}
return hf;
}
public static CliHelpFormatter create() {
return new CliHelpFormatter(HelpFormatter.DEFAULT_GUTTER_LEFT,
HelpFormatter.DEFAULT_GUTTER_CENTER,
HelpFormatter.DEFAULT_GUTTER_RIGHT,
getDefaultWidth());
}
private static int getDefaultWidth() {
int w = Integer.getInteger(SYS_PROP_TERM_WIDTH, HelpFormatter.DEFAULT_FULL_WIDTH).intValue();
return Math.max(w, HelpFormatter.DEFAULT_FULL_WIDTH);
}