private void handleOption(Option option) throws CLIException { // check the previous option before handling the next one checkRequiredValues(); updateRequiredOptions(option); //IMPORTANT for flag we must set this attributes as it will determine the value out of it. commandLine.setSeenInCommandLine(option); if (commandLine.acceptMoreValues(option)) { current = option; } else { current = null; } }
/** * Creates an instance of {@link CLI} using the default implementation. * * @param name the name of the CLI (must not be {@code null}) * @return the created instance of {@link CLI} */ static CLI create(String name) { return new DefaultCLI().setName(name); }
/** * Parses the user command line interface and create a new {@link CommandLine} containing extracting values. * * @param arguments the arguments * @return the creates command line */ @Override public CommandLine parse(List<String> arguments) { return new DefaultParser().parse(this, arguments); }
protected void handleConcatenatedOptions(String token) throws CLIException { for (int i = 1; i < token.length(); i++) { String ch = String.valueOf(token.charAt(i)); if (hasOptionWithShortName(ch)) { handleOption(getOption(ch)); if (current != null && token.length() != i + 1) { // add the trail as an argument of the option commandLine.addRawValue(current, token.substring(i + 1)); break; } } else { handleArgument(token); break; } } }
/** * Tells if the token looks like an option. * * @param token the token * @return {@code true} if the token represents an option */ private boolean isOption(String token) { return isLongOption(token) || isShortOption(token); }
/** * Returns true is the token is a valid value. * * @param token the token * @return {@code true} if the token represents a value */ private boolean isValue(String token) { return !isOption(token) || isNegativeNumber(token); }
private boolean isAValidShortOption(String token) { String opt = token.substring(0, 1); Option option = getOption(opt); return option != null && commandLine.acceptMoreValues(option); }
@Override @SuppressWarnings("unchecked") public <T> T getArgumentValue(int index) { Argument arg = cli.getArgument(index); if (arg == null) { return null; } if (arg instanceof TypedArgument) { return create(getRawValueForArgument(arg), (TypedArgument<T>) arg); } else { return (T) getRawValueForArgument(arg); } }
@Override @SuppressWarnings("unchecked") public <T> T getOptionValue(String name) { Option option = cli.getOption(name); if (option == null) { return null; } if (option instanceof TypedOption) { return getValue((TypedOption<T>) option); } else { return (T) getRawValueForOption(option); } }
/** * @return a new instance of the command by invoking the default constructor of the given class. */ @Override public C create(CommandLine cl) { C c = ReflectionUtils.newInstance(clazz); CLIConfigurator.inject(cl, c); return c; }
@Test public void testNegativeArgument() throws Exception { String[] args = new String[]{"-b", "-1"}; CommandLine evaluated = cli.parse(Arrays.asList(args)); assertThat(getStringOption(evaluated, "b")).isEqualTo("-1"); }
/** * Creates a command line object from the {@link CLI}. This object is intended to be used by * the parser to set the argument and option values. * * @param cli the CLI definition * @return the command line object */ static CommandLine create(CLI cli) { return new DefaultCommandLine(cli); }
public CommandLine parse(CLI cli, List<String> cla) throws CLIException { return parse(cli, cla, true); }
private void handleArgument(String token) { commandLine.addArgumentValue(token); }
@Override public boolean isOptionAssigned(Option option) { return !getRawValuesForOption(option).isEmpty(); }
/** * Tells if the token looks like a short option. * * @param token the token * @return {@code true} if the token represents an option (short name match) */ private boolean isShortOption(String token) { // short options (-S, -SV, -S=V, -SV1=V2, -S1S2) return token.startsWith("-") && token.length() >= 2 && hasOptionWithShortName(token.substring(1, 2)); }
@Override public boolean acceptMoreValues(Option option) { return option.isMultiValued() || option.isSingleValued() && !isOptionAssigned(option); }
@Before public void setUp() { cli = new DefaultCLI().setName("test"); }
/** * Parses the user command line interface and create a new {@link CommandLine} containing extracting values. * * @param arguments the arguments * @param validate enable / disable parsing validation * @return the creates command line */ @Override public CommandLine parse(List<String> arguments, boolean validate) { return new DefaultParser().parse(this, arguments, validate); }
@Before public void setUp() { cli = new DefaultCLI().setName("test"); }