| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package org.apache.commons.cli.bug; |
| 19 |
|
| 20 |
import static org.junit.Assert.assertEquals; |
| 21 |
import static org.junit.Assert.assertFalse; |
| 22 |
import static org.junit.Assert.assertNotNull; |
| 23 |
import static org.junit.Assert.assertNull; |
| 24 |
import static org.junit.Assert.assertTrue; |
| 25 |
import static org.junit.Assert.fail; |
| 26 |
|
| 27 |
import java.io.ByteArrayOutputStream; |
| 28 |
import java.io.PrintStream; |
| 29 |
import java.util.Iterator; |
| 30 |
import java.util.Properties; |
| 31 |
|
| 32 |
import org.apache.commons.cli.CommandLine; |
| 33 |
import org.apache.commons.cli.CommandLineParser; |
| 34 |
import org.apache.commons.cli.GnuParser; |
| 35 |
import org.apache.commons.cli.HelpFormatter; |
| 36 |
import org.apache.commons.cli.MissingArgumentException; |
| 37 |
import org.apache.commons.cli.Option; |
| 38 |
import org.apache.commons.cli.OptionBuilder; |
| 39 |
import org.apache.commons.cli.OptionGroup; |
| 40 |
import org.apache.commons.cli.Options; |
| 41 |
import org.apache.commons.cli.ParseException; |
| 42 |
import org.apache.commons.cli.Parser; |
| 43 |
import org.apache.commons.cli.PosixParser; |
| 44 |
import org.junit.Test; |
| 45 |
|
| 46 |
@SuppressWarnings("deprecation") // tests some deprecated classes |
| 47 |
public class BugsTest { |
| 48 |
@Test |
| 49 |
public void test11456() throws Exception { |
| 50 |
// Posix |
| 51 |
Options options = new Options(); |
| 52 |
options.addOption(OptionBuilder.hasOptionalArg().create('a')); |
| 53 |
options.addOption(OptionBuilder.hasArg().create('b')); |
| 54 |
String[] args = {"-a", "-bvalue"}; |
| 55 |
|
| 56 |
CommandLineParser parser = new PosixParser(); |
| 57 |
|
| 58 |
CommandLine cmd = parser.parse(options, args); |
| 59 |
assertEquals(cmd.getOptionValue('b'), "value"); |
| 60 |
|
| 61 |
// GNU |
| 62 |
options = new Options(); |
| 63 |
options.addOption(OptionBuilder.hasOptionalArg().create('a')); |
| 64 |
options.addOption(OptionBuilder.hasArg().create('b')); |
| 65 |
args = new String[] {"-a", "-b", "value"}; |
| 66 |
|
| 67 |
parser = new GnuParser(); |
| 68 |
|
| 69 |
cmd = parser.parse(options, args); |
| 70 |
assertEquals(cmd.getOptionValue('b'), "value"); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void test11457() throws Exception { |
| 75 |
final Options options = new Options(); |
| 76 |
options.addOption(OptionBuilder.withLongOpt("verbose").create()); |
| 77 |
final String[] args = {"--verbose"}; |
| 78 |
|
| 79 |
final CommandLineParser parser = new PosixParser(); |
| 80 |
|
| 81 |
final CommandLine cmd = parser.parse(options, args); |
| 82 |
assertTrue(cmd.hasOption("verbose")); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void test11458() throws Exception { |
| 87 |
final Options options = new Options(); |
| 88 |
options.addOption(OptionBuilder.withValueSeparator('=').hasArgs().create('D')); |
| 89 |
options.addOption(OptionBuilder.withValueSeparator(':').hasArgs().create('p')); |
| 90 |
final String[] args = {"-DJAVA_HOME=/opt/java", "-pfile1:file2:file3"}; |
| 91 |
|
| 92 |
final CommandLineParser parser = new PosixParser(); |
| 93 |
|
| 94 |
final CommandLine cmd = parser.parse(options, args); |
| 95 |
|
| 96 |
String[] values = cmd.getOptionValues('D'); |
| 97 |
|
| 98 |
assertEquals(values[0], "JAVA_HOME"); |
| 99 |
assertEquals(values[1], "/opt/java"); |
| 100 |
|
| 101 |
values = cmd.getOptionValues('p'); |
| 102 |
|
| 103 |
assertEquals(values[0], "file1"); |
| 104 |
assertEquals(values[1], "file2"); |
| 105 |
assertEquals(values[2], "file3"); |
| 106 |
|
| 107 |
final Iterator<Option> iter = cmd.iterator(); |
| 108 |
while (iter.hasNext()) { |
| 109 |
final Option opt = iter.next(); |
| 110 |
switch (opt.getId()) { |
| 111 |
case 'D': |
| 112 |
assertEquals(opt.getValue(0), "JAVA_HOME"); |
| 113 |
assertEquals(opt.getValue(1), "/opt/java"); |
| 114 |
break; |
| 115 |
case 'p': |
| 116 |
assertEquals(opt.getValue(0), "file1"); |
| 117 |
assertEquals(opt.getValue(1), "file2"); |
| 118 |
assertEquals(opt.getValue(2), "file3"); |
| 119 |
break; |
| 120 |
default: |
| 121 |
fail("-D option not found"); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void test11680() throws Exception { |
| 128 |
final Options options = new Options(); |
| 129 |
options.addOption("f", true, "foobar"); |
| 130 |
options.addOption("m", true, "missing"); |
| 131 |
final String[] args = {"-f", "foo"}; |
| 132 |
|
| 133 |
final CommandLineParser parser = new PosixParser(); |
| 134 |
|
| 135 |
final CommandLine cmd = parser.parse(options, args); |
| 136 |
|
| 137 |
cmd.getOptionValue("f", "default f"); |
| 138 |
cmd.getOptionValue("m", "default m"); |
| 139 |
// |
| 140 |
assertNull(cmd.getOptionValue((String) null, null)); |
| 141 |
assertEquals("default", cmd.getOptionValue((String) null, "default")); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void test12210() throws Exception { |
| 146 |
// create the main options object which will handle the first parameter |
| 147 |
final Options mainOptions = new Options(); |
| 148 |
// There can be 2 main exclusive options: -exec|-rep |
| 149 |
|
| 150 |
// Therefore, place them in an option group |
| 151 |
|
| 152 |
String[] argv = {"-exec", "-exec_opt1", "-exec_opt2"}; |
| 153 |
final OptionGroup grp = new OptionGroup(); |
| 154 |
|
| 155 |
grp.addOption(new Option("exec", false, "description for this option")); |
| 156 |
|
| 157 |
grp.addOption(new Option("rep", false, "description for this option")); |
| 158 |
|
| 159 |
mainOptions.addOptionGroup(grp); |
| 160 |
|
| 161 |
// for the exec option, there are 2 options... |
| 162 |
final Options execOptions = new Options(); |
| 163 |
execOptions.addOption("exec_opt1", false, " desc"); |
| 164 |
execOptions.addOption("exec_opt2", false, " desc"); |
| 165 |
|
| 166 |
// similarly, for rep there are 2 options... |
| 167 |
final Options repOptions = new Options(); |
| 168 |
repOptions.addOption("repopto", false, "desc"); |
| 169 |
repOptions.addOption("repoptt", false, "desc"); |
| 170 |
|
| 171 |
// create the parser |
| 172 |
final GnuParser parser = new GnuParser(); |
| 173 |
|
| 174 |
// finally, parse the arguments: |
| 175 |
|
| 176 |
// first parse the main options to see what the user has specified |
| 177 |
// We set stopAtNonOption to true so it does not touch the remaining |
| 178 |
// options |
| 179 |
CommandLine cmd = parser.parse(mainOptions, argv, true); |
| 180 |
// get the remaining options... |
| 181 |
argv = cmd.getArgs(); |
| 182 |
|
| 183 |
if (cmd.hasOption("exec")) { |
| 184 |
cmd = parser.parse(execOptions, argv, false); |
| 185 |
// process the exec_op1 and exec_opt2... |
| 186 |
assertTrue(cmd.hasOption("exec_opt1")); |
| 187 |
assertTrue(cmd.hasOption("exec_opt2")); |
| 188 |
} else if (cmd.hasOption("rep")) { |
| 189 |
cmd = parser.parse(repOptions, argv, false); |
| 190 |
// process the rep_op1 and rep_opt2... |
| 191 |
} else { |
| 192 |
fail("exec option not found"); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void test13425() throws Exception { |
| 198 |
final Options options = new Options(); |
| 199 |
//@formatter:off |
| 200 |
final Option oldpass = OptionBuilder.withLongOpt("old-password") |
| 201 |
.withDescription("Use this option to specify the old password") |
| 202 |
.hasArg() |
| 203 |
.create('o'); |
| 204 |
final Option newpass = OptionBuilder.withLongOpt("new-password") |
| 205 |
.withDescription("Use this option to specify the new password") |
| 206 |
.hasArg() |
| 207 |
.create('n'); |
| 208 |
//@formatter:on |
| 209 |
|
| 210 |
final String[] args = {"-o", "-n", "newpassword"}; |
| 211 |
|
| 212 |
options.addOption(oldpass); |
| 213 |
options.addOption(newpass); |
| 214 |
|
| 215 |
final Parser parser = new PosixParser(); |
| 216 |
|
| 217 |
try { |
| 218 |
parser.parse(options, args); |
| 219 |
fail("MissingArgumentException not caught."); |
| 220 |
} catch (final MissingArgumentException expected) { |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void test13666() throws Exception { |
| 226 |
final Options options = new Options(); |
| 227 |
final Option dir = OptionBuilder.withDescription("dir").hasArg().create('d'); |
| 228 |
options.addOption(dir); |
| 229 |
|
| 230 |
final PrintStream oldSystemOut = System.out; |
| 231 |
try { |
| 232 |
final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 233 |
final PrintStream print = new PrintStream(bytes); |
| 234 |
|
| 235 |
// capture this platform's eol symbol |
| 236 |
print.println(); |
| 237 |
final String eol = bytes.toString(); |
| 238 |
bytes.reset(); |
| 239 |
|
| 240 |
System.setOut(new PrintStream(bytes)); |
| 241 |
|
| 242 |
final HelpFormatter formatter = new HelpFormatter(); |
| 243 |
formatter.printHelp("dir", options); |
| 244 |
|
| 245 |
assertEquals("usage: dir" + eol + " -d <arg> dir" + eol, bytes.toString()); |
| 246 |
} finally { |
| 247 |
System.setOut(oldSystemOut); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
@Test |
| 252 |
public void test13935() throws Exception { |
| 253 |
final OptionGroup directions = new OptionGroup(); |
| 254 |
|
| 255 |
final Option left = new Option("l", "left", false, "go left"); |
| 256 |
final Option right = new Option("r", "right", false, "go right"); |
| 257 |
final Option straight = new Option("s", "straight", false, "go straight"); |
| 258 |
final Option forward = new Option("f", "forward", false, "go forward"); |
| 259 |
forward.setRequired(true); |
| 260 |
|
| 261 |
directions.addOption(left); |
| 262 |
directions.addOption(right); |
| 263 |
directions.setRequired(true); |
| 264 |
|
| 265 |
final Options opts = new Options(); |
| 266 |
opts.addOptionGroup(directions); |
| 267 |
opts.addOption(straight); |
| 268 |
|
| 269 |
final CommandLineParser parser = new PosixParser(); |
| 270 |
|
| 271 |
String[] args = {}; |
| 272 |
try { |
| 273 |
parser.parse(opts, args); |
| 274 |
fail("Expected ParseException"); |
| 275 |
} catch (final ParseException expected) { |
| 276 |
} |
| 277 |
|
| 278 |
args = new String[] {"-s"}; |
| 279 |
try { |
| 280 |
parser.parse(opts, args); |
| 281 |
fail("Expected ParseException"); |
| 282 |
} catch (final ParseException expected) { |
| 283 |
} |
| 284 |
|
| 285 |
args = new String[] {"-s", "-l"}; |
| 286 |
CommandLine line = parser.parse(opts, args); |
| 287 |
assertNotNull(line); |
| 288 |
|
| 289 |
opts.addOption(forward); |
| 290 |
args = new String[] {"-s", "-l", "-f"}; |
| 291 |
line = parser.parse(opts, args); |
| 292 |
assertNotNull(line); |
| 293 |
} |
| 294 |
|
| 295 |
@Test |
| 296 |
public void test14786() throws Exception { |
| 297 |
final Option o = OptionBuilder.isRequired().withDescription("test").create("test"); |
| 298 |
final Options opts = new Options(); |
| 299 |
opts.addOption(o); |
| 300 |
opts.addOption(o); |
| 301 |
|
| 302 |
final CommandLineParser parser = new GnuParser(); |
| 303 |
|
| 304 |
final String[] args = {"-test"}; |
| 305 |
|
| 306 |
final CommandLine line = parser.parse(opts, args); |
| 307 |
assertTrue(line.hasOption("test")); |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void test15046() throws Exception { |
| 312 |
final CommandLineParser parser = new PosixParser(); |
| 313 |
final String[] cliArgs = {"-z", "c"}; |
| 314 |
|
| 315 |
final Options options = new Options(); |
| 316 |
options.addOption(new Option("z", "timezone", true, "affected option")); |
| 317 |
|
| 318 |
parser.parse(options, cliArgs); |
| 319 |
|
| 320 |
// now add conflicting option |
| 321 |
options.addOption("c", "conflict", true, "conflict option"); |
| 322 |
final CommandLine line = parser.parse(options, cliArgs); |
| 323 |
assertEquals(line.getOptionValue('z'), "c"); |
| 324 |
assertFalse(line.hasOption("c")); |
| 325 |
} |
| 326 |
|
| 327 |
@Test |
| 328 |
public void test15648() throws Exception { |
| 329 |
final CommandLineParser parser = new PosixParser(); |
| 330 |
final String[] args = {"-m", "\"Two Words\""}; |
| 331 |
final Option m = OptionBuilder.hasArgs().create("m"); |
| 332 |
final Options options = new Options(); |
| 333 |
options.addOption(m); |
| 334 |
final CommandLine line = parser.parse(options, args); |
| 335 |
assertEquals("Two Words", line.getOptionValue("m")); |
| 336 |
} |
| 337 |
|
| 338 |
@Test |
| 339 |
public void test31148() throws ParseException { |
| 340 |
final Option multiArgOption = new Option("o", "option with multiple args"); |
| 341 |
multiArgOption.setArgs(1); |
| 342 |
|
| 343 |
final Options options = new Options(); |
| 344 |
options.addOption(multiArgOption); |
| 345 |
|
| 346 |
final Parser parser = new PosixParser(); |
| 347 |
final String[] args = {}; |
| 348 |
final Properties props = new Properties(); |
| 349 |
props.setProperty("o", "ovalue"); |
| 350 |
final CommandLine cl = parser.parse(options, args, props); |
| 351 |
|
| 352 |
assertTrue(cl.hasOption('o')); |
| 353 |
assertEquals("ovalue", cl.getOptionValue('o')); |
| 354 |
} |
| 355 |
|
| 356 |
} |