<commit_before>/*
 * Copyright 2011 SpringSource
 *
 * Licensed 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.codehaus.groovy.grails.cli.parsing;

import grails.util.Environment;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Command line parser that parses arguments to the command line. Written as a replacement for Commons CLI
 * because it doesn't support unknown arguments and requires all arguments to be declared up front.
 *
 *  It also doesn't support command options with hyphens. This class gets around those problems.
 *
 * @author <NAME>
 * @since 1.4
 */
public class CommandLineParser {

    static Map<String, String> ENV_ARGS = new HashMap<String, String>();
    static Map<String, String> DEFAULT_ENVS = new HashMap<String, String>();
    private static final String DEFAULT_PADDING = "        ";

    static {
        ENV_ARGS.put("dev", Environment.DEVELOPMENT.getName());
        ENV_ARGS.put("prod", Environment.PRODUCTION.getName());
        ENV_ARGS.put("test", Environment.TEST.getName());
        DEFAULT_ENVS.put("war", Environment.PRODUCTION.getName());
        DEFAULT_ENVS.put("test-app", Environment.TEST.getName());
    }

    private Map<String, Option> declaredOptions = new HashMap<String, Option> ();
    private int longestOptionNameLength = 0;
    private String usageMessage;

    /**
     * Adds a declared option
     *
     * @param name The name of the option
     * @param description The description
     */
    public void addOption(String name, String description) {
        int length = name.length();
        if(length >longestOptionNameLength) {
            longestOptionNameLength = length;
        }
        declaredOptions.put(name, new Option(name, description));
    }

    /**
     * Parses a string of all the command line options converting them into an array of arguments to pass to #parse(String..args)
     *
     * @param string The string
     * @return The command line
     */
    public CommandLine parseString(String string) {
        // stupid implementation right now that doesn't take into account quoted argument values
        String[] args = string.split(" ");
        return parse(args);
    }

    /**
     * Parses the given list of command line arguments. Arguments starting with -D become system properties,
     * arguments starting with -- or - become either declared or undeclared options. All other arguments are
     * put into a list of remaining arguments
     *
     * @param args The arguments
     * @return The command line state
     */
    public CommandLine parse(String... args) {
        DefaultCommandLine cl = createCommandLine();
        boolean first = true;
        for (String arg : args) {
            if(arg == null) continue;
            String trimmed = arg.trim();
            if(trimmed != null && trimmed.length()>0) {
                if(trimmed.charAt(0) == '-') {
                    processOption(cl, trimmed);
                }
                else {
                   if(ENV_ARGS.containsKey(trimmed)) {
                       cl.setEnvironment(ENV_ARGS.get(trimmed));
                   }
                   else {
                      if(first) {
                          cl.setCommandName(trimmed);
                          first = false;
                      }
                      else {
                          cl.addRemainingArg(trimmed);
                      }
                   }
                }
            }
        }

        return cl;
    }

    public String getHelpMessage() {
        String ls = System.getProperty("line.separator");
        usageMessage = "usage: grails [options] [command]";
        StringBuilder sb = new StringBuilder(usageMessage);
        sb.append(ls);
        for (Option option : declaredOptions.values()) {
            String name = option.getName();
            int extraPadding = longestOptionNameLength - name.length();
            sb.append(" -").append(name);
            for (int i = 0; i < extraPadding; i++) {
                sb.append(' ');
            }
            sb.append(DEFAULT_PADDING).append(option.getDescription()).append(ls);
        }<commit_msg>bug fix - if an arg value was 'test', 'prod', or 'dev' it would change the environment, so now only looking for environment options before the command<commit_after>/*
 * Copyright 2011 SpringSource
 *
 * Licensed 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.codehaus.groovy.grails.cli.parsing;

import grails.util.Environment;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Command line parser that parses arguments to the command line. Written as a replacement for Commons CLI
 * because it doesn't support unknown arguments and requires all arguments to be declared up front.
 *
 *  It also doesn't support command options with hyphens. This class gets around those problems.
 *
 * @author <NAME>
 * @since 1.4
 */
public class CommandLineParser {

    static Map<String, String> ENV_ARGS = new HashMap<String, String>();
    static Map<String, String> DEFAULT_ENVS = new HashMap<String, String>();
    private static final String DEFAULT_PADDING = "        ";

    static {
        ENV_ARGS.put("dev", Environment.DEVELOPMENT.getName());
        ENV_ARGS.put("prod", Environment.PRODUCTION.getName());
        ENV_ARGS.put("test", Environment.TEST.getName());
        DEFAULT_ENVS.put("war", Environment.PRODUCTION.getName());
        DEFAULT_ENVS.put("test-app", Environment.TEST.getName());
    }

    private Map<String, Option> declaredOptions = new HashMap<String, Option> ();
    private int longestOptionNameLength = 0;
    private String usageMessage;

    /**
     * Adds a declared option
     *
     * @param name The name of the option
     * @param description The description
     */
    public void addOption(String name, String description) {
        int length = name.length();
        if(length >longestOptionNameLength) {
            longestOptionNameLength = length;
        }
        declaredOptions.put(name, new Option(name, description));
    }

    /**
     * Parses a string of all the command line options converting them into an array of arguments to pass to #parse(String..args)
     *
     * @param string The string
     * @return The command line
     */
    public CommandLine parseString(String string) {
        // stupid implementation right now that doesn't take into account quoted argument values
        String[] args = string.split(" ");
        return parse(args);
    }

    /**
     * Parses the given list of command line arguments. Arguments starting with -D become system properties,
     * arguments starting with -- or - become either declared or undeclared options. All other arguments are
     * put into a list of remaining arguments
     *
     * @param args The arguments
     * @return The command line state
     */
    public CommandLine parse(String... args) {
        DefaultCommandLine cl = createCommandLine();
        boolean beforeCommand = true;
        for (String arg : args) {
            if(arg == null) continue;
            String trimmed = arg.trim();
            if(trimmed != null && trimmed.length()>0) {
                if(trimmed.charAt(0) == '-') {
                    processOption(cl, trimmed);
                }
                else {
                   if(beforeCommand && ENV_ARGS.containsKey(trimmed)) {
                       cl.setEnvironment(ENV_ARGS.get(trimmed));
                   }
                   else {
                      if(beforeCommand) {
                          cl.setCommandName(trimmed);
                          beforeCommand = false;
                      }
                      else {
                          cl.addRemainingArg(trimmed);
                      }
                   }
                }
            }
        }

        return cl;
    }

    public String getHelpMessage() {
        String ls = System.getProperty("line.separator");
        usageMessage = "usage: grails [options] [command]";
        StringBuilder sb = new StringBuilder(usageMessage);
        sb.append(ls);
        for (Option option : declaredOptions.values()) {
            String name = option.getName();
            int extraPadding = longestOptionNameLength - name.length();
            sb.append(" -").append(name);
            for (int i = 0; i < extraPadding; i++) {
                sb.append(' ');
            }
            sb.append(DEFAULT_PADDING).append(option.getDescription()).append(ls);
        }
{'fraction_non_alphanumeric': 0.09530822638333684, 'fraction_numerical': 0.0029455081001472753, 'mean_word_length': 2.181726907630522, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '4674889', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2333, 'n_words': 804}