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

Commit 2f98307

Browse files
author
Aleksi Salmela
committed
Implement the get option.
1 parent f5395c6 commit 2f98307

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ public class ConfigCommand extends AbstractCommand {
2626

2727
@Override
2828
public String[] getUsages() {
29-
return new String[] {"[-q|--quiet] KEY=\"VALUE\"...", "-d [-q|--quiet] KEY...", "-l"};
29+
return new String[] {
30+
"[-q|--quiet] KEY=\"VALUE\"...",
31+
"-d|--delete [-q|--quiet] KEY...",
32+
"-l|--list",
33+
"-g|--get=KEY"};
3034
}
3135

3236
@Override
3337
public void getOptions(Options options) {
34-
options.addOption("g", "get", false, "Get value of a key");
38+
options.addOption("g", "get", true, "Get value of a key");
3539
options.addOption("d", "delete", false, "Unset given property keys");
3640
options.addOption("q", "quiet", false, "Don't ask confirmations");
3741
options.addOption("l", "list", false, "List all properties");
@@ -60,13 +64,28 @@ public void run(CliContext context, CommandLine args) {
6064
if (listing) {
6165
if (arguments.length != 0) {
6266
io.errorln("Listing option doesn't take any arguments.");
67+
printUsage(context);
6368
return;
6469
}
6570
printAllProperties();
6671
return;
6772
}
6873

69-
if (delete) {
74+
if(get) {
75+
if (arguments.length != 0) {
76+
io.errorln("There should not be extra arguments when using --get option.");
77+
printUsage(context);
78+
return;
79+
}
80+
String key = args.getOptionValue('g');
81+
boolean exists = properties.containsKey(key);
82+
if (!exists && !quiet) {
83+
io.errorln("The property " + key + " doesn't exist.");
84+
return;
85+
}
86+
io.println(exists ? properties.get(key) : "");
87+
return;
88+
} else if (delete) {
7089
deleteProperties(arguments);
7190
} else {
7291
setProperties(arguments);
@@ -113,7 +132,7 @@ private void deleteProperties(String[] keys) {
113132
}
114133
for (String key : keys) {
115134
String oldValue = properties.remove(key);
116-
io.println("Unset key " + key + ", was " + oldValue);
135+
io.println("Deleted key " + key + ", was " + oldValue);
117136
}
118137
}
119138

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public void printsErrorIfNoArgumentsGiven() {
3939

4040
@Test
4141
public void printsErrorIfTwoConflictingOptionsGiven() {
42-
app.run(new String[] {"config", "--get", "--list"});
42+
app.run(new String[] {"config", "--delete", "--list"});
4343
io.assertContains("Only one of the");
4444
}
4545

4646
@Test
4747
public void printsErrorIfThreeConflictingOptionsGiven() {
48-
app.run(new String[] {"config", "--get", "--list", "--delete"});
48+
app.run(new String[] {"config", "--delete", "--list", "--get", "property"});
4949
io.assertContains("Only one of the");
5050
}
5151

@@ -58,6 +58,43 @@ public void listsAllProperties() {
5858
io.assertContains("toilet=wonderland");
5959
}
6060

61+
@Test
62+
public void listsAllPropertiesWithExtraArgument() {
63+
props.put("hello", "world");
64+
props.put("toilet", "wonderland");
65+
app.run(new String[] {"config", "--list", "abc"});
66+
io.assertContains("Listing option doesn't take any arguments.");
67+
}
68+
69+
@Test
70+
public void getProperty() {
71+
props.put("thing", "value");
72+
app.run(new String[] {"config", "--get", "thing"});
73+
io.assertContains("value");
74+
io.assertAllPromptsUsed();
75+
}
76+
77+
@Test
78+
public void getUnexistingProperty() {
79+
app.run(new String[] {"config", "--get", "thing"});
80+
io.assertContains("The property thing doesn't exist");
81+
io.assertAllPromptsUsed();
82+
}
83+
84+
@Test
85+
public void getUnexistingPropertyQuietly() {
86+
app.run(new String[] {"config", "--get", "-q", "thing"});
87+
io.assertNotContains("The property thing doesn't exist");
88+
io.assertAllPromptsUsed();
89+
}
90+
91+
@Test
92+
public void getPropertyWithExtraArgument() {
93+
props.put("thing", "value");
94+
app.run(new String[] {"config", "--get", "thing", "abc"});
95+
io.assertContains("There should not be extra arguments when using --get option.");
96+
}
97+
6198
@Test
6299
public void setsOnePropertyWhenNoOptionsGiven() {
63100
io.addConfirmationPrompt(true);
@@ -122,6 +159,25 @@ public void deletesOnePropertyQuietly() {
122159
io.assertAllPromptsUsed();
123160
}
124161

162+
@Test
163+
public void deletesInvalidProperty() {
164+
app.run(new String[] {"config", "-d", "property"});
165+
assertTrue(!props.containsKey("no"));
166+
io.assertContains("Key property doesn't exist.");
167+
io.assertAllPromptsUsed();
168+
}
169+
170+
@Test
171+
public void deletesOnePropertyWithNoConfirmation() {
172+
io.addConfirmationPrompt(false);
173+
props.put("no", "e");
174+
app.run(new String[] {"config", "-d", "no"});
175+
assertTrue(props.containsKey("no"));
176+
io.assertContains("Deleting 1 properties.");
177+
io.assertNotContains("Deleted key no, was");
178+
io.assertAllPromptsUsed();
179+
}
180+
125181
@Test
126182
public void deletesMultiplePropertiesCorrectly() {
127183
io.addConfirmationPrompt(true);

0 commit comments

Comments
 (0)