Skip to content

Commit 6c66f79

Browse files
committed
closes #19: Add output option to diff.
* Added output option.
1 parent b6295e5 commit 6c66f79

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

diff/src/main/java/diffr/diff/Main.java

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package diffr.diff;
22

3+
import com.google.common.base.Optional;
34
import com.google.common.io.Files;
5+
import diffr.util.ArgumentsProcessor;
46
import diffr.util.instruction.Instruction;
57
import diffr.util.instruction.InstructionComposer;
8+
import diffr.util.instruction.Instructions;
69

10+
import java.io.BufferedWriter;
711
import java.io.File;
12+
import java.io.FileWriter;
813
import java.io.IOException;
914
import java.nio.charset.Charset;
1015
import java.util.List;
1116

1217
/**
1318
* Main entry point to diffr's DIFF tool.
14-
*
19+
* <p/>
1520
* <p>
1621
* Expects two arguments:
1722
* <ul>
@@ -30,7 +35,9 @@ public final class Main {
3035
* Prints the usage of this tool.
3136
*/
3237
private static void printUsage() {
33-
System.out.println("Usage: diffr <original-file> <new-file>");
38+
System.out.println("Usage: \n" +
39+
" diffr <original-file> <new-file>\n" +
40+
" diffr <original-file> <new-file> -o <output-file>");
3441
}
3542

3643
/**
@@ -41,8 +48,9 @@ private static void printUsage() {
4148
public static void main(String... args) throws IOException {
4249

4350
try {
44-
45-
if (args.length != 2) {
51+
if (ArgumentsProcessor.containsHelpArgument(args)
52+
|| (2 != args.length
53+
&& 4 != args.length)) {
4654
printUsage();
4755
System.exit(-1);
4856
}
@@ -65,15 +73,26 @@ public static void main(String... args) throws IOException {
6573

6674
final List<Instruction> instructions = new Diffr(originalFile, newFile).diff();
6775

68-
for (final Instruction instruction : instructions) {
69-
System.out.println(InstructionComposer.composeString(instruction));
76+
final Optional<String> outputFile = ArgumentsProcessor.extractOutputFile(args);
77+
78+
if (4 == args.length
79+
&& outputFile.isPresent()) {
80+
81+
final File file = new File(outputFile.get());
82+
final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
83+
for (final Instruction instruction : instructions) {
84+
Instructions.writeInstruction(instruction, bufferedWriter);
85+
}
86+
bufferedWriter.close();
87+
} else {
88+
for (final Instruction instruction : instructions) {
89+
System.out.println(InstructionComposer.composeString(instruction));
90+
}
91+
System.out.flush();
7092
}
71-
72-
System.out.flush();
7393

7494
System.exit(0);
75-
}
76-
catch (final IOException io) {
95+
} catch (final IOException io) {
7796
System.err.println("There was a problem reading the files: " + io);
7897
}
7998
}

patch/src/main/java/diffr/patch/Main.java

+3
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public static void main(final String... args) {
7474
final List<String> newFileStrings = patchr.patch();
7575

7676
final Optional<String> outputFile = ArgumentsProcessor.extractOutputFile(args);
77+
7778
if (4 == args.length
7879
&& outputFile.isPresent()) {
80+
7981
final File file = new File(outputFile.get());
8082
final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
8183
for (final String line : newFileStrings) {
@@ -87,6 +89,7 @@ public static void main(final String... args) {
8789
for (final String line : newFileStrings) {
8890
System.out.println(line);
8991
}
92+
System.out.flush();
9093
}
9194

9295
} catch (final IOException io) {

0 commit comments

Comments
 (0)