001package org.w3.ldp.testsuite.util;
002
003import org.apache.commons.cli.CommandLine;
004import org.apache.commons.lang3.StringUtils;
005
006import java.util.Map;
007
008public class OptionsHandler {
009
010        private final Map<String, String> options;
011        private final CommandLine cmd;
012
013        public OptionsHandler(final Map<String, String> options) {
014                this.options = options;
015                this.cmd = null;
016        }
017
018        public OptionsHandler(final CommandLine cmd) {
019                this.cmd = cmd;
020                this.options = null;
021        }
022
023        public boolean hasOption(String name) {
024                if (options == null) {
025                        return cmd.hasOption(name);
026                }
027
028                return options.containsKey(name);
029        }
030
031        public boolean hasOptionWithValue(String name) {
032                if (options == null) {
033                        return cmd.hasOption(name) && StringUtils.isNotBlank(cmd.getOptionValue(name));
034                }
035
036                return options.containsKey(name) && StringUtils.isNotBlank(options.get(name));
037        }
038
039        public String getOptionValue(String name) {
040                if (options == null) {
041                        return cmd.getOptionValue(name);
042                }
043
044                return options.get(name);
045        }
046
047        public String[] getOptionValues(String name) {
048                if (options == null) {
049                        return cmd.getOptionValues(name);
050                }
051
052                String[] values = options.get(name).split(",");
053                for (int i = 0; i < values.length; i++) {
054                        values[i] = values[i].trim();
055                }
056
057                return values;
058        }
059
060}