/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.cli.bug; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.Iterator; import java.util.Properties; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.MissingArgumentException; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.OptionGroup; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.Parser; import org.apache.commons.cli.PosixParser; import org.junit.Test; @SuppressWarnings("deprecation") // tests some deprecated classes public class BugsTest { @Test public void test11456() throws Exception { // Posix Options options = new Options(); options.addOption(OptionBuilder.hasOptionalArg().create('a')); options.addOption(OptionBuilder.hasArg().create('b')); String[] args = {"-a", "-bvalue"}; CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); assertEquals(cmd.getOptionValue('b'), "value"); // GNU options = new Options(); options.addOption(OptionBuilder.hasOptionalArg().create('a')); options.addOption(OptionBuilder.hasArg().create('b')); args = new String[] {"-a", "-b", "value"}; parser = new GnuParser(); cmd = parser.parse(options, args); assertEquals(cmd.getOptionValue('b'), "value"); } @Test public void test11457() throws Exception { final Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("verbose").create()); final String[] args = {"--verbose"}; final CommandLineParser parser = new PosixParser(); final CommandLine cmd = parser.parse(options, args); assertTrue(cmd.hasOption("verbose")); } @Test public void test11458() throws Exception { final Options options = new Options(); options.addOption(OptionBuilder.withValueSeparator('=').hasArgs().create('D')); options.addOption(OptionBuilder.withValueSeparator(':').hasArgs().create('p')); final String[] args = {"-DJAVA_HOME=/opt/java", "-pfile1:file2:file3"}; final CommandLineParser parser = new PosixParser(); final 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"); final Iterator