From fc7052b95ce62016449ca01ca608ba2ab34fd078 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sun, 1 Jul 2018 07:52:52 +0200 Subject: [PATCH] Moved command line logic to its own file. --- src/main/java/nl/andrewlalis/Main.java | 68 +--------------- .../java/nl/andrewlalis/util/CommandLine.java | 77 +++++++++++++++++++ 2 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/util/CommandLine.java diff --git a/src/main/java/nl/andrewlalis/Main.java b/src/main/java/nl/andrewlalis/Main.java index a85ad3c..ae73668 100644 --- a/src/main/java/nl/andrewlalis/Main.java +++ b/src/main/java/nl/andrewlalis/Main.java @@ -2,13 +2,13 @@ package nl.andrewlalis; import nl.andrewlalis.model.Team; import nl.andrewlalis.util.TeamGenerator; -import org.apache.commons.cli.*; import java.io.IOException; -import java.util.HashMap; import java.util.List; import java.util.Map; +import nl.andrewlalis.util.CommandLine; + /** * Main program entry point. */ @@ -18,7 +18,7 @@ public class Main { System.out.println("Initializer for Github Repositories in Educational Organizations."); - Map userOptions = parseArgs(args); + Map userOptions = CommandLine.parseArgs(args); List teams = TeamGenerator.generateFromCSV( 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 parseArgs(String[] args) { - CommandLineParser parser = new DefaultParser(); - HelpFormatter formatter = new HelpFormatter(); - Options options = setupCommandOptions(); - Map 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; - } } diff --git a/src/main/java/nl/andrewlalis/util/CommandLine.java b/src/main/java/nl/andrewlalis/util/CommandLine.java new file mode 100644 index 0000000..8e4e206 --- /dev/null +++ b/src/main/java/nl/andrewlalis/util/CommandLine.java @@ -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 parseArgs(String[] args) { + CommandLineParser parser = new DefaultParser(); + HelpFormatter formatter = new HelpFormatter(); + Options options = setupCommandOptions(); + Map 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; + } + +}