1
1
package diffr .diff ;
2
2
3
+ import com .google .common .base .Optional ;
3
4
import com .google .common .io .Files ;
5
+ import diffr .util .ArgumentsProcessor ;
4
6
import diffr .util .instruction .Instruction ;
5
7
import diffr .util .instruction .InstructionComposer ;
8
+ import diffr .util .instruction .Instructions ;
6
9
10
+ import java .io .BufferedWriter ;
7
11
import java .io .File ;
12
+ import java .io .FileWriter ;
8
13
import java .io .IOException ;
9
14
import java .nio .charset .Charset ;
10
15
import java .util .List ;
11
16
12
17
/**
13
18
* Main entry point to diffr's DIFF tool.
14
- *
19
+ * <p/>
15
20
* <p>
16
21
* Expects two arguments:
17
22
* <ul>
@@ -30,7 +35,9 @@ public final class Main {
30
35
* Prints the usage of this tool.
31
36
*/
32
37
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>" );
34
41
}
35
42
36
43
/**
@@ -41,8 +48,9 @@ private static void printUsage() {
41
48
public static void main (String ... args ) throws IOException {
42
49
43
50
try {
44
-
45
- if (args .length != 2 ) {
51
+ if (ArgumentsProcessor .containsHelpArgument (args )
52
+ || (2 != args .length
53
+ && 4 != args .length )) {
46
54
printUsage ();
47
55
System .exit (-1 );
48
56
}
@@ -65,15 +73,26 @@ public static void main(String... args) throws IOException {
65
73
66
74
final List <Instruction > instructions = new Diffr (originalFile , newFile ).diff ();
67
75
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 ();
70
92
}
71
-
72
- System .out .flush ();
73
93
74
94
System .exit (0 );
75
- }
76
- catch (final IOException io ) {
95
+ } catch (final IOException io ) {
77
96
System .err .println ("There was a problem reading the files: " + io );
78
97
}
79
98
}
0 commit comments