summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
blob: e0927fab926eeaed13e7d9764a8d3c92975106c1 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cz.crcs.ectester.common.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.function.BiFunction;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class TreeCommandLine extends CommandLine {
    private String name = "";
    private TreeCommandLine next;
    private CommandLine cli;

    public TreeCommandLine(CommandLine cli, TreeCommandLine next) {
        this.cli = cli;
        this.next = next;
    }

    public TreeCommandLine(String name, CommandLine cli, TreeCommandLine next) {
        this(cli, next);
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getNextName() {
        if (next != null) {
            return next.getName();
        }
        return null;
    }

    public TreeCommandLine getNext() {
        return next;
    }

    public boolean isNext(String next) {
        return Objects.equals(getNextName(), next);
    }

    public CommandLine getThis() {
        return cli;
    }

    private <T> T getOption(String opt, BiFunction<CommandLine, String, T> getter, T defaultValue) {
        if (opt.contains(".")) {
            String[] parts = opt.split(".", 2);
            if (next != null && parts[0].equals(next.getName())) {
                return getter.apply(next, parts[1]);
            }
            return defaultValue;
        }
        return getter.apply(cli, opt);
    }

    @Override
    public boolean hasOption(String opt) {
        return getOption(opt, CommandLine::hasOption, false);
    }

    @Override
    public boolean hasOption(char opt) {
        return cli.hasOption(opt);
    }

    @Override
    public Object getParsedOptionValue(String opt) throws ParseException {
        if (opt.contains(".")) {
            String[] parts = opt.split(".", 2);
            if (next != null && parts[0].equals(next.getName())) {
                return next.getParsedOptionValue(parts[1]);
            }
            return null;
        }
        return cli.getParsedOptionValue(opt);
    }

    @Override
    public Object getOptionObject(char opt) {
        return cli.getOptionObject(opt);
    }

    @Override
    public String getOptionValue(String opt) {
        return getOption(opt, CommandLine::getOptionValue, null);
    }

    @Override
    public String getOptionValue(char opt) {
        return cli.getOptionValue(opt);
    }

    @Override
    public String[] getOptionValues(String opt) {
        return getOption(opt, CommandLine::getOptionValues, null);
    }

    @Override
    public String[] getOptionValues(char opt) {
        return cli.getOptionValues(opt);
    }

    @Override
    public String getOptionValue(String opt, String defaultValue) {
        return getOption(opt, CommandLine::getOptionValue, defaultValue);
    }

    @Override
    public String getOptionValue(char opt, String defaultValue) {
        return cli.getOptionValue(opt, defaultValue);
    }

    @Override
    public Properties getOptionProperties(String opt) {
        return getOption(opt, CommandLine::getOptionProperties, new Properties());
    }

    @Override
    public Iterator<Option> iterator() {
        return cli.iterator();
    }

    @Override
    public Option[] getOptions() {
        return cli.getOptions();
    }

    public String getArg(int index) {
        if (index < 0 || index >= cli.getArgs().length) {
            return null;
        }
        return cli.getArgs()[index];
    }

    @Override
    public String[] getArgs() {
        return cli.getArgs();
    }

    @Override
    public List<String> getArgList() {
        return cli.getArgList();
    }
}