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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
* $Id: BugsTest.java 3134 2007-03-02 07:20:08Z otmarhumbel $
*/
package org.apache.commons.cli;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class BugsTest extends TestCase
{
public static Test suite() {
return new TestSuite( BugsTest.class );
}
public BugsTest( String name )
{
super( name );
}
public void setUp()
{
}
public void tearDown()
{
}
public void test11457() {
Options options = new Options();
OptionBuilder.withLongOpt( "verbose" );
options.addOption( OptionBuilder.create() );
String[] args = new String[] { "--verbose" };
CommandLineParser parser = new PosixParser();
try {
CommandLine cmd = parser.parse( options, args );
assertTrue( cmd.hasOption( "verbose" ) );
}
catch( ParseException exp ) {
exp.printStackTrace();
fail( "Unexpected Exception: " + exp.getMessage() );
}
}
public void test11458()
{
Options options = new Options();
OptionBuilder.withValueSeparator( '=' );
OptionBuilder.hasArgs();
options.addOption(OptionBuilder.create( 'D' ) );
OptionBuilder.withValueSeparator( ':' );
OptionBuilder.hasArgs();
options.addOption( OptionBuilder.create( 'p' ) );
String[] args = new String[] { "-DJAVA_HOME=/opt/java" ,
"-pfile1:file2:file3" };
CommandLineParser parser = new PosixParser();
try {
CommandLine cmd = parser.parse( options, args );
String[] values = cmd.getOptionValues( 'D' );
assertEquals( values[0], "JAVA_HOME" );
assertEquals( values[1], "/opt/java" );
values = cmd.getOptionValues( 'p' );
assertEquals( values[0], "file1" );
assertEquals( values[1], "file2" );
assertEquals( values[2], "file3" );
java.util.Iterator iter = cmd.iterator();
while( iter.hasNext() ) {
Option opt = (Option)iter.next();
switch( opt.getId() ) {
case 'D':
assertEquals( opt.getValue( 0 ), "JAVA_HOME" );
assertEquals( opt.getValue( 1 ), "/opt/java" );
break;
case 'p':
assertEquals( opt.getValue( 0 ), "file1" );
assertEquals( opt.getValue( 1 ), "file2" );
assertEquals( opt.getValue( 2 ), "file3" );
break;
default:
fail( "-D option not found" );
}
}
}
catch( ParseException exp ) {
fail( "Unexpected Exception:\nMessage:" + exp.getMessage()
+ "Type: " + exp.getClass().getName() );
}
}
public void test11680()
{
Options options = new Options();
options.addOption("f", true, "foobar");
options.addOption("m", true, "missing");
String[] args = new String[] { "-f" , "foo" };
CommandLineParser parser = new PosixParser();
try {
CommandLine cmd = parser.parse( options, args );
try {
cmd.getOptionValue( "f", "default f");
cmd.getOptionValue( "m", "default m");
}
catch( NullPointerException exp ) {
fail( "NullPointer caught: " + exp.getMessage() );
}
}
catch( ParseException exp ) {
fail( "Unexpected Exception: " + exp.getMessage() );
}
}
public void test11456()
{
// Posix
Options options = new Options();
OptionBuilder.hasOptionalArg();
options.addOption( OptionBuilder.create( 'a' ) );
OptionBuilder.hasArg();
options.addOption( OptionBuilder.create( 'b' ) );
String[] args = new String[] { "-a", "-bvalue" };
CommandLineParser parser = new PosixParser();
try {
CommandLine cmd = parser.parse( options, args );
assertEquals( cmd.getOptionValue( 'b' ), "value" );
}
catch( ParseException exp ) {
fail( "Unexpected Exception: " + exp.getMessage() );
}
// GNU
options = new Options();
OptionBuilder.hasOptionalArg();
options.addOption( OptionBuilder.create( 'a' ) );
OptionBuilder.hasArg();
options.addOption( OptionBuilder.create( 'b' ) );
args = new String[] { "-a", "-b", "value" };
parser = new GnuParser();
try {
CommandLine cmd = parser.parse( options, args );
assertEquals( cmd.getOptionValue( 'b' ), "value" );
}
catch( ParseException exp ) {
fail( "Unexpected Exception: " + exp.getMessage() );
}
}
public void test12210() {
// create the main options object which will handle the first parameter
Options mainOptions = new Options();
// There can be 2 main exclusive options: -exec|-rep
// Therefore, place them in an option group
String[] argv = new String[] { "-exec", "-exec_opt1", "-exec_opt2" };
OptionGroup grp = new OptionGroup();
grp.addOption(new Option("exec",false,"description for this option"));
grp.addOption(new Option("rep",false,"description for this option"));
mainOptions.addOptionGroup(grp);
// for the exec option, there are 2 options...
Options execOptions = new Options();
execOptions.addOption("exec_opt1",false," desc");
execOptions.addOption("exec_opt2",false," desc");
// similarly, for rep there are 2 options...
Options repOptions = new Options();
repOptions.addOption("repopto",false,"desc");
repOptions.addOption("repoptt",false,"desc");
// create the parser
GnuParser parser = new GnuParser();
// finally, parse the arguments:
// first parse the main options to see what the user has specified
// We set stopAtNonOption to true so it does not touch the remaining
// options
try {
CommandLine cmd = parser.parse(mainOptions,argv,true);
// get the remaining options...
argv = cmd.getArgs();
if(cmd.hasOption("exec")){
cmd = parser.parse(execOptions,argv,false);
// process the exec_op1 and exec_opt2...
assertTrue( cmd.hasOption("exec_opt1") );
assertTrue( cmd.hasOption("exec_opt2") );
}
else if(cmd.hasOption("rep")){
cmd = parser.parse(repOptions,argv,false);
// process the rep_op1 and rep_opt2...
}
else {
fail( "exec option not found" );
}
}
catch( ParseException exp ) {
fail( "Unexpected exception: " + exp.getMessage() );
}
}
public void test13425() {
Options options = new Options();
OptionBuilder.withLongOpt( "old-password" );
OptionBuilder.withDescription( "Use this option to specify the old password" );
OptionBuilder.hasArg();
Option oldpass = OptionBuilder.create( 'o' );
OptionBuilder.withLongOpt( "new-password" );
OptionBuilder.withDescription( "Use this option to specify the new password" );
OptionBuilder.hasArg();
Option newpass = OptionBuilder.create( 'n' );
String[] args = {
"-o",
"-n",
"newpassword"
};
options.addOption( oldpass );
options.addOption( newpass );
Parser parser = new PosixParser();
CommandLine line = null;
try {
line = parser.parse( options, args );
}
// catch the exception and leave the method
catch( Exception exp ) {
assertTrue( exp != null );
return;
}
fail( "MissingArgumentException not caught." + line);
}
public void test13666() {
Options options = new Options();
OptionBuilder.withDescription( "dir" );
OptionBuilder.hasArg();
Option dir = OptionBuilder.create( 'd' );
options.addOption( dir );
try {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "dir", options );
}
catch( Exception exp ) {
fail( "Unexpected Exception: " + exp.getMessage() );
}
}
public void test13935() {
OptionGroup directions = new OptionGroup();
Option left = new Option( "l", "left", false, "go left" );
Option right = new Option( "r", "right", false, "go right" );
Option straight = new Option( "s", "straight", false, "go straight" );
Option forward = new Option( "f", "forward", false, "go forward" );
forward.setRequired( true );
directions.addOption( left );
directions.addOption( right );
directions.setRequired( true );
Options opts = new Options();
opts.addOptionGroup( directions );
opts.addOption( straight );
CommandLineParser parser = new PosixParser();
boolean exception = false;
CommandLine line = null;
String[] args = new String[] { };
try {
line = parser.parse( opts, args );
}
catch( ParseException exp ) {
exception = true;
}
if( !exception ) {
fail( "Expected exception not caught.");
}
exception = false;
args = new String[] { "-s" };
try {
line = parser.parse( opts, args );
}
catch( ParseException exp ) {
exception = true;
}
if( !exception ) {
fail( "Expected exception not caught.");
}
exception = false;
args = new String[] { "-s", "-l" };
try {
line = parser.parse( opts, args );
}
catch( ParseException exp ) {
fail( "Unexpected exception: " + exp.getMessage() );
}
opts.addOption( forward );
args = new String[] { "-s", "-l", "-f" };
try {
line = parser.parse( opts, args );
}
catch( ParseException exp ) {
fail( "Unexpected exception: " + exp.getMessage() );
}
if(line != null) {
line = null;
}
}
}
|