Java Code Examples for picocli.CommandLine.Model.ArgSpec#isOption()

The following examples show how to use picocli.CommandLine.Model.ArgSpec#isOption() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptionalOptionParamDemo.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Override
public void consumeParameters(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) {
    String arg = args.pop();
    try {
        int port = Integer.parseInt(arg);
        argSpec.setValue(port);
    } catch (Exception ex) {
        String fallbackValue = (argSpec.isOption()) ? ((OptionSpec) argSpec).fallbackValue() : null;
        try {
            int fallbackPort = Integer.parseInt(fallbackValue);
            argSpec.setValue(fallbackPort);
        } catch (Exception badFallbackValue) {
            throw new InitializationException("FallbackValue for --debug must be an int", badFallbackValue);
        }
        args.push(arg); // put it back
    }
}
 
Example 2
Source File: TomlConfigFileDefaultProvider.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public String defaultValue(final ArgSpec argSpec) {
  loadConfigurationFromFile();

  // only options can be used in config because a name is needed for the key
  // so we skip default for positional params
  return argSpec.isOption() ? getConfigurationValue(((OptionSpec) argSpec)) : null;
}
 
Example 3
Source File: EnvironmentVariableDefaultProvider.java    From teku with Apache License 2.0 5 votes vote down vote up
@Override
public String defaultValue(final ArgSpec argSpec) {
  if (!argSpec.isOption()) {
    return null;
  }
  return envVarNames((OptionSpec) argSpec)
      .map(environment::get)
      .filter(Objects::nonNull)
      .findFirst()
      .orElse(null);
}
 
Example 4
Source File: YamlConfigFileDefaultProvider.java    From teku with Apache License 2.0 5 votes vote down vote up
@Override
public String defaultValue(final ArgSpec argSpec) {
  if (result == null) {
    result = loadConfigurationFromFile();
    checkConfigurationValidity(result == null || result.isEmpty());
    checkUnknownOptions(result);
  }

  // only options can be used in config because a name is needed for the key
  // so we skip default for positional params
  return argSpec.isOption() ? getConfigurationValue(((OptionSpec) argSpec)) : null;
}
 
Example 5
Source File: PropertiesDefaultProvider.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Override
public String defaultValue(ArgSpec argSpec) throws Exception {
    if (properties == null) {
        properties = loadProperties(argSpec.command());
    }
    if (properties == null || properties.isEmpty()) {
        return null;
    }
    return argSpec.isOption()
            ? optionDefaultValue((OptionSpec) argSpec)
            : positionalDefaultValue((PositionalParamSpec) argSpec);
}