Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Challenge #187 [Easy] A Flagon of Flags
package com.christianwilkie.rdailyprogrammer.c187p1.easy;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Created by chris on 11/8/2014.
*/
public class ChallengeRunner {
public static void main(String[] args) {
CommandLineArgumentParser commandLineArgumentParser = new CommandLineArgumentParser();
Scanner sc = new Scanner(System.in);
//get number of short form definitions
int numShortFormDefinitions = 0;
try {
numShortFormDefinitions = Integer.parseInt(sc.nextLine());
} catch(NumberFormatException e) {
e.printStackTrace();
}
List<String> rawShortFormDefinitions = new ArrayList<>(numShortFormDefinitions);
//add short form definitions
for(int i = 0; i < numShortFormDefinitions; i++) {
rawShortFormDefinitions.add(sc.nextLine());
}
//get raw argument line data
String rawArgumentLine = sc.nextLine();
//input done, now parse
List<String> output = commandLineArgumentParser.parseCommandLineArguments(rawShortFormDefinitions,rawArgumentLine);
//print the output
output.forEach(System.out::println);
}
}
package com.christianwilkie.rdailyprogrammer.c187p1.easy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by chris on 11/8/2014.
*/
public class CommandLineArgumentParser {
private Map<String, String> shortFormDefinitionMap;
private boolean errorFlag = false;
private static String errorOutputString = "Internal error in argument parsing.";
public CommandLineArgumentParser() {
shortFormDefinitionMap = new HashMap<>();
}
public List<String> parseCommandLineArguments(List<String> rawShortFormDefinitions, String rawArgumentLine) {
List<String> output;
//parse all short form definitions
rawShortFormDefinitions.forEach(this::parseShortFormDefinition);
//parse the argument lines and form the output
output = parseRawArgumentLine(rawArgumentLine);
//if there was an error at some point, return bad output info
if (errorFlag == true) {
List<String> badOutput = new ArrayList<>(1);
badOutput.add(errorOutputString);
return badOutput;
}
//no errors, return good info
return output;
}
private void parseShortFormDefinition(String shortFormDefinition) {
String[] splitShortFormDefinition = shortFormDefinition.split(":");
if(splitShortFormDefinition.length != 2) {
errorFlag = true;
} else {
shortFormDefinitionMap.put(splitShortFormDefinition[0],splitShortFormDefinition[1]);
}
}
private List<String> parseRawArgumentLine(String rawArgumentLine) {
List<String> output = new ArrayList<>();
String[] splitArguments = rawArgumentLine.split(" ");
for(String rawArgument: splitArguments) {
//add each parsed argument to the output
parseCommandLineArgument(rawArgument).forEach(output::add);
}
return output;
}
private List<String> parseCommandLineArgument(String rawArgument) {
List<String> parsedArguments = new ArrayList<>();
if(rawArgument.startsWith("--")) {
//full argument
parsedArguments.add("flag: " + rawArgument);
} else if (rawArgument.startsWith("-")) {
//condensed argument
//split up into chars in case there is more than 1 argument, also skip the '-'
char[] argumentChars = rawArgument.substring(1).toCharArray();
for(char argument: argumentChars) {
//check for definition
String fullDefinition = shortFormDefinitionMap.get(String.valueOf(argument));
if (fullDefinition == null) {
errorFlag = true;
parsedArguments.add("");
} else {
parsedArguments.add("flag: " + fullDefinition);
}
}
} else {
//parameter
parsedArguments.add("parameter: " + rawArgument);
}
return parsedArguments;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment