Skip to content

Commit 2092e04

Browse files
command-line argument works
1 parent cb8fa97 commit 2092e04

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
plugins {
22
id 'java'
3+
id 'application'
4+
id 'com.github.johnrengelman.shadow' version '5.1.0'
35
}
46

57
group 'de.saar.coli'
68
version '1.0-SNAPSHOT'
79

8-
sourceCompatibility = 1.8
10+
mainClassName = 'de.saar.coli.arranger.Arrange'
11+
12+
913

1014
repositories {
1115
mavenCentral()
@@ -18,6 +22,7 @@ dependencies {
1822
compile 'com.ganderband:abcj:1.8'
1923
compile group: 'com.google.guava', name: 'guava', version: '28.1-jre'
2024
compile 'org.yaml:snakeyaml:1.25'
25+
compile group: 'com.beust', name: 'jcommander', version: '1.78'
2126
testCompile group: 'junit', name: 'junit', version: '4.12'
2227
}
2328

src/main/java/de/saar/coli/arranger/Arrange.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.saar.coli.arranger;
22

3+
import com.beust.jcommander.JCommander;
4+
import com.beust.jcommander.Parameter;
35
import com.google.common.collect.ArrayListMultimap;
46
import com.google.common.collect.ListMultimap;
57
import de.saar.coli.arranger.abc.AbcParser;
@@ -29,13 +31,25 @@ public Arrange(Config config) {
2931

3032
public static void main(String[] args) throws IOException, AbcParser.AbcParsingException {
3133
Config config = Config.read(new InputStreamReader(Arrange.class.getResourceAsStream("/config.yaml")));
32-
Score score = new AbcParser().read(new FileReader("down_our_way.abc"));
34+
Args arguments = new Args();
35+
JCommander jc = JCommander.newBuilder().addObject(arguments).build();
36+
jc.parse(args);
37+
38+
if(arguments.help) {
39+
jc.usage();
40+
System.exit(0);
41+
}
42+
43+
System.out.printf("Reading melody and chords from: %s\n", arguments.inputFilename);
44+
System.out.printf("Writing arrangement to: %s\n\n", arguments.outputFilename);
45+
46+
Score score = new AbcParser().read(new FileReader(arguments.inputFilename));
3347

3448
Arrange arranger = new Arrange(config);
3549
Score bestArrangedScore = arranger.arrange(score);
3650

3751
AbcWriter abcw = new AbcWriter();
38-
FileWriter fw = new FileWriter("arranged.abc");
52+
FileWriter fw = new FileWriter(arguments.outputFilename);
3953
abcw.write(bestArrangedScore, fw);
4054
fw.flush();
4155
fw.close();
@@ -108,7 +122,7 @@ private Score arrange(Score score) {
108122
List<Map.Entry<Item, Integer>> sortedEntries = new ArrayList<>(bestScores.entrySet());
109123
Collections.sort(sortedEntries, Comparator.comparing(Map.Entry::getValue));
110124
Map.Entry<Item, Integer> bestGoalItem = sortedEntries.get(sortedEntries.size() - 1);
111-
System.out.printf("Best arrangement has score %d\n", bestGoalItem.getValue());
125+
System.out.printf("Best arrangement has score %d.\n", bestGoalItem.getValue());
112126

113127
Score bestArrangedScore = extractBestScore(bestGoalItem.getKey(), backpointers, score);
114128
return bestArrangedScore;
@@ -321,4 +335,16 @@ public int hashCode() {
321335
return Arrays.hashCode(x);
322336
}
323337
}
338+
339+
public static class Args {
340+
@Parameter(description = "Name of the input file (*.abc).", required = true)
341+
private String inputFilename = null;
342+
343+
@Parameter(names = {"--output", "-o"}, description="Name of the output file (*.abc).")
344+
private String outputFilename = "arranged.abc";
345+
346+
@Parameter(names = "--help", description="Display usage instructions.", help = true)
347+
private boolean help;
348+
349+
}
324350
}

0 commit comments

Comments
 (0)