|
| 1 | +/* |
| 2 | + * Copyright @ 2015 - present, 8x8 Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.jitsi.jigasi.cmd; |
| 17 | + |
| 18 | + |
| 19 | +import org.jitsi.utils.logging.*; |
| 20 | + |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +/** |
| 24 | + * Utility class for parsing command line arguments that take some value. |
| 25 | + * Arguments can have one of the following formats: |
| 26 | + * <ul> |
| 27 | + * <li>"arg=value"</li> |
| 28 | + * <li>"-arg=value"</li> |
| 29 | + * <li>"--arg=value"</li> |
| 30 | + * </ul> |
| 31 | + * It's also possible to specify required arguments. If any of required |
| 32 | + * arguments is not found {@link ParseException} will be thrown by |
| 33 | + * {@link #parse(String[])}. |
| 34 | + * |
| 35 | + * @author Pawel Domas |
| 36 | + */ |
| 37 | +public class CmdLine |
| 38 | +{ |
| 39 | + /** |
| 40 | + * The logger |
| 41 | + */ |
| 42 | + private final static Logger logger = Logger.getLogger(CmdLine.class); |
| 43 | + |
| 44 | + /** |
| 45 | + * Map of argument values. |
| 46 | + */ |
| 47 | + private Map<String, String> argMap = new HashMap<String, String>(); |
| 48 | + |
| 49 | + /** |
| 50 | + * The list of required arguments. |
| 51 | + */ |
| 52 | + private List<String> requiredArgs = new ArrayList<String>(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Adds argument name to the list of required arguments. |
| 56 | + * @param reqArg "arg", "-arg" or "--arg" argument name to be added. |
| 57 | + */ |
| 58 | + public void addRequiredArgument(String reqArg) |
| 59 | + { |
| 60 | + reqArg = cleanHyphens(reqArg); |
| 61 | + |
| 62 | + if (!requiredArgs.contains(reqArg)) |
| 63 | + requiredArgs.add(reqArg); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Removes given argument name from the list of required arguments. |
| 68 | + * @param reqArg "arg", "-arg" or "--arg" argument name. |
| 69 | + */ |
| 70 | + public void removeRequiredArgument(String reqArg) |
| 71 | + { |
| 72 | + reqArg = cleanHyphens(reqArg); |
| 73 | + |
| 74 | + requiredArgs.remove(reqArg); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the list of required arguments. Names are stripped from hyphens. |
| 79 | + */ |
| 80 | + public List<String> getRequiredArguments() |
| 81 | + { |
| 82 | + return Collections.unmodifiableList(requiredArgs); |
| 83 | + } |
| 84 | + |
| 85 | + private String cleanHyphens(String arg) |
| 86 | + { |
| 87 | + if (arg.startsWith("--")) |
| 88 | + return arg.substring(2); |
| 89 | + else if (arg.startsWith("-")) |
| 90 | + return arg.substring(1); |
| 91 | + else |
| 92 | + return arg; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Parses the array of command line arguments. |
| 97 | + * |
| 98 | + * @param args String array which should come from the "main" method. |
| 99 | + * |
| 100 | + * @throws ParseException if any of required arguments has not been found |
| 101 | + * in <tt>args</tt>. |
| 102 | + */ |
| 103 | + public void parse(String[] args) throws ParseException |
| 104 | + { |
| 105 | + for (String arg : args) |
| 106 | + { |
| 107 | + arg = cleanHyphens(arg); |
| 108 | + |
| 109 | + int eqIdx = arg.indexOf("="); |
| 110 | + if (eqIdx <= 0) |
| 111 | + { |
| 112 | + logger.warn("Skipped invalid cmd line argument: " + arg); |
| 113 | + continue; |
| 114 | + } |
| 115 | + else if (eqIdx == arg.length() - 1) |
| 116 | + { |
| 117 | + logger.warn("Skipped empty cmd line argument: " + arg); |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + String key = arg.substring(0, eqIdx); |
| 122 | + String val = arg.substring(eqIdx+1); |
| 123 | + argMap.put(key, val); |
| 124 | + } |
| 125 | + |
| 126 | + List<String> leftReqArgs = new ArrayList<String>(requiredArgs); |
| 127 | + leftReqArgs.removeAll(argMap.keySet()); |
| 128 | + if (!leftReqArgs.isEmpty()) |
| 129 | + { |
| 130 | + throw new ParseException( |
| 131 | + "Some of required arguments were not specified: " |
| 132 | + + leftReqArgs.toString()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the value of cmd line argument for given name. <tt>null</tt> |
| 138 | + * if there was no value or it was empty. |
| 139 | + * @param opt the name of command line argument which value we want to get. |
| 140 | + */ |
| 141 | + public String getOptionValue(String opt) |
| 142 | + { |
| 143 | + return argMap.get(cleanHyphens(opt)); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Returns the value of cmd line argument for given name. |
| 148 | + * <tt>defaultValue</tt> if there was no value or it was empty. |
| 149 | + * @param opt the name of command line argument which value we want to get. |
| 150 | + * @param defaultValue the default value which should be returned if the |
| 151 | + * argument value is missing. |
| 152 | + */ |
| 153 | + public String getOptionValue(String opt, String defaultValue) |
| 154 | + { |
| 155 | + String val = getOptionValue(opt); |
| 156 | + return val != null ? val : defaultValue; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns <tt>int</tt> value of cmd line argument for given name. |
| 161 | + * <tt>defaultValue</tt> if there was no valid value for that argument. |
| 162 | + * @param opt the name of command line argument which value we want to get. |
| 163 | + * @param defaultValue the default value which should be returned if the |
| 164 | + * argument value is missing. |
| 165 | + */ |
| 166 | + public int getIntOptionValue(String opt, int defaultValue) |
| 167 | + { |
| 168 | + String val = getOptionValue(opt); |
| 169 | + if (val == null) |
| 170 | + return defaultValue; |
| 171 | + try |
| 172 | + { |
| 173 | + return Integer.parseInt(val); |
| 174 | + } |
| 175 | + catch (NumberFormatException fmt) |
| 176 | + { |
| 177 | + return defaultValue; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments