001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.cli; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022 023import org.junit.Before; 024import org.junit.Test; 025 026@SuppressWarnings("deprecation") // tests some deprecated classes 027public class ArgumentIsOptionTest 028{ 029 private Options options = null; 030 private CommandLineParser parser = null; 031 032 @Before 033 public void setUp() 034 { 035 options = new Options().addOption("p", false, "Option p").addOption("attr", true, "Option accepts argument"); 036 037 parser = new PosixParser(); 038 } 039 040 @Test 041 public void testOptionAndOptionWithArgument() throws Exception 042 { 043 String[] args = new String[]{ 044 "-p", 045 "-attr", 046 "p" 047 }; 048 049 CommandLine cl = parser.parse(options, args); 050 assertTrue("Confirm -p is set", cl.hasOption("p")); 051 assertTrue("Confirm -attr is set", cl.hasOption("attr")); 052 assertTrue("Confirm arg of -attr", cl.getOptionValue("attr").equals("p")); 053 assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0); 054 } 055 056 @Test 057 public void testOptionWithArgument() throws Exception 058 { 059 String[] args = new String[]{ 060 "-attr", 061 "p" 062 }; 063 064 CommandLine cl = parser.parse(options, args); 065 assertFalse("Confirm -p is set", cl.hasOption("p")); 066 assertTrue("Confirm -attr is set", cl.hasOption("attr")); 067 assertTrue("Confirm arg of -attr", 068 cl.getOptionValue("attr").equals("p")); 069 assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0); 070 } 071 072 @Test 073 public void testOption() throws Exception 074 { 075 String[] args = new String[]{ 076 "-p" 077 }; 078 079 CommandLine cl = parser.parse(options, args); 080 assertTrue("Confirm -p is set", cl.hasOption("p")); 081 assertFalse("Confirm -attr is not set", cl.hasOption("attr")); 082 assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0); 083 } 084}