<commit_before>package net.browsertime.tool.run;

import net.browsertime.tool.BrowserConfig;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.Parser;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import static java.util.Arrays.asList;
import static net.browsertime.tool.run.CliParser.OptionString.*;

/**
 *
 */
public class CliParser {
    private static final Browser DEFAULT_BROWSER = Browser.firefox;
    private static final Format DEFAULT_FORMAT = Format.xml;
    private static final int DEFAULT_TIMEOUT_SECONDS = 60;
    private static final int DEFAULT_NUMBER_OF_ITERATIONS = 3;

    private CommandLine commandLine;
    private final Options options;

    public CliParser() {
        options = createCliOptions();
    }

    public void parseArgs(String[] args) throws ParseException {
        Parser parser = new BasicParser();
        commandLine = parser.parse(options, args);
    }

    public boolean shouldShowHelp() {
        return commandLine.hasOption(helpOption.longForm);
    }

    public boolean shouldShowVersion() {
        return commandLine.hasOption(versionOption.longForm);
    }

    public URL parseUrl() throws MalformedURLException, ParseException {
        String[] urlArgs = commandLine.getArgs();
        if (urlArgs.length != 1) {
            throw new ParseException("One url must be passed as the last argument.");
        }

        return new URL(urlArgs[0]);
    }

    public TimingConfig parseTimingConfig() throws IOException, ParseException {
        TimingConfig config = new TimingConfig();

        config.numIterations = parseIterations();
        config.shouldPrettyPrint = !commandLine.hasOption(compactOption.longForm);
        config.shouldIncludeRuns = commandLine.hasOption(rawOption.longForm);
        config.browser = parseBrowser();
        config.timeoutSeconds = parseTimeout();
        config.format = parseFormat();

        config.outputWriter = parseSerializationWriter(commandLine.getOptionValue(outputOption.longForm));

        config.browserOptions = parseBrowserOptions();

        return config;
    }

    private Map<BrowserConfig, String> parseBrowserOptions() throws ParseException {
        Map<BrowserConfig, String> map = new HashMap<BrowserConfig, String>();

        String size = commandLine.getOptionValue(windowSizeOption.longForm);
        if (size != null) {
            if (!size.matches("\\d+x\\d+")) {
                throw new ParseException("Window size is <width>x<height>");
            }
            map.put(BrowserConfig.windowSize, size);
        }

        String ua = commandLine.getOptionValue(userAgentOption.longForm);
        if (ua != null) {
            map.put(BrowserConfig.userAgent, ua);
        }

        String proxyHost = commandLine.getOptionValue(proxyHostOption.shortForm);
        if (proxyHost != null) {
            try {
                new URI(proxyHost);
            } catch (URISyntaxException e) {
                throw new ParseException("Invalid proxy url: " + proxyHost);
            }

            map.put(BrowserConfig.proxyHost, proxyHost);
        }

        return map;
    }

    private Browser parseBrowser() throws ParseException {
        String browser = commandLine.getOptionValue(browserOption.longForm, DEFAULT_BROWSER.name());
        try {
            return Browser.valueOf(browser);
        } catch (IllegalArgumentException e) {
            throw new ParseException("Invalid browser: " + browser);
        }
    }

    private int parseTimeout() throws ParseException {
        if (!commandLine.hasOption(timeoutOption.longForm)) {
            return DEFAULT_TIMEOUT_SECONDS;
        }
        String timeout = commandLine.getOptionValue(timeoutOption.longForm);
        try {
            int i = Integer.parseInt(timeout);
            if (i <= 0) {<commit_msg>Make argument parsing code consistent by always using long form of argument.<commit_after>package net.browsertime.tool.run;

import net.browsertime.tool.BrowserConfig;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.Parser;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import static java.util.Arrays.asList;
import static net.browsertime.tool.run.CliParser.OptionString.*;

/**
 *
 */
public class CliParser {
    private static final Browser DEFAULT_BROWSER = Browser.firefox;
    private static final Format DEFAULT_FORMAT = Format.xml;
    private static final int DEFAULT_TIMEOUT_SECONDS = 60;
    private static final int DEFAULT_NUMBER_OF_ITERATIONS = 3;

    private CommandLine commandLine;
    private final Options options;

    public CliParser() {
        options = createCliOptions();
    }

    public void parseArgs(String[] args) throws ParseException {
        Parser parser = new BasicParser();
        commandLine = parser.parse(options, args);
    }

    public boolean shouldShowHelp() {
        return commandLine.hasOption(helpOption.longForm);
    }

    public boolean shouldShowVersion() {
        return commandLine.hasOption(versionOption.longForm);
    }

    public URL parseUrl() throws MalformedURLException, ParseException {
        String[] urlArgs = commandLine.getArgs();
        if (urlArgs.length != 1) {
            throw new ParseException("One url must be passed as the last argument.");
        }

        return new URL(urlArgs[0]);
    }

    public TimingConfig parseTimingConfig() throws IOException, ParseException {
        TimingConfig config = new TimingConfig();

        config.numIterations = parseIterations();
        config.shouldPrettyPrint = !commandLine.hasOption(compactOption.longForm);
        config.shouldIncludeRuns = commandLine.hasOption(rawOption.longForm);
        config.browser = parseBrowser();
        config.timeoutSeconds = parseTimeout();
        config.format = parseFormat();

        config.outputWriter = parseSerializationWriter(commandLine.getOptionValue(outputOption.longForm));

        config.browserOptions = parseBrowserOptions();

        return config;
    }

    private Map<BrowserConfig, String> parseBrowserOptions() throws ParseException {
        Map<BrowserConfig, String> map = new HashMap<BrowserConfig, String>();

        String size = commandLine.getOptionValue(windowSizeOption.longForm);
        if (size != null) {
            if (!size.matches("\\d+x\\d+")) {
                throw new ParseException("Window size is <width>x<height>");
            }
            map.put(BrowserConfig.windowSize, size);
        }

        String ua = commandLine.getOptionValue(userAgentOption.longForm);
        if (ua != null) {
            map.put(BrowserConfig.userAgent, ua);
        }

        String proxyHost = commandLine.getOptionValue(proxyHostOption.longForm);
        if (proxyHost != null) {
            try {
                new URI(proxyHost);
            } catch (URISyntaxException e) {
                throw new ParseException("Invalid proxy url: " + proxyHost);
            }

            map.put(BrowserConfig.proxyHost, proxyHost);
        }

        return map;
    }

    private Browser parseBrowser() throws ParseException {
        String browser = commandLine.getOptionValue(browserOption.longForm, DEFAULT_BROWSER.name());
        try {
            return Browser.valueOf(browser);
        } catch (IllegalArgumentException e) {
            throw new ParseException("Invalid browser: " + browser);
        }
    }

    private int parseTimeout() throws ParseException {
        if (!commandLine.hasOption(timeoutOption.longForm)) {
            return DEFAULT_TIMEOUT_SECONDS;
        }
        String timeout = commandLine.getOptionValue(timeoutOption.longForm);
        try {
            int i = Integer.parseInt(timeout);
            if (i <= 0) {
{'fraction_non_alphanumeric': 0.09600277264325323, 'fraction_numerical': 0.0013863216266173752, 'mean_word_length': 2.9547738693467336, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4225431', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2261, 'n_words': 527}