aboutsummaryrefslogtreecommitdiffstats
path: root/main/core/CommandLineArgumentContainer.java
blob: 2bcb160eefb9664994a2b59d1f93cd8f3a13f0e2 (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
package com.jantuomi.interpreter.main.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 verbose debug output.")
    public boolean debugModeActive = false;

    public static CommandLineArgumentContainer getInstance() {
        return instance;
    }

    public boolean isInteractive() {
        return interactiveModeActive;
    }

    public boolean isDebugModeActive() {
        return debugModeActive;
    }

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

        BufferedReader br;
        String contents = null;
        try {
            br = new BufferedReader(new FileReader(srcFile));
            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) {
            e.printStackTrace();
            return null;
        } finally {
            // Add a newline at the end for comment rows to terminate nicely
            return contents + "\n";
        }
    }
}