<commit_before>/* 
 * 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.avalon;

// Renamed from org.apache.avalon.excalibur.cli

import java.util.List;

import junit.framework.TestCase;

/**
 * 
 */
public final class ClutilTestCase extends TestCase {
	private static final String[] ARGLIST1 = new String[] { "--you", "are", "--all", "-cler", "kid" };

	private static final String[] ARGLIST2 = new String[] { "-Dstupid=idiot", "are", "--all", "here", "-d" };

	private static final String[] ARGLIST3 = new String[] {
	// duplicates
			"-Dstupid=idiot", "are", "--all", "--all", "here" };

	private static final String[] ARGLIST4 = new String[] {
	// incompatable (blee/all)
			"-Dstupid", "idiot", "are", "--all", "--blee", "here" };

	private static final String[] ARGLIST5 = new String[] { "-f", "myfile.txt" };

	private static final int DEFINE_OPT = 'D';

	private static final int CASE_CHECK_OPT = 'd';

	private static final int YOU_OPT = 'y';

	private static final int ALL_OPT = 'a';

	private static final int CLEAR1_OPT = 'c';

	private static final int CLEAR2_OPT = 'l';

	private static final int CLEAR3_OPT = 'e';

	private static final int CLEAR5_OPT = 'r';

	private static final int BLEE_OPT = 'b';

	private static final int FILE_OPT = 'f';

	private static final int TAINT_OPT = 'T';

	private static final CLOptionDescriptor DEFINE = new CLOptionDescriptor("define",
			CLOptionDescriptor.ARGUMENTS_REQUIRED_2, DEFINE_OPT, "define");

	private static final CLOptionDescriptor DEFINE_MANY = new CLOptionDescriptor("define",
			CLOptionDescriptor.ARGUMENTS_REQUIRED_2 | CLOptionDescriptor.DUPLICATES_ALLOWED, DEFINE_OPT, "define");

	private static final CLOptionDescriptor CASE_CHECK = new CLOptionDescriptor("charCheck",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CASE_CHECK_OPT, "check character case sensitivity");

	private static final CLOptionDescriptor YOU = new CLOptionDescriptor("you", CLOptionDescriptor.ARGUMENT_DISALLOWED,
			YOU_OPT, "you");

	private static final CLOptionDescriptor CLEAR1 = new CLOptionDescriptor("c",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR1_OPT, "c");

	private static final CLOptionDescriptor CLEAR2 = new CLOptionDescriptor("l",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR2_OPT, "l");

	private static final CLOptionDescriptor CLEAR3 = new CLOptionDescriptor("e",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR3_OPT, "e");

	private static final CLOptionDescriptor CLEAR5 = new CLOptionDescriptor("r",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR5_OPT, "r");

	private static final CLOptionDescriptor BLEE = new CLOptionDescriptor("blee",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, BLEE_OPT, "blee");

	private static final CLOptionDescriptor ALL = new CLOptionDescriptor("all", CLOptionDescriptor.ARGUMENT_DISALLOWED,
			ALL_OPT, "all", new CLOptionDescriptor[] { BLEE });

	private static final CLOptionDescriptor FILE = new CLOptionDescriptor("file", CLOptionDescriptor.ARGUMENT_REQUIRED,
			FILE_OPT, "the build file.");

	private static final CLOptionDescriptor TAINT = new CLOptionDescriptor("taint",
			CLOptionDescriptor.ARGUMENT_OPTIONAL, TAINT_OPT, "turn on tainting checks (optional level).");

	public ClutilTestCase() {
		this("Command Line Interpreter Test Case");
	}

	public ClutilTestCase(String name) {
		super(name);
	}

	public void testOptionalArgWithSpace() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(option1.getDescriptor().getId(), CLOption.TEXT_ARGUMENT);
		assertEquals(option1.getArgument(0), "param");

		final CLOption option2 = (CLOption) clOptions.get(2);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testOptionalArgLong() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		// Check that optional args work woth long options
		final String[] args = new String[] { "--taint", "param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(CLOption.TEXT_ARGUMENT, option1.getDescriptor().getId());
		assertEquals("param", option1.getArgument(0));

		final CLOption option2 = (CLOption) clOptions.get(2);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testShortOptArgUnenteredBeforeOtherOpt() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 2, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(option1.getDescriptor().getId(), ALL_OPT);
		assertEquals(option1.getArgument(0), null);
	}

	public void testOptionalArgsWithArgShortBeforeOtherOpt() {
		// "-T3","-a"
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T3", "-a" };

		// System.out.println("[before parsing]");

		final CLArgsParser parser = new CLArgsParser(args, options);

		// System.out.println("[after parsing]");

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 2);<commit_msg>Test cases for optional argument handling. N.B. testOptionalArgLongEquals currently fails; optional arguments don't currently work for long options<commit_after>/* 
 * 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.avalon;

// Renamed from org.apache.avalon.excalibur.cli

import java.util.List;

import junit.framework.TestCase;

/**
 * 
 */
public final class ClutilTestCase extends TestCase {
	private static final String[] ARGLIST1 = new String[] { "--you", "are", "--all", "-cler", "kid" };

	private static final String[] ARGLIST2 = new String[] { "-Dstupid=idiot", "are", "--all", "here", "-d" };

	private static final String[] ARGLIST3 = new String[] {
	// duplicates
			"-Dstupid=idiot", "are", "--all", "--all", "here" };

	private static final String[] ARGLIST4 = new String[] {
	// incompatable (blee/all)
			"-Dstupid", "idiot", "are", "--all", "--blee", "here" };

	private static final String[] ARGLIST5 = new String[] { "-f", "myfile.txt" };

	private static final int DEFINE_OPT = 'D';

	private static final int CASE_CHECK_OPT = 'd';

	private static final int YOU_OPT = 'y';

	private static final int ALL_OPT = 'a';

	private static final int CLEAR1_OPT = 'c';

	private static final int CLEAR2_OPT = 'l';

	private static final int CLEAR3_OPT = 'e';

	private static final int CLEAR5_OPT = 'r';

	private static final int BLEE_OPT = 'b';

	private static final int FILE_OPT = 'f';

	private static final int TAINT_OPT = 'T';

	private static final CLOptionDescriptor DEFINE = new CLOptionDescriptor("define",
			CLOptionDescriptor.ARGUMENTS_REQUIRED_2, DEFINE_OPT, "define");

	private static final CLOptionDescriptor DEFINE_MANY = new CLOptionDescriptor("define",
			CLOptionDescriptor.ARGUMENTS_REQUIRED_2 | CLOptionDescriptor.DUPLICATES_ALLOWED, DEFINE_OPT, "define");

	private static final CLOptionDescriptor CASE_CHECK = new CLOptionDescriptor("charCheck",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CASE_CHECK_OPT, "check character case sensitivity");

	private static final CLOptionDescriptor YOU = new CLOptionDescriptor("you", CLOptionDescriptor.ARGUMENT_DISALLOWED,
			YOU_OPT, "you");

	private static final CLOptionDescriptor CLEAR1 = new CLOptionDescriptor("c",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR1_OPT, "c");

	private static final CLOptionDescriptor CLEAR2 = new CLOptionDescriptor("l",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR2_OPT, "l");

	private static final CLOptionDescriptor CLEAR3 = new CLOptionDescriptor("e",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR3_OPT, "e");

	private static final CLOptionDescriptor CLEAR5 = new CLOptionDescriptor("r",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, CLEAR5_OPT, "r");

	private static final CLOptionDescriptor BLEE = new CLOptionDescriptor("blee",
			CLOptionDescriptor.ARGUMENT_DISALLOWED, BLEE_OPT, "blee");

	private static final CLOptionDescriptor ALL = new CLOptionDescriptor("all", CLOptionDescriptor.ARGUMENT_DISALLOWED,
			ALL_OPT, "all", new CLOptionDescriptor[] { BLEE });

	private static final CLOptionDescriptor FILE = new CLOptionDescriptor("file", CLOptionDescriptor.ARGUMENT_REQUIRED,
			FILE_OPT, "the build file.");

	private static final CLOptionDescriptor TAINT = new CLOptionDescriptor("taint",
			CLOptionDescriptor.ARGUMENT_OPTIONAL, TAINT_OPT, "turn on tainting checks (optional level).");

	public ClutilTestCase() {
		this("Command Line Interpreter Test Case");
	}

	public ClutilTestCase(String name) {
		super(name);
	}

	public void testOptionalArgWithSpace() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(option1.getDescriptor().getId(), CLOption.TEXT_ARGUMENT);
		assertEquals(option1.getArgument(0), "param");

		final CLOption option2 = (CLOption) clOptions.get(2);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testOptionalArgLong() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		// Check that optional args work woth long options
		final String[] args = new String[] { "--taint", "param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(CLOption.TEXT_ARGUMENT, option1.getDescriptor().getId());
		assertEquals("param", option1.getArgument(0));

		final CLOption option2 = (CLOption) clOptions.get(2);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testOptionalArgLongEquals() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		// Check that optional args work woth long options
		final String[] args = new String[] { "--taint=param", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 3, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), "param", option0.getArgument(0));

		final CLOption option2 = (CLOption) clOptions.get(1);
		assertEquals(option2.getDescriptor().getId(), ALL_OPT);
		assertEquals(option2.getArgument(0), null);
	}

	public void testShortOptArgUnenteredBeforeOtherOpt() {
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T", "-a" };

		final CLArgsParser parser = new CLArgsParser(args, options);

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals("Option count", 2, size);

		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
		assertEquals("Option Arg: " + option0.getArgument(0), null, option0.getArgument(0));

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(option1.getDescriptor().getId(), ALL_OPT);
		assertEquals(option1.getArgument(0), null);
	}

	public void testOptionalArgsWithArgShortBeforeOtherOpt() {
		// "-T3","-a"
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T3", "-a" };

		// System.out.println("[before parsing]");

		final CLArgsParser parser = new CLArgsParser(args, options);

		// System.out.println("[after parsing]");

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 2);
		final CLOption option0 = (CLOption) clOptions.get(0);
		assertEquals(option0.getDescriptor().getId(), TAINT_OPT);
		assertEquals(option0.getArgument(0), "3");

		final CLOption option1 = (CLOption) clOptions.get(1);
		assertEquals(ALL_OPT, option1.getDescriptor().getId());
		assertEquals(null, option1.getArgument(0));
	}

	public void testOptionalArgsWithArgShortEqualsBeforeOtherOpt() {
		// "-T3","-a"
		final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };

		final String[] args = new String[] { "-T=3", "-a" };

		// System.out.println("[before parsing]");

		final CLArgsParser parser = new CLArgsParser(args, options);

		// System.out.println("[after parsing]");

		assertNull(parser.getErrorString(), parser.getErrorString());

		final List clOptions = parser.getArguments();
		final int size = clOptions.size();

		assertEquals(size, 2);
{'fraction_non_alphanumeric': 0.12673118154951613, 'fraction_numerical': 0.010662339920032451, 'mean_word_length': 5.183446793264063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4262056', 'n_tokens_mistral': 5505, 'n_tokens_neox': 5101, 'n_words': 1445}