<commit_before>/*
 * Copyright 2017 <NAME>
 *
 * 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 com.github.triceo.robozonky.app.configuration;

import java.util.Optional;
import java.util.stream.Stream;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.ParametersDelegate;
import com.github.triceo.robozonky.app.investing.InvestmentMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Converts command line into application configuration using {@link JCommander}.
 */
public class CommandLineInterface {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommandLineInterface.class);

    private static Optional<InvestmentMode> terminate(final JCommander jc, final String message) {
        System.out.println(message);
        return CommandLineInterface.terminate(jc);
    }

    private static Optional<InvestmentMode> terminate(final JCommander jc) {
        jc.usage();
        return Optional.empty();
    }

    @ParametersDelegate
    private AuthenticationCommandLineFragment authenticationFragment = new AuthenticationCommandLineFragment();

    @ParametersDelegate
    private TweaksCommandLineFragment tweaksFragment = new TweaksCommandLineFragment();

    @ParametersDelegate
    private ConfirmationCommandLineFragment confirmationFragment = new ConfirmationCommandLineFragment();

    @Parameter(names = {"-h", "--help"}, help = true, description = "Print usage end exit.")
    private boolean help;

    /**
     * Takes command-line arguments and converts them into an application configuration, printing command line usage
     * information in case the arguments are somehow invalid.
     *
     * @param args Command-line arguments, coming from the main() method.
     * @return Present if the arguments resulted in a valid configuration, empty otherwise.
     */
    public static Optional<InvestmentMode> parse(final String... args) {
        final CommandLineInterface cli = new CommandLineInterface();
        final JCommander jc = new JCommander(cli);
        jc.setProgramName(CommandLineInterface.getScriptIdentifier());
        Stream.of(OperatingMode.values()).forEach(mode -> jc.addCommand(mode.getName(), mode));
        try { // internal validation
            jc.parse(args);
            if (cli.help) { // don't validate since the CLI is likely to be invalid
                return CommandLineInterface.terminate(jc);
            }
            cli.setImplementation(jc);
            cli.validate();
            return cli.newApplicationConfiguration();
        } catch (final ParameterException ex) {
            CommandLineInterface.LOGGER.debug("Command line parsing ended with parameter exception.", ex);
            return CommandLineInterface.terminate(jc, ex.getMessage());
        }
    }

    private OperatingMode mode;

    private void validate() throws ParameterException {
        Stream.of(authenticationFragment, mode, confirmationFragment, tweaksFragment).forEach(CommandLineFragment::validate);
    }

    private void setImplementation(final JCommander jc) {
        this.mode = (OperatingMode)jc.getCommands().get(jc.getParsedCommand()).getObjects().get(0);
    }

    private Optional<InvestmentMode> newApplicationConfiguration() {
        return SecretProviderFactory.getSecretProvider(this)
                .map(secrets -> Optional.ofNullable(mode)
                        .map(i -> i.configure(this, authenticationFragment.createAuthenticationHandler(secrets)))
                        .orElse(Optional.empty()))
                .orElse(Optional.empty());
    }

    AuthenticationCommandLineFragment getAuthenticationFragment() {
        return authenticationFragment;
    }

    TweaksCommandLineFragment getTweaksFragment() {
        return tweaksFragment;
    }

    ConfirmationCommandLineFragment getConfirmationFragment() {
        return confirmationFragment;
    }

    static String getScriptIdentifier() {<commit_msg>Fix NPE when command not specified (closes #101)<commit_after>/*
 * Copyright 2017 <NAME>
 *
 * 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 com.github.triceo.robozonky.app.configuration;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.ParametersDelegate;
import com.github.triceo.robozonky.app.investing.InvestmentMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Converts command line into application configuration using {@link JCommander}.
 */
public class CommandLineInterface {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommandLineInterface.class);

    private static Optional<InvestmentMode> terminate(final JCommander jc, final String message) {
        System.out.println(message);
        return CommandLineInterface.terminate(jc);
    }

    private static Optional<InvestmentMode> terminate(final JCommander jc) {
        jc.usage();
        return Optional.empty();
    }

    @ParametersDelegate
    private AuthenticationCommandLineFragment authenticationFragment = new AuthenticationCommandLineFragment();

    @ParametersDelegate
    private TweaksCommandLineFragment tweaksFragment = new TweaksCommandLineFragment();

    @ParametersDelegate
    private ConfirmationCommandLineFragment confirmationFragment = new ConfirmationCommandLineFragment();

    @Parameter(names = {"-h", "--help"}, help = true, description = "Print usage end exit.")
    private boolean help;

    /**
     * Takes command-line arguments and converts them into an application configuration, printing command line usage
     * information in case the arguments are somehow invalid.
     *
     * @param args Command-line arguments, coming from the main() method.
     * @return Present if the arguments resulted in a valid configuration, empty otherwise.
     */
    public static Optional<InvestmentMode> parse(final String... args) {
        final CommandLineInterface cli = new CommandLineInterface();
        final JCommander jc = new JCommander(cli);
        jc.setProgramName(CommandLineInterface.getScriptIdentifier());
        Stream.of(OperatingMode.values()).forEach(mode -> jc.addCommand(mode.getName(), mode));
        try { // internal validation
            jc.parse(args);
            if (cli.help) { // don't validate since the CLI is likely to be invalid
                return CommandLineInterface.terminate(jc);
            }
            cli.setImplementation(jc);
            cli.validate();
            return cli.newApplicationConfiguration();
        } catch (final ParameterException ex) {
            CommandLineInterface.LOGGER.debug("Command line parsing ended with parameter exception.", ex);
            return CommandLineInterface.terminate(jc, ex.getMessage());
        }
    }

    private OperatingMode mode;

    private void validate() throws ParameterException {
        Stream.of(authenticationFragment, mode, confirmationFragment, tweaksFragment).forEach(CommandLineFragment::validate);
    }

    private void setImplementation(final JCommander jc) {
        final String parsedCommand = jc.getParsedCommand();
        if (parsedCommand == null) {
            throw new ParameterException("You must specify one mode of operation. Check usage.");
        }
        final JCommander command = jc.getCommands().get(parsedCommand);
        final List<Object> objects = command.getObjects();
        this.mode = (OperatingMode)objects.get(0);
    }

    private Optional<InvestmentMode> newApplicationConfiguration() {
        return SecretProviderFactory.getSecretProvider(this)
                .map(secrets -> Optional.ofNullable(mode)
                        .map(i -> i.configure(this, authenticationFragment.createAuthenticationHandler(secrets)))
                        .orElse(Optional.empty()))
                .orElse(Optional.empty());
    }

    AuthenticationCommandLineFragment getAuthenticationFragment() {
        return authenticationFragment;
    }

    TweaksCommandLineFragment getTweaksFragment() {
        return tweaksFragment;
    }

    ConfirmationCommandLineFragment getConfirmationFragment() {
        return confirmationFragment;
    }

    static String getScriptIdentifier() {
{'fraction_non_alphanumeric': 0.08576274780027562, 'fraction_numerical': 0.0026502703275734125, 'mean_word_length': 3.4416195856873824, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 20, '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': '2410475', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2283, 'n_words': 671}