org.shaf.core.process.cmd.CommandOptionHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.core.process.cmd.CommandOptionHandler.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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 org.shaf.core.process.cmd;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.shaf.core.content.DescriptionContent;
import org.shaf.core.content.GroupOptionInfo;
import org.shaf.core.content.ResourceInfo;
import org.shaf.core.content.SingleOptionInfo;

/**
 * This handler generates the command line {@code Options} object from the
 * {@link Process process} {@link DescriptionContent description content} .
 * 
 * @author Mykola Galushka
 */
public class CommandOptionHandler {

    /**
     * Get {@code Options} constructed from the specified class.
     * 
     * @param descr
     *            the process descriptor.
     * @return the {@code Options} object.
     */
    public static Options getOptions(DescriptionContent descr) {
        Options options = new Options();

        for (ResourceInfo resource : descr) {
            if (resource.getOptionInfo() instanceof SingleOptionInfo) {
                options.addOption(generate((SingleOptionInfo) resource.getOptionInfo()));
            } else {
                options.addOptionGroup(generate((GroupOptionInfo) resource.getOptionInfo()));
            }
        }

        return options;
    }

    /**
     * Generates a command line option for the specified
     * {@code SingleOptionInfo}.
     * 
     * @param singleOptionInfo
     *            the specified {@code SingleOptionInfo}.
     * @return the generated command line option.
     */
    private static Option generate(final SingleOptionInfo singleOptionInfo) {
        Option option = new Option(singleOptionInfo.getName(), singleOptionInfo.getArg() != null,
                singleOptionInfo.getDescr());

        if (singleOptionInfo.getArg() != null) {
            option.setArgName(singleOptionInfo.getArg());
        }
        option.setRequired(singleOptionInfo.isRequired());

        return option;
    }

    /**
     * Generates a command line option group for the specified
     * {@code GroupOptionInfo}.
     * 
     * @param groupOptionInfo
     *            the specified {@code GroupOptionInfo} .
     * @return the generated command line option group.
     */
    private static OptionGroup generate(final GroupOptionInfo groupOptionInfo) {
        OptionGroup group = new OptionGroup();

        for (SingleOptionInfo info : groupOptionInfo) {
            group.addOption(generate(info));
        }
        group.setRequired(groupOptionInfo.isRequired());

        return group;
    }
}