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 package org.apache.commons.cli; 18 19 import junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 23 24 public class ArgumentIsOptionTest extends TestCase { 25 private Options options = null; 26 private CommandLineParser parser = null; 27 28 public ArgumentIsOptionTest(String name) { 29 super(name); 30 } 31 32 public static Test suite() { 33 return new TestSuite(ArgumentIsOptionTest.class); 34 } 35 36 public void setUp() { 37 options = new Options().addOption("p", false, "Option p").addOption("attr", 38 true, "Option accepts argument"); 39 40 parser = new PosixParser(); 41 } 42 43 public void tearDown() { 44 } 45 46 public void testOptionAndOptionWithArgument() { 47 String[] args = new String[] { 48 "-p", 49 "-attr", 50 "p" 51 }; 52 53 try { 54 CommandLine cl = parser.parse(options, args); 55 assertTrue("Confirm -p is set", cl.hasOption("p")); 56 assertTrue("Confirm -attr is set", cl.hasOption("attr")); 57 assertTrue("Confirm arg of -attr", 58 cl.getOptionValue("attr").equals("p")); 59 assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0); 60 } 61 catch (ParseException e) { 62 fail(e.toString()); 63 } 64 } 65 66 public void testOptionWithArgument() { 67 String[] args = new String[] { 68 "-attr", 69 "p" 70 }; 71 72 try { 73 CommandLine cl = parser.parse(options, args); 74 assertFalse("Confirm -p is set", cl.hasOption("p")); 75 assertTrue("Confirm -attr is set", cl.hasOption("attr")); 76 assertTrue("Confirm arg of -attr", 77 cl.getOptionValue("attr").equals("p")); 78 assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0); 79 } 80 catch (ParseException e) { 81 fail(e.toString()); 82 } 83 } 84 85 public void testOption() { 86 String[] args = new String[] { 87 "-p" 88 }; 89 90 try { 91 CommandLine cl = parser.parse(options, args); 92 assertTrue("Confirm -p is set", cl.hasOption("p")); 93 assertFalse("Confirm -attr is not set", cl.hasOption("attr")); 94 assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0); 95 } 96 catch (ParseException e) { 97 fail(e.toString()); 98 } 99 } 100 }