Skip to content

Commit

Permalink
Merge branch 'ask-server-from-user-mikko' of github.com:tmc-cli/tmc-c…
Browse files Browse the repository at this point in the history
…li into ask-server-from-user-mikko
  • Loading branch information
mikkomaa committed Jun 21, 2016
2 parents 3a230df + 2068233 commit f4108de
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 104 deletions.
File renamed without changes.
76 changes: 31 additions & 45 deletions src/main/java/fi/helsinki/cs/tmc/cli/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
import fi.helsinki.cs.tmc.cli.command.core.AbstractCommand;
import fi.helsinki.cs.tmc.cli.command.core.CommandFactory;
import fi.helsinki.cs.tmc.cli.io.Color;
import fi.helsinki.cs.tmc.cli.io.HelpGenerator;
import fi.helsinki.cs.tmc.cli.io.Io;
import fi.helsinki.cs.tmc.cli.io.TerminalIo;

import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfo;
import fi.helsinki.cs.tmc.cli.tmcstuff.CourseInfoIo;
import fi.helsinki.cs.tmc.cli.tmcstuff.Settings;
import fi.helsinki.cs.tmc.cli.tmcstuff.SettingsIo;
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
import fi.helsinki.cs.tmc.cli.updater.TmcCliUpdater;

import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.domain.Course;
import fi.helsinki.cs.tmc.langs.util.TaskExecutor;
import fi.helsinki.cs.tmc.langs.util.TaskExecutorImpl;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
Expand All @@ -29,9 +29,11 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
Expand All @@ -43,6 +45,7 @@ public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
private static final String previousUpdateDateKey = "update-date";
private static final long defaultUpdateInterval = 60 * 60 * 1000;
private static final String usage = "tmc [args] COMMAND [command-args]";

private CommandFactory commandFactory;
private HashMap<String, String> properties;
Expand All @@ -56,6 +59,7 @@ public class Application {

private Options options;
private GnuParser parser;
private String commandName;

public Application(Io io) {
this.parser = new GnuParser();
Expand All @@ -82,18 +86,6 @@ public Application(Io io, WorkDir workDir) {
this.workDir = workDir;
}

/**
* Find first argument that isn't flag.
*/
private int findCommand(String[] args) {
for (int i = 0; i < args.length; i++) {
if (!args[i].startsWith("-")) {
return i;
}
}
return -1;
}

private boolean runCommand(String name, String[] args) {
AbstractCommand command = commandFactory.createCommand(this, name);
if (command == null) {
Expand All @@ -105,61 +97,55 @@ private boolean runCommand(String name, String[] args) {
return true;
}

private boolean parseArgs(String commandName, String[] args) {
private String[] parseArgs(String[] args) {
CommandLine line;
try {
line = this.parser.parse(this.options, args);
line = this.parser.parse(this.options, args, true);
} catch (ParseException e) {
io.println("Invalid command line arguments.");
io.println(e.getMessage());
return false;
return null;
}

List<String> subArgs = new ArrayList<>(Arrays.asList(line.getArgs()));
if (subArgs.size() > 0) {
commandName = subArgs.remove(0);
} else {
commandName = "help";
}

if (commandName.startsWith("-")) {
io.println("Unrecognized option: " + commandName);
return null;
}

if (line.hasOption("h")) {
// don't run the help sub-command with -h switch
if (commandName.equals("help")) {
runCommand(commandName, new String[0]);
return false;
runCommand("help", new String[0]);
return null;
}
runCommand(commandName, new String[]{"-h"});
return false;
return null;
}
if (line.hasOption("v")) {
io.println("TMC-CLI version " + getVersion());
return false;
return null;
}
return true;
return subArgs.toArray(new String[subArgs.size()]);
}

public void printHelp() {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("tmc-cli", this.options);
public void printHelp(String description) {
HelpGenerator.run(io, usage, description, this.options);
}

public void run(String[] args) {
if (!inTest) {
versionCheck();
}

String[] tmcArgs;
String[] commandArgs;
String commandName;

int commandIndex = findCommand(args);

if (commandIndex != -1) {
commandName = args[commandIndex];

/* split the arguments to the tmc's and command's arguments */
tmcArgs = Arrays.copyOfRange(args, 0, commandIndex);
commandArgs = Arrays.copyOfRange(args, commandIndex + 1, args.length);

} else {
commandName = "help";
tmcArgs = args;
commandArgs = new String[0];
}

if (!parseArgs(commandName, tmcArgs)) {
String[] commandArgs = parseArgs(args);
if (commandArgs == null) {
return;
}

Expand Down
57 changes: 36 additions & 21 deletions src/main/java/fi/helsinki/cs/tmc/cli/command/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Command(name = "help", desc = "List every command")
public class HelpCommand extends AbstractCommand {
private final int longestName = 9; // Length of the longest command name
private CommandFactory commands;
private int longestNameLength;
private CommandFactory commandFactory;
private Io io;

@Override
public void getOptions(Options options) {
Expand All @@ -25,43 +27,56 @@ public void getOptions(Options options) {
@Override
public void run(CommandLine args, Io io) {
Application app = getApp();
this.commands = app.getCommandFactory();
this.commandFactory = app.getCommandFactory();
this.io = io;

StringBuilder sb = new StringBuilder();
sb.append("TMC commands:\n");

io.println("Usage: tmc [args] COMMAND [command-args]\n");
io.println("TMC commands:");

List<String> commandStrings = getCommandStrings();
Collections.sort(commandStrings);
for (String commandString : commandStrings) {
io.println(commandString);
sb.append(commandString).append("\n");
}

io.println("");
app.printHelp();

app.printHelp(sb.toString());
}

private List<String> getCommandStrings() {
List<String> strings = new ArrayList<>();
for (Class<Command> commandClass : this.commands.getCommands()) {
Set<Class<Command>> commands = this.commandFactory.getCommands();
commands.remove(castToCommandClass(TestCommand.class));
commands.remove(castToCommandClass(ShellHelperCommand.class));

longestNameLength = longestName(commands);
for (Class<Command> commandClass : commands) {
Command command = CommandFactory.getCommand(commandClass);
if ((Class)commandClass == (Class)TestCommand.class) {
continue;
}
if ((Class)commandClass == (Class)ShellHelperCommand.class) {
continue;
}
strings.add(createCommandString(command));
}
return strings;
}


@SuppressWarnings("unchecked")
private Class<Command> castToCommandClass(Class klass) {
return (Class<Command>)klass;
}

private String createCommandString(Command command) {
StringBuilder builder = new StringBuilder();
builder.append(" ").append(command.name());
for (int i = 0; i < longestName - command.name().length() + 2; i++) {
for (int i = 0; i < longestNameLength - command.name().length() + 1; i++) {
builder.append(" ");
}
builder.append(command.desc());
return builder.toString();
}
}

private int longestName(Set<Class<Command>> commandList) {
int longest = 0;
for (Class<Command> commandClass : commandList) {
Command command = CommandFactory.getCommand(commandClass);
longest = Math.max(longest, command.name().length());
}
return longest;
}
}
10 changes: 5 additions & 5 deletions src/main/java/fi/helsinki/cs/tmc/cli/command/PasteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void run(CommandLine args, Io io) {
return;
}
WorkDir workdir = app.getWorkDir();
List<String> argsList = args.getArgList();
String[] stringArgs = args.getArgs();

// if (argsList.size() > 1) {
// io.println(
Expand All @@ -53,13 +53,13 @@ public void run(CommandLine args, Io io) {
// return;
// }
Boolean valid;
if (argsList.isEmpty()) {
if (stringArgs.length == 0) {
valid = workdir.addPath();
} else if (argsList.size() == 1) {
valid = workdir.addPath(argsList.get(0));
} else if (stringArgs.length == 1) {
valid = workdir.addPath(stringArgs[0]);
} else {
io.println(
"Error: Too many arguments. Expected 1, got " + argsList.size());
"Error: Too many arguments. Expected 1, got " + stringArgs.length);
return;
}
if (!valid) {
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/fi/helsinki/cs/tmc/cli/command/PropertiesCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ public void getOptions(Options options) {
public void run(CommandLine args, Io io) {
this.io = io;
Boolean unset = args.hasOption("u");
List<String> arguments = args.getArgList();
String[] arguments = args.getArgs();
HashMap<String, String> props = getApp().getProperties();
if (arguments.isEmpty()) {
if (arguments.length == 0) {
printAllProps(props);
return;
}

if (arguments.size() % 2 == 1 && !unset) {
if (arguments.length % 2 == 1 && !unset) {
io.println("Invalid argument count. Usage: tmc prop KEY VALUE ...");
return;
}

if (unset) {
if (arguments.size() > 1) {
if (arguments.length > 1) {
io.print("Unsetting property keys:");
for (String arg : arguments) {
io.print(" " + arg);
Expand All @@ -58,25 +58,23 @@ public void run(CommandLine args, Io io) {
io.println("Unset key " + key + ", was " + props.remove(key));
}
getApp().saveProperties();
return;
} else {
if (arguments.size() > 2) {
if (arguments.length > 2) {
io.print("Setting property keys:");
for (int i = 0; i < arguments.size(); i = i + 2) {
io.print(" " + arguments.get(i) + "=>" + arguments.get(i + 1));
for (int i = 0; i < arguments.length; i = i + 2) {
io.print(" " + arguments[i] + "=>" + arguments[i + 1]);
}
io.println("");
if (!io.readConfirmation("Are you sure?", true)) {
return;
}
}
for (int i = 0; i < arguments.size(); i = i + 2) {
String last = props.put(arguments.get(i), arguments.get(i + 1));
io.println("Set " + arguments.get(i) + "=>" + arguments.get(i + 1)
for (int i = 0; i < arguments.length; i = i + 2) {
String last = props.put(arguments[i], arguments[i + 1]);
io.println("Set " + arguments[i] + "=>" + arguments[i + 1]
+ ", was " + last);
}
getApp().saveProperties();
return;
}
}

Expand Down Expand Up @@ -107,9 +105,7 @@ private void printAllProps(HashMap<String, String> props) {

private int longestKey(HashMap<String, String> props) {
int longest = 0;
Iterator iterator = props.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
for (String key : props.keySet()) {
longest = Math.max(longest, key.length());
}
return longest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void getOptions(Options options) {
public void run(CommandLine args, Io io) {
// this.io = io;

List<String> exercisesFromArgs = parseArgs(args);
String[] exercisesFromArgs = parseArgs(args);
if (exercisesFromArgs == null) {
return;
}
Expand Down Expand Up @@ -123,9 +123,9 @@ private String getCourseName(WorkDir dirUtil) {
return null;
}

private List<String> parseArgs(CommandLine args) {
private String[] parseArgs(CommandLine args) {
this.showPassed = args.hasOption("a");
this.showDetails = args.hasOption("d");
return args.getArgList();
return args.getArgs();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void getOptions(Options options) {
public void run(CommandLine args, Io io) {
this.io = io;

List<String> exercisesFromArgs = parseArgs(args);
String[] exercisesFromArgs = parseArgs(args);
if (exercisesFromArgs == null) {
return;
}
Expand Down Expand Up @@ -133,9 +133,9 @@ public void checkForExerciseUpdates(TmcCore core, Course course) {
io.println(Color.colorString(msg, Color.AnsiColor.ANSI_YELLOW));
}

private List<String> parseArgs(CommandLine args) {
private String[] parseArgs(CommandLine args) {
this.showAll = args.hasOption("a");
this.showDetails = args.hasOption("d");
return args.getArgList();
return args.getArgs();
}
}
Loading

0 comments on commit f4108de

Please sign in to comment.