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

org.exist.util.ArgumentUtil Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
/*
 * eXist-db Open Source Native XML Database
 * Copyright (C) 2001 The eXist-db Authors
 *
 * [email protected]
 * http://www.exist-db.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.exist.util;

import se.softhouse.jargo.Argument;
import se.softhouse.jargo.ParsedArguments;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Utility functions for working with Jargo
 */
public class ArgumentUtil {

    /**
     * Get the value of an optional argument.
     *
     * @param  the type of the argument.
     *
     * @param parsedArguments The arguments which have been parsed
     * @param argument The argument that we are looking for
     *
     * @return Some value or {@link Optional#empty()} if the
     *     argument was not supplied
     */
    public static  Optional getOpt(final ParsedArguments parsedArguments, final Argument argument) {
        if(parsedArguments.wasGiven(argument)) {
            return Optional.of(parsedArguments.get(argument));
        } else {
            return Optional.empty();
        }
    }

    /**
     * Get the values of an optional argument.
     *
     * @param  the type of the argument.
     *
     * @param parsedArguments The arguments which have been parsed
     * @param argument The argument that we are looking for
     *
     * @return A list of the provided argument values, or
     *     an empty list if the argument was not supplied
     */
    public static  List getListOpt(final ParsedArguments parsedArguments, final Argument> argument) {
        return getOpt(parsedArguments, argument)
                .orElseGet(Collections::emptyList);
    }

    /**
     * Get the value of an optional file argument
     *
     * @param parsedArguments The arguments which have been parsed
     * @param argument The argument that we are looking for
     *
     * @return Some {@link java.nio.file.Path} or
     *     {@link Optional#empty()} if the argument was not supplied
     */
    public static Optional getPathOpt(final ParsedArguments parsedArguments, final Argument argument) {
        return getOpt(parsedArguments, argument).map(File::toPath);
    }

    public static List getPathsOpt(final ParsedArguments parsedArguments, final Argument> argument) {
        try(final Stream files = getListOpt(parsedArguments, argument).stream()) {
            return files.map(File::toPath)
                    .collect(Collectors.toList());
        }
    }

    /**
     * Get the value of an option argument
     *
     * @param parsedArguments The arguments which have been parsed
     * @param argument The option argument that we are looking for
     *
     * @return true if the option was set, false otherwise
     */
    public static boolean getBool(final ParsedArguments parsedArguments, final Argument argument) {
        return getOpt(parsedArguments, argument)
                .flatMap(Optional::ofNullable)
                .orElse(false);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy