private void printSingleLineOption(PrintWriter pw, OptionHandler h, ResourceBundle rb) { pw.print(' '); if (!h.option.required()) pw.print('['); pw.print(h.getNameAndMeta(rb, parserProperties)); if (h.option.isMultiValued()) { pw.print(" ..."); } if (!h.option.required()) pw.print(']'); } }
public boolean select(OptionHandler o) { return !o.option.hidden(); } };
/** * Get string representing usage for this option, of the form "name metaval" or "name=metaval, * e.g. "--foo VALUE" or "--foo=VALUE" * @param rb ResourceBundle to get localized version of meta string * @param properties * Affects the formatting behaviours. */ public final String getNameAndMeta(ResourceBundle rb, ParserProperties properties) { String str = option.isArgument() ? "" : option.toString(); String meta = getMetaVariable(rb); if (meta != null) { if (str.length() > 0) { str += properties.getOptionValueDelimiter(); } str += meta; } return str; }
private void checkRequiredOptionsAndArguments(Set<OptionHandler> present) throws CmdLineException { // make sure that all mandatory options are present for (OptionHandler handler : options) { if(handler.option.required() && !present.contains(handler)) { throw new CmdLineException(this, Messages.REQUIRED_OPTION_MISSING, handler.option.toString()); } } // make sure that all mandatory arguments are present for (OptionHandler handler : arguments) { if(handler.option.required() && !present.contains(handler)) { throw new CmdLineException(this, Messages.REQUIRED_ARGUMENT_MISSING, handler.option.toString()); } } //make sure that all requires arguments are present for (OptionHandler handler : present) { if (handler.option instanceof NamedOptionDef && !isHandlerHasHisOptions((NamedOptionDef)handler.option, present)) { throw new CmdLineException(this, Messages.REQUIRES_OPTION_MISSING, handler.option.toString(), Arrays.toString(((NamedOptionDef)handler.option).depends())); } } //make sure that all forbids arguments are not present for (OptionHandler handler : present) { if (handler.option instanceof NamedOptionDef && !isHandlerAllowOtherOptions((NamedOptionDef) handler.option, present)) { throw new CmdLineException(this, Messages.FORBIDDEN_OPTION_PRESENT, handler.option.toString(), Arrays.toString(((NamedOptionDef) handler.option).forbids())); } } }
public MyOptionDef(OptionDef o) { super(o.usage(), o.metaVar(), o.required(), o.handler(), o .isMultiValued()); }
/** * Default constructor. * @param parser the parser * @param option the option definition * @param setter the setter * @throws CmdLineException */ public ProxyOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Object> setter) throws CmdLineException { super(parser, option, setter); OptionDef proxyOption = new OptionDef(option.usage(), option.metaVar(), option.required(), OptionHandler.class, option.isMultiValued()); proxy = parser.createOptionHandler(proxyOption, setter); if (!option.required() && CmdLineOptionsProvider.class.isAssignableFrom(this.setter.getType())) { handleExtraArgs(); } }
/** * This constructor is required by the args4j framework. * * @param parser * @param option * @param setter */ public CommandHandler(final CmdLineParser parser, final OptionDef option, final Setter<Object> setter) { super(parser, new OptionDef(AllCommands.names(), "<command>", option.required(), option.help(), option.hidden(), CommandHandler.class, option.isMultiValued()) { }, setter); }
throw new IllegalStateException("@Argument with index="+argIndex+" is undefined"); if (!currentOptionHandler.option.isMultiValued()) argIndex++; if(handler.option.help() && present.contains(handler)) { helpSet = true;
public boolean select(OptionHandler o) { return o.option.required(); } }
private String getOptionName() { return currentOptionHandler.option.toString(); }
private int getPrefixLen(OptionHandler h, ResourceBundle rb) { if(h.option.usage().length()==0) return 0; return h.getNameAndMeta(rb, parserProperties).length(); }
private static void writeOptions(final XMLElement parent, @SuppressWarnings("rawtypes") final List<OptionHandler> list) throws IOException { for (final OptionHandler<?> o : list) { final XMLElement optionNode = parent.element("option"); optionNode.attr("required", String.valueOf(o.option.required())); optionNode.attr("multiple", String.valueOf(o.setter.isMultiValued())); optionNode.element("usage").text(o.getNameAndMeta(null)); optionNode.element("description").text(o.option.usage()); } }
@Override public int parseArguments(final Parameters params) throws CmdLineException { String name = params.getParameter(0); boolean interesting = true; if (name.startsWith("^")) { //$NON-NLS-1$ name = name.substring(1); interesting = false; } final int dot2 = name.indexOf(".."); //$NON-NLS-1$ if (dot2 != -1) { if (!option.isMultiValued()) throw new CmdLineException(MessageFormat.format(CLIText.get().onlyOneMetaVarExpectedIn , option.metaVar(), name)); final String left = name.substring(0, dot2); final String right = name.substring(dot2 + 2); addOne(left, false); addOne(right, true); return 1; } addOne(name, interesting); return 1; }
/** * Creates an {@link OptionHandler} that handles the given {@link Option} annotation * and the {@link Setter} instance. */ @SuppressWarnings("unchecked") protected OptionHandler createOptionHandler(CmdLineParser parser, OptionDef o, Setter setter) { checkNonNull(o, "CmdLineParser"); checkNonNull(o, "OptionDef"); checkNonNull(setter, "Setter"); Class<? extends OptionHandler> h = o.handler(); if(h==OptionHandler.class) { // infer the type Class<?> t = setter.getType(); // enum is the special case if(Enum.class.isAssignableFrom(t)) return new EnumOptionHandler(parser,o,setter,t); OptionHandlerFactory factory = handlers.get(t); if (factory==null) throw new IllegalAnnotationError(Messages.UNKNOWN_HANDLER.format(t)); return factory.getHandler(parser, o, setter); } else { // explicit handler specified return new DefaultConstructorHandlerFactory(h).getHandler(parser, o, setter); } }
/** * Programmatically defines an argument (instead of reading it from annotations as normal). * * @param setter the setter for the type * @param a the Argument * @throws NullPointerException if {@code setter} or {@code a} is {@code null}. */ public void addArgument(Setter setter, Argument a) { checkNonNull(setter, "Setter"); checkNonNull(a, "Argument"); OptionHandler h = createOptionHandler(new OptionDef(a,setter.isMultiValued()),setter); int index = a.index(); // make sure the argument will fit in the list while (index >= arguments.size()) { arguments.add(null); } if(arguments.get(index)!=null) { throw new IllegalAnnotationError(Messages.MULTIPLE_USE_OF_ARGUMENT.format(index)); } arguments.set(index, h); }
/** * Prints the usage. * * @param parser - The command line parser. */ public static void printUsage(final CmdLineParser parser) { System.out.println("reqcoco-runner [options...] arguments..."); parser.printUsage(System.out); System.out.println(); System.out.println(" Example: reqcoco-runner " + parser.printExample(filter -> !filter.option.help())); } }
@Override public int parseArguments(Parameters params) throws CmdLineException { if (option.isArgument()) { String valueStr = params.getParameter(0).toLowerCase(); int index = ACCEPTABLE_VALUES.indexOf(valueStr); if (index == -1) { throw new CmdLineException(owner, Messages.ILLEGAL_BOOLEAN, valueStr); } setter.addValue(index < ACCEPTABLE_VALUES.size() / 2); return 1; } else { setter.addValue(true); return 0; } }
/** * Default constructor. * @param parser the parser * @param option the option definition * @param setter the setter * @throws CmdLineException */ public ProxyOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Object> setter) throws CmdLineException { super(parser, option, setter); OptionDef proxyOption = new OptionDef(option.usage(), option.metaVar(), option.required(), OptionHandler.class, option.isMultiValued()); proxy = parser.createOptionHandler(proxyOption, setter); if (!option.required() && CmdLineOptionsProvider.class.isAssignableFrom(this.setter.getType())) { handleExtraArgs(); } }
throw new IllegalStateException("@Argument with index="+argIndex+" is undefined"); if (!currentOptionHandler.option.isMultiValued()) argIndex++; if(handler.option.help() && present.contains(handler)) { helpSet = true;