in disco-java-agent-instrumentation-preprocess/src/main/java/software/amazon/disco/instrumentation/preprocess/cli/PreprocessConfigParser.java [40:97]
public PreprocessConfig parseCommandLine(String[] args) {
if (args == null || args.length == 0) {
throw new ArgumentParserException("Mandatory options not supplied, please use [--help] to get a list of all options supported by this CLI.");
}
setupAcceptedFlags();
PreprocessConfig.PreprocessConfigBuilder builder = PreprocessConfig.builder();
OptionToMatch flagBeingMatched = null;
for (String arg : args) {
final String argLowered = arg.toLowerCase();
if (flagBeingMatched == null) {
// no previous flag found, expecting a flag
if (!ACCEPTED_FLAGS.containsKey(argLowered)) {
throw new ArgumentParserException("Flag: [" + arg + "] is invalid");
}
final OptionToMatch option = ACCEPTED_FLAGS.get(argLowered);
if (option.hasArgument) {
flagBeingMatched = ACCEPTED_FLAGS.get(argLowered);
} else {
processFlagWithNoArg(argLowered, builder);
}
} else {
// previous flag still expecting an argument but another flag is discovered
if (ACCEPTED_FLAGS.containsKey(argLowered) && !flagBeingMatched.isMatched()) {
throw new ArgumentParserException("Flag: [" + flagBeingMatched.getFlag() + "] requires an argument");
}
// a previously detected option that accepts multi values is now finished matching its arguments.
// and a new option is now being matched
if (ACCEPTED_FLAGS.containsKey(argLowered) && flagBeingMatched.isMatched()) {
final OptionToMatch option = ACCEPTED_FLAGS.get(argLowered);
if (option.hasArgument) {
flagBeingMatched = option;
} else {
processFlagWithNoArg(argLowered, builder);
}
continue;
}
if (flagBeingMatched.hasArgument) {
flagBeingMatched = matchArgWithFlag(flagBeingMatched, arg, builder);
}
}
}
// the last flag discovered is missing its arg
if (flagBeingMatched != null && !flagBeingMatched.isMatched) {
throw new ArgumentParserException("Flag: [" + flagBeingMatched.getFlag() + "] requires an argument");
}
return builder.build();
}