import java.util.HashMap;
import java.util.Map;
public class CommandLineEncoder {
public static String encode(Map<String, String> options) {
StringBuilder encodedString = new StringBuilder();
for (Map.Entry<String, String> entry : options.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// Encode key and value by escaping special characters
String encodedKey = escapeString(key);
String encodedValue = escapeString(value);
// Append encoded key-value pair to the encoded string
encodedString.append(encodedKey).append("=").append(encodedValue).append(";");
}
// Remove the trailing semicolon
if (encodedString.length() > 0) {
encodedString.deleteCharAt(encodedString.length() - 1);
}
return encodedString.toString();
}
private static String escapeString(String str) {
if (str == null) {
return "";
}
return str.replace("\"", "\\\""); // Escape double quotes
}
public static void main(String[] args) {
// Example usage
Map<String, String> commandLineOptions = new HashMap<>();
commandLineOptions.put("name", "John \"Doe\"");
commandLineOptions.put("age", "30");
commandLineOptions.put("city", "New York");
String encodedOutput = encode(commandLineOptions);
System.out.println(encodedOutput);
}
}
Add your comment