aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/com/jantuomi/tunkki/core/CommandLineArgumentContainer.java
blob: 458fe5410c6b9b1c5ed8c1f469fba14e01e4a806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.jantuomi.tunkki.core;


import org.kohsuke.args4j.Option;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by jan on 10.6.2016.
 */

public class CommandLineArgumentContainer {

    private CommandLineArgumentContainer() {}

    private static final CommandLineArgumentContainer instance = new CommandLineArgumentContainer();
    private File srcFile;


    @Option(name="-f", usage="Execute script in file FILE.")
    public void setFile(File file) {
        this.srcFile = file;
    }

    @Option(name="-i", usage="Run in interactive mode.")
    public boolean interactiveModeActive = false;

    @Option(name="-d", usage="Display AST output (for debugging purposes).")
    public boolean astModeActive = false;

    public static CommandLineArgumentContainer getInstance() {
        return instance;
    }

    public boolean isInteractive() {
        return interactiveModeActive;
    }

    public boolean isAstModeActive() {
        return astModeActive;
    }

    public String getSourceFileContents() {
        if (srcFile == null) {
            return null;
        }

        return readFileContents(srcFile);
    }

    public String readFileContents(String filename) {
        return readFileContents(new File(filename));
    }

    public String readFileContents(File file) {
        BufferedReader br;
        String contents = null;
        try {
            br = new BufferedReader(new FileReader(file));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            contents = sb.toString();
        } catch (IOException e) {
            // System.err.println(String.format("Couldn't read file \"%s\".", file.toString()));
            return null;
        }

        // Add a newline at the end for comment rows to terminate nicely
        return contents + "\n";
    }
}