import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DryRunMatcher {
public static Map<String, Boolean> matchOptions(String commandLine) {
Map<String, Boolean> options = new HashMap<>();
// Define patterns for each option. Case-insensitive.
Pattern dryRunPattern = Pattern.compile("^-d\\s+(.*)$", Pattern.CASE_INSENSITIVE); // -d
Pattern verbosePattern = Pattern.compile("^-v\\s+(.*)$", Pattern.CASE_INSENSITIVE); // -v
Pattern quietPattern = Pattern.compile("^-q\\s+(.*)$", Pattern.CASE_INSENSITIVE); // -q
Pattern helpPattern = Pattern.compile("^-h\\s+(.*)$", Pattern.CASE_INSENSITIVE); // -h
// Check for dry-run option
Matcher dryRunMatcher = dryRunPattern.matcher(commandLine);
if (dryRunMatcher.find()) {
options.put("dryRun", true);
}
// Check for verbose option
Matcher verboseMatcher = verbosePattern.matcher(commandLine);
if (verboseMatcher.find()) {
options.put("verbose", true);
}
// Check for quiet option
Matcher quietMatcher = quietPattern.matcher(commandLine);
if (quietMatcher.find()) {
options.put("quiet", true);
}
// Check for help option
Matcher helpMatcher = helpPattern.matcher(commandLine);
if (helpMatcher.find()) {
options.put("help", true);
}
return options;
}
public static void main(String[] args) {
String commandLine1 = "-d --some-other-option";
String commandLine2 = "-v -q";
String commandLine3 = "-h";
String commandLine4 = "some command";
String commandLine5 = "-D --dry-run"; //Test case insensitivity
System.out.println("Command Line 1: " + commandLine1 + " -> " + matchOptions(commandLine1));
System.out.println("Command Line 2: " + commandLine2 + " -> " + matchOptions(commandLine2));
System.out.println("Command Line 3: " + commandLine3 + " -> " + matchOptions(commandLine3));
System.out.println("Command Line 4: " + commandLine4 + " -> " + matchOptions(commandLine4));
System.out.println("Command Line 5: " + commandLine5 + " -> " + matchOptions(commandLine5));
}
}
Add your comment