All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.triceo.robozonky.app.configuration.CommandLineInterface Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta-5
Show newest version
/*
 * Copyright 2017 Lukáš Petrovický
 *
 * 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.io.File;
import java.time.Duration;
import java.time.temporal.TemporalAmount;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Stream;

import com.github.triceo.robozonky.app.App;
import com.github.triceo.robozonky.app.authentication.AuthenticationHandler;
import com.github.triceo.robozonky.app.authentication.SecretProvider;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Processes command line arguments and provides access to their values.
 */
public class CommandLineInterface {

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

    static final int DEFAULT_CAPTCHA_DELAY_SECONDS = 2 * 60;
    static final int DEFAULT_SLEEP_PERIOD_MINUTES = 60;

    static final Option OPTION_STRATEGY = Option.builder("s").hasArg().longOpt("strategy")
            .argName("Investment strategy").desc("Points to a resource holding the investment strategy configuration.")
            .build();
    static final Option OPTION_INVESTMENT = Option.builder("l").hasArg().longOpt("loan")
            .argName("Single loan ID").desc("Ignore strategy, invest to one specific loan and exit.")
            .build();
    static final Option OPTION_AMOUNT = Option.builder("a").hasArg().longOpt("amount")
            .argName("Amount to invest").desc("Amount to invest to a single loan when ignoring strategy.")
            .build();
    static final Option OPTION_USERNAME = Option.builder("u").hasArg().longOpt("username")
            .argName("Zonky username").desc("Used to connect to the Zonky server.").build();
    static final Option OPTION_KEYSTORE = Option.builder("g").hasArg().longOpt("guarded")
            .argName("Guarded file").desc("Path to secure file that contains username, password etc.").build();
    static final Option OPTION_PASSWORD = Option.builder("p").required().hasArg().longOpt("password")
            .argName("Guarded password").desc("Used to connect to the Zonky server, or to read the secure storage.")
            .build();
    static final Option OPTION_USE_TOKEN = Option.builder("r").hasArg().optionalArg(true)
            .argName("Seconds before expiration").longOpt("refresh")
            .desc("Once logged in, RoboZonky will never log out unless login expires. Use with caution.").build();
    static final Option OPTION_FAULT_TOLERANT = Option.builder("t").longOpt("fault-tolerant")
            .desc("RoboZonky will not report errors if it thinks Zonky is down. Use with caution.").build();
    static final Option OPTION_DRY_RUN = Option.builder("d").hasArg().optionalArg(true).
            argName("Dry run balance").longOpt("dry").desc("Simulate the investments, but never actually spend money.")
            .build();
    static final Option OPTION_CLOSED_SEASON = Option.builder("c").hasArg()
            .argName("Delay in seconds before new loans lose CAPTCHA protection.").longOpt("closed-season")
            .desc("Allows to override the default CAPTCHA loan delay.").build();
    static final Option OPTION_ZONK = Option.builder("z").hasArg()
            .argName("The longest amount of time in minutes for which Zonky is allowed to sleep.").longOpt("zonk")
            .desc("Allows to override the default length of sleep period.").build();
    static final Option OPTION_CONFIRMATION = Option.builder("x").hasArg().optionalArg(false)
            .argName("'tool:token'").longOpt("external")
            .desc("Use external tool to confirm investments.").build();

    /**
     * Parse the command line.
     *
     * @param args Command line arguments as received by {@link App#main(String...)}.
     * @return Empty if parsing failed, at which point it will write standard help message to sysout.
     */
    public static Optional parse(final String... args) {
        // create the mode of operation
        final OptionGroup operatingModes = new OptionGroup();
        Stream.of(OperatingMode.values())
                .map(OperatingMode::getSelectingOption)
                .forEach(operatingModes::addOption);
        operatingModes.setRequired(true);
        // include authentication options
        final OptionGroup authenticationModes = new OptionGroup()
                .addOption(CommandLineInterface.OPTION_USERNAME)
                .addOption(CommandLineInterface.OPTION_KEYSTORE);
        authenticationModes.setRequired(true);
        // join all in a single config
        final Options options = new Options()
                .addOptionGroup(operatingModes)
                .addOptionGroup(authenticationModes);
        final Stream




© 2015 - 2024 Weber Informatics LLC | Privacy Policy