package cz.hobrasoft.pdfmu.operation.args;

import java.util.logging.Logger;
import net.sourceforge.argparse4j.inf.Argument;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
import net.sourceforge.argparse4j.inf.Namespace;
import org.apache.commons.lang3.StringUtils;

public class PasswordArgs implements ArgsConfiguration {

    private static final Logger logger = Logger.getLogger(PasswordArgs.class.getName());

    private final String title;
    private final String passArgNameShort;
    private final String passArgNameLong;
    private final String passArgHelp;
    private final String envvarArgNameShort;
    private final String envvarArgNameLong;
    private final String envvarArgHelp;
    private final String envvarArgDefault;

    private char[] password = null;

    public char[] getPassword() {
        return password;
    }

    public PasswordArgs(String title,
            String passArgNameShort,
            String passArgNameLong,
            String passArgHelp,
            String envvarArgNameShort,
            String envvarArgNameLong,
            String envvarArgHelp,
            String envvarArgDefault) {
        this.title = title;
        this.passArgNameShort = passArgNameShort;
        this.passArgNameLong = passArgNameLong;
        this.passArgHelp = passArgHelp;
        this.envvarArgNameShort = envvarArgNameShort;
        this.envvarArgNameLong = envvarArgNameLong;
        this.envvarArgHelp = envvarArgHelp;
        this.envvarArgDefault = envvarArgDefault;
    }

    private static Argument addArgument(MutuallyExclusiveGroup group, String nameShort, String nameLong) {
        assert nameLong != null;
        if (nameShort != null) {
            return group.addArgument("-" + nameShort, "--" + nameLong);
        } else {
            return group.addArgument("--" + nameLong);
        }
    }

    @Override
    public void addArguments(ArgumentParser parser) {
        MutuallyExclusiveGroup keypassGroup = parser.addMutuallyExclusiveGroup(title);
        // TODO?: Add description that states that the arguments are mutually exclusive
        // and that `passArg` takes precedence to `envvarArg`.
        addArgument(keypassGroup, passArgNameShort, passArgNameLong)
                .help(passArgHelp)
                .type(String.class);
        addArgument(keypassGroup, envvarArgNameShort, envvarArgNameLong)
                .help(envvarArgHelp)
                .type(String.class)
                .setDefault(envvarArgDefault);
    }

    @Override
    public void setFromNamespace(Namespace namespace) {
        String passwordString = namespace.getString(passArgNameLong);
        if (passwordString == null) {
            // Load the password from an environment variable
            String envVar = namespace.getString(envvarArgNameLong);
            assert envVar != null; // The argument has a default value
            logger.info(String.format("%s environment variable: %s", StringUtils.capitalize(title), envVar));
            passwordString = System.getenv(envVar);
            if (passwordString != null) {
                logger.info(String.format("%s loaded from the environment variable %s.", StringUtils.capitalize(title), envVar));
            } else {
                logger.info(String.format("%s was not set; using empty password.", StringUtils.capitalize(title)));
            }
        } else {
            logger.info(String.format("%s loaded from the command line option --%s.", StringUtils.capitalize(title), passArgNameLong));
        }
        if (passwordString != null) {
            password = passwordString.toCharArray();
        } else {
            password = null;
        }
    }

}

@@ -2684,32 +2684,50 @@
 (passArgNameLong
+.replace('-', '_')
 );%0A        if (p
@@ -2874,24 +2874,42 @@
 rArgNameLong
+.replace('-', '_')
 );%0A         

Support argument names with dashes