下面列出了org.apache.commons.cli.Option#hasLongOpt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void logOptions(String sectionName, String prefix, Options options)
{
if ( sectionName != null )
{
log.info("== " + sectionName + " ==");
}
//noinspection unchecked
for ( Option option : (Iterable<? extends Option>)options.getOptions() )
{
if ( option.hasLongOpt() )
{
if ( option.hasArg() )
{
log.info(prefix + option.getLongOpt() + " <arg> - " + option.getDescription());
}
else
{
log.info(prefix + option.getLongOpt() + " - " + option.getDescription());
}
}
}
}
@SuppressWarnings("unchecked")
@Override
protected StringBuffer renderOptions(final StringBuffer sb, final int width, final Options options, final int leftPad, final int descPad) {
final String lpad = this.createPadding(leftPad);
final String dpad = this.createPadding(8); // we use a fixed value instead of descPad
StringBuilder optBuf;
final List<Option> optList = new ArrayList<Option>(options.getOptions());
Collections.sort(optList, this.getOptionComparator());
for (final Iterator<Option> i = optList.iterator(); i.hasNext();) {
final Option option = i.next();
optBuf = new StringBuilder(8);
if (option.getOpt() == null) {
optBuf.append(lpad).append(" ").append(this.getLongOptPrefix()).append(option.getLongOpt());
} else {
optBuf.append(lpad).append(this.getOptPrefix()).append(option.getOpt());
if (option.hasLongOpt()) {
optBuf.append(',').append(this.getLongOptPrefix()).append(option.getLongOpt());
}
}
if (option.hasArg()) {
if (option.hasArgName()) {
optBuf.append(" <").append(option.getArgName()).append('>');
} else {
optBuf.append(' ');
}
}
sb.append(optBuf.toString()).append(this.getNewLine());
optBuf = new StringBuilder();
optBuf.append(dpad);
if (option.getDescription() != null) {
optBuf.append(option.getDescription());
}
this.renderWrappedText(sb, width, dpad.length(), optBuf.toString());
if (i.hasNext()) {
sb.append(this.getNewLine());
sb.append(this.getNewLine());
}
}
return sb;
}
public String opt(Option option) {
return option.hasLongOpt()
? option.getLongOpt()
: option.getOpt();
}
/** Render the option help to a Markdown table.
*
* @param options the options.
* @return the markdown table.
*/
protected static String _renderToMarkdown(Options options) {
if (options == null) {
return ""; //$NON-NLS-1$
}
final List<Option> optList = new ArrayList<>(options.getOptions());
if (optList.isEmpty()) {
return ""; //$NON-NLS-1$
}
Collections.sort(optList, new OptionComparator());
final StringBuilder buffer = new StringBuilder();
for (final Option option : optList) {
buffer.append("| `"); //$NON-NLS-1$
if (option.getOpt() == null) {
buffer.append(DEFAULT_LONG_OPT_PREFIX).append(option.getLongOpt());
} else {
buffer.append(DEFAULT_OPT_PREFIX).append(option.getOpt());
if (option.hasLongOpt()) {
buffer.append("`, `"); //$NON-NLS-1$
buffer.append(DEFAULT_LONG_OPT_PREFIX).append(option.getLongOpt());
}
}
if (option.hasArg()) {
if (option.hasArgName()) {
buffer.append(" <").append(option.getArgName()).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
buffer.append("` | "); //$NON-NLS-1$
if (option.getDescription() != null) {
String text = option.getDescription().replaceAll("[ \t\n\r\f]+", " "); //$NON-NLS-1$ //$NON-NLS-2$
text = text.replaceAll("\\<", "<"); //$NON-NLS-1$//$NON-NLS-2$
text = text.replaceAll("\\>", ">"); //$NON-NLS-1$//$NON-NLS-2$
buffer.append(text);
}
buffer.append(" |\n"); //$NON-NLS-1$
}
return buffer.toString();
}