Moved command line logic to its own file.

This commit is contained in:
Andrew Lalis 2018-07-01 07:52:52 +02:00
parent c3b1534695
commit fc7052b95c
2 changed files with 80 additions and 65 deletions

View File

@ -2,13 +2,13 @@ package nl.andrewlalis;
import nl.andrewlalis.model.Team; import nl.andrewlalis.model.Team;
import nl.andrewlalis.util.TeamGenerator; import nl.andrewlalis.util.TeamGenerator;
import org.apache.commons.cli.*;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import nl.andrewlalis.util.CommandLine;
/** /**
* Main program entry point. * Main program entry point.
*/ */
@ -18,7 +18,7 @@ public class Main {
System.out.println("Initializer for Github Repositories in Educational Organizations."); System.out.println("Initializer for Github Repositories in Educational Organizations.");
Map<String, String> userOptions = parseArgs(args); Map<String, String> userOptions = CommandLine.parseArgs(args);
List<Team> teams = TeamGenerator.generateFromCSV( List<Team> teams = TeamGenerator.generateFromCSV(
userOptions.get("input"), userOptions.get("input"),
@ -28,66 +28,4 @@ public class Main {
} }
/**
* Parses the command line arguments and gets all the needed values.
* @param args The command line arguments as they are given to main.
* @return A map of keys and values for each option that the user must give.
*/
private static Map<String, String> parseArgs(String[] args) {
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
Options options = setupCommandOptions();
Map<String, String> userOptions = new HashMap<>();
try {
CommandLine cmd = parser.parse(options, args);
userOptions.put("token", cmd.getOptionValue("token"));
userOptions.put("input", cmd.getOptionValue("input"));
userOptions.put("organization", cmd.getOptionValue("organization"));
// The optional teamsize argument must be handled.
String teamSizeInput = cmd.getOptionValue("teamsize");
System.out.println(teamSizeInput);
if (teamSizeInput == null ) {
userOptions.put("teamsize", "2");
} else {
userOptions.put("teamsize", teamSizeInput);
}
} catch (ParseException e) {
formatter.printHelp("java -jar GithubInitializer.jar", options);
System.exit(1);
}
return userOptions;
}
/**
* Sets up the command line interface options using Apache Commons CLI library.
* @return The Options object used when parsing the arguments.
*/
private static Options setupCommandOptions() {
Options options = new Options();
// Authentication token for github.
Option tokenInput = new Option("t", "token", true, "The authentication token, with which you are authenticated on Github. See the Github OAuth information section for more information.");
tokenInput.setRequired(true);
options.addOption(tokenInput);
// CSV file of responses to form.
Option fileInput = new Option("i", "input", true, "The input file. Should be in CSV format with the following columns:\n" +
"\t Two Student numbers, two Github usernames, two email addresses, and \n" +
"\t whether the first student has a partner.");
fileInput.setRequired(true);
options.addOption(fileInput);
// The github organization to add the repositories to.
Option organizationInput = new Option("o", "organization", true, "The name of the organization for which this program is being run.");
organizationInput.setRequired(true);
options.addOption(organizationInput);
// The maximum team size.
Option teamSizeInput = new Option("s", "teamsize", true, "The maximum size of teams to generate.");
teamSizeInput.setRequired(false);
options.addOption(teamSizeInput);
return options;
}
} }

View File

@ -0,0 +1,77 @@
package nl.andrewlalis.util;
import org.apache.commons.cli.*;
import java.util.HashMap;
import java.util.Map;
/**
* Contains some utility methods to make getting command line arguments easier.
* The basic function of this class is to make it so that in Main, the program needs only to receive a Map object
* containing all the options the user has specified.
*/
public class CommandLine {
/**
* Parses the command line arguments and gets all the needed values.
* @param args The command line arguments as they are given to main.
* @return A map of keys and values for each option that the user must give.
*/
public static Map<String, String> parseArgs(String[] args) {
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
Options options = setupCommandOptions();
Map<String, String> userOptions = new HashMap<>();
try {
org.apache.commons.cli.CommandLine cmd = parser.parse(options, args);
userOptions.put("token", cmd.getOptionValue("token"));
userOptions.put("input", cmd.getOptionValue("input"));
userOptions.put("organization", cmd.getOptionValue("organization"));
// The optional teamsize argument must be handled.
String teamSizeInput = cmd.getOptionValue("teamsize");
System.out.println(teamSizeInput);
if (teamSizeInput == null ) {
userOptions.put("teamsize", "2");
} else {
userOptions.put("teamsize", teamSizeInput);
}
} catch (ParseException e) {
formatter.printHelp("java -jar GithubInitializer.jar", options);
System.exit(1);
}
return userOptions;
}
/**
* Sets up the command line interface options using Apache Commons CLI library.
* @return The Options object used when parsing the arguments.
*/
private static Options setupCommandOptions() {
Options options = new Options();
// Authentication token for github.
Option tokenInput = new Option("t", "token", true, "The authentication token, with which you are authenticated on Github. See the Github OAuth information section for more information.");
tokenInput.setRequired(true);
options.addOption(tokenInput);
// CSV file of responses to form.
Option fileInput = new Option("i", "input", true, "The input file. Should be in CSV format with the following columns:\n" +
"\t Two Student numbers, two Github usernames, two email addresses, and \n" +
"\t whether the first student has a partner.");
fileInput.setRequired(true);
options.addOption(fileInput);
// The github organization to add the repositories to.
Option organizationInput = new Option("o", "organization", true, "The name of the organization for which this program is being run.");
organizationInput.setRequired(true);
options.addOption(organizationInput);
// The maximum team size.
Option teamSizeInput = new Option("s", "teamsize", true, "The maximum size of teams to generate.");
teamSizeInput.setRequired(false);
options.addOption(teamSizeInput);
return options;
}
}