Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 74fdd71

Browse files
author
Aleksi Salmela
committed
Separate the argument handling of paste.
1 parent d1b5a5e commit 74fdd71

2 files changed

Lines changed: 37 additions & 28 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/command/PasteCommand.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,12 @@ public void getOptions(Options options) {
4747
public void run(CliContext context, CommandLine args) {
4848
this.ctx = context;
4949
this.io = ctx.getIo();
50-
51-
this.message = args.getOptionValue("m");
52-
this.noMessage = args.hasOption("n");
53-
this.openInBrowser = args.hasOption("o");
5450
WorkDir workdir = ctx.getWorkDir();
55-
String[] stringArgs = args.getArgs();
5651

57-
if (noMessage && message != null) {
58-
io.errorln("You can't have the no-message flag and message set at the same time.");
59-
printUsage(context);
60-
return;
61-
}
62-
if (stringArgs.length > 1) {
63-
io.errorln("Error: Too many arguments.");
64-
printUsage(context);
52+
if(!parseArgs(args)) {
6553
return;
6654
}
6755

68-
if (stringArgs.length == 1) {
69-
if (!workdir.addPath(stringArgs[0])) {
70-
io.errorln("The path '" + stringArgs[0] + "' is not valid exercise.");
71-
return;
72-
}
73-
} else {
74-
//TODO replace the following call with workdir.getCurrentExercise()
75-
if (!workdir.addPath()) {
76-
io.errorln("You are not in exercise directory.");
77-
return;
78-
}
79-
}
80-
8156
if (!ctx.loadBackend()) {
8257
return;
8358
}
@@ -104,6 +79,40 @@ public void run(CliContext context, CommandLine args) {
10479
sendPaste(message, exerciseNames.get(0));
10580
}
10681

82+
private boolean parseArgs(CommandLine args) {
83+
WorkDir workdir = ctx.getWorkDir();
84+
String[] stringArgs = args.getArgs();
85+
86+
this.message = args.getOptionValue("m");
87+
this.noMessage = args.hasOption("n");
88+
this.openInBrowser = args.hasOption("o");
89+
90+
if (noMessage && message != null) {
91+
io.errorln("You can't have the no-message flag and message set at the same time.");
92+
printUsage(ctx);
93+
return false;
94+
}
95+
if (stringArgs.length > 1) {
96+
io.errorln("Error: Too many arguments.");
97+
printUsage(ctx);
98+
return false;
99+
}
100+
101+
if (stringArgs.length == 1) {
102+
if (!workdir.addPath(stringArgs[0])) {
103+
io.errorln("The path '" + stringArgs[0] + "' is not valid exercise.");
104+
return false;
105+
}
106+
} else {
107+
//TODO replace the following call with workdir.getCurrentExercise()
108+
if (!workdir.addPath()) {
109+
io.errorln("You are not in exercise directory.");
110+
return false;
111+
}
112+
}
113+
return true;
114+
}
115+
107116
private void sendPaste(String message, String exerciseName) {
108117
if (message == null) {
109118
message = "";

src/test/java/fi/helsinki/cs/tmc/cli/command/PasteCommandTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void pasteRunsRightWithoutArguments() throws URISyntaxException {
127127
@Test
128128
public void pasteRunsRightwithTooManyArguments() {
129129
app.run(new String[] {"paste", "paste-exercise", "secondArgument"});
130-
io.assertContains("Error: Too many arguments. Expected 1, got");
130+
io.assertContains("Error: Too many arguments.");
131131
}
132132

133133
@Test
@@ -202,7 +202,7 @@ public void failsWithNoExercise() {
202202
Mockito.when(workDir.addPath(anyString())).thenReturn(false);
203203
app.run(new String[] {"paste", "-m", "This is a message given as an argument"});
204204

205-
io.assertContains("The command can be used in an exercise directory");
205+
io.assertContains("You are not in exercise directory.");
206206

207207
verifyStatic(Mockito.never());
208208
TmcUtil.sendPaste(eq(ctx), any(Exercise.class), anyString());

0 commit comments

Comments
 (0)