<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.oodt.cas.cli.parser;

//OODT static imports
import static org.apache.oodt.cas.cli.util.CmdLineUtils.findHelpOption;
import static org.apache.oodt.cas.cli.util.CmdLineUtils.getOptionByName;
import static org.apache.oodt.cas.cli.util.CmdLineUtils.isSubOption;
import static org.apache.oodt.cas.cli.util.CmdLineUtils.sortOptionsByRequiredStatus;

//JDK imports
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

//OODT imports
import org.apache.commons.lang.Validate;
import org.apache.oodt.cas.cli.help.OptionHelpException;
import org.apache.oodt.cas.cli.option.CmdLineOption;
import org.apache.oodt.cas.cli.option.CmdLineOptionInstance;
import org.apache.oodt.cas.cli.option.GroupCmdLineOption;
import org.apache.oodt.cas.cli.option.GroupSubOption;
import org.apache.oodt.cas.cli.util.Args;

//Google imports
import com.google.common.annotations.VisibleForTesting;

/**
 * Standard Command-line parser which parser command line options of the form
 * --longOption -shortOption. Supports group options, options with values, and
 * without values.
 * 
 * @author bfoster (<NAME>)
 */
public class StdCmdLineOptionParser implements CmdLineOptionParser {

   public Set<CmdLineOptionInstance> parse(Args args,
         Set<CmdLineOption> validOptions) throws IOException {
      HashSet<CmdLineOptionInstance> optionInstances = new HashSet<CmdLineOptionInstance>();

      CmdLineOption helpOption = findHelpOption(validOptions);
      if (helpOption == null) {
         throw new OptionHelpException(
               "Must specify a help option in set of valid options");
      }

      if (args.numArgs() < 1) {
         throw new OptionHelpException("Must specify options : type -"
               + helpOption.getShortOption() + " or --"
               + helpOption.getLongOption() + " for info");
      }

      Stack<CmdLineOptionInstance> groupOptions = new Stack<CmdLineOptionInstance>();
      for (String arg : args) {

         if (isOption(arg)) {

            // check if option is a valid one
            CmdLineOption option = getOptionByName(getOptionName(arg),
                  validOptions);
            if (option == null) {
               throw new IOException("Invalid option: '" + arg + "'");
            }

            // read found option
            CmdLineOptionInstance specifiedOption = getOption(args, option);

            // Check if we are currently loading subOptions.
            if (!groupOptions.isEmpty()) {

               CmdLineOptionInstance currentGroup = groupOptions.peek();

               // Check if option is a subOption for current group.
               if (!isSubOption(currentGroup.getOption(), option)) {

                  // Check if current group was expecting more subOptions.
                  Set<CmdLineOption> requiredSubOptions = verifyGroupHasRequiredSubOptions(currentGroup);
                  if (!requiredSubOptions.isEmpty()) {
                     throw new IOException(
                           "Missing the following required subOptions for '"
                                 + currentGroup.getOption()
                                 + "': "
                                 + sortOptionsByRequiredStatus(requiredSubOptions));

                  } else {

                     // pop group and add to list of specified options.
                     optionInstances.add(groupOptions.pop());
                  }
               } else {

                  // Add option to current group subOptions.
                  currentGroup.addSubOption(specifiedOption);
                  continue;

               }
            }

            if (option instanceof GroupCmdLineOption) {

               // Push group as current group.
               groupOptions.push(specifiedOption);

            } else {

               // Option good to go.
               optionInstances.add(specifiedOption);
            }
         } else {
            throw new IOException("Invalid argument: '" + arg + "'");
         }
      }
      while (!groupOptions.isEmpty()) {
         CmdLineOptionInstance currentGroup = groupOptions.pop();<commit_msg>- fixed subOptions from be able to be used at a stand-along top level option - group options now require that at least on subOption should be specified<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.oodt.cas.cli.parser;

//OODT static imports
import static org.apache.oodt.cas.cli.util.CmdLineUtils.findHelpOption;
import static org.apache.oodt.cas.cli.util.CmdLineUtils.getOptionByName;
import static org.apache.oodt.cas.cli.util.CmdLineUtils.isSubOption;
import static org.apache.oodt.cas.cli.util.CmdLineUtils.sortOptionsByRequiredStatus;

//JDK imports
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;

//OODT imports
import org.apache.commons.lang.Validate;
import org.apache.oodt.cas.cli.help.OptionHelpException;
import org.apache.oodt.cas.cli.option.CmdLineOption;
import org.apache.oodt.cas.cli.option.CmdLineOptionInstance;
import org.apache.oodt.cas.cli.option.GroupCmdLineOption;
import org.apache.oodt.cas.cli.option.GroupSubOption;
import org.apache.oodt.cas.cli.util.Args;

//Google imports
import com.google.common.annotations.VisibleForTesting;

/**
 * Standard Command-line parser which parser command line options of the form
 * --longOption -shortOption. Supports group options, options with values, and
 * without values.
 * 
 * @author bfoster (<NAME>)
 */
public class StdCmdLineOptionParser implements CmdLineOptionParser {

   public Set<CmdLineOptionInstance> parse(Args args,
         Set<CmdLineOption> validOptions) throws IOException {
      HashSet<CmdLineOptionInstance> optionInstances = new HashSet<CmdLineOptionInstance>();

      CmdLineOption helpOption = findHelpOption(validOptions);
      if (helpOption == null) {
         throw new OptionHelpException(
               "Must specify a help option in set of valid options");
      }

      if (args.numArgs() < 1) {
         throw new OptionHelpException("Must specify options : type -"
               + helpOption.getShortOption() + " or --"
               + helpOption.getLongOption() + " for info");
      }

      Stack<CmdLineOptionInstance> groupOptions = new Stack<CmdLineOptionInstance>();
      for (String arg : args) {

         if (isOption(arg)) {

            // check if option is a valid one
            CmdLineOption option = getOptionByName(getOptionName(arg),
                  validOptions);
            if (option == null) {
               throw new IOException("Invalid option: '" + arg + "'");
            }

            // read found option
            CmdLineOptionInstance specifiedOption = getOption(args, option);

            // Check if we are currently loading subOptions.
            if (!groupOptions.isEmpty()) {

               CmdLineOptionInstance currentGroup = groupOptions.peek();

               // Check if option is a subOption for current group.
               if (!isSubOption(currentGroup.getOption(), option)) {

                  // Check if current group was expecting more subOptions.
                  Set<CmdLineOption> requiredSubOptions = verifyGroupHasRequiredSubOptions(currentGroup);
                  if (!requiredSubOptions.isEmpty()) {
                     throw new IOException(
                           "Missing the following required subOptions for '"
                                 + currentGroup.getOption()
                                 + "': "
                                 + sortOptionsByRequiredStatus(requiredSubOptions));

                  } else if (currentGroup.getSubOptions().isEmpty()) {
                     throw new IOException(
                           "Must specify a subOption for group option '"
                                 + currentGroup.getOption() + "'");

                  } else {

                     // pop group and add to list of specified options.
                     optionInstances.add(groupOptions.pop());
                  }
               } else {

                  // Add option to current group subOptions.
                  currentGroup.addSubOption(specifiedOption);
                  continue;

               }
            }

            if (option instanceof GroupCmdLineOption) {

               // Push group as current group.
               groupOptions.push(specifiedOption);

            } else if (option.isSubOption()) {
               throw new IOException("Option '" + option
                     + "' is a subOption, but was used at top level Option");

            } else {

               // Option good to go.
               optionInstances.add(specifiedOption);
            }
         } else {
            throw new IOException("Invalid argument: '" + arg + "'");
         }
      }
      while (!groupOptions.isEmpty()) {
         CmdLineOptionInstance currentGroup = groupOptions.pop();
{'fraction_non_alphanumeric': 0.08648242150780222, 'fraction_numerical': 0.0009400263207369807, 'mean_word_length': 2.280604378661733, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5530081', 'n_tokens_mistral': 2646, 'n_tokens_neox': 2554, 'n_words': 796}