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

org.grails.cli.profile.CommandDescription.groovy Maven / Gradle / Ivy

There is a newer version: 2023.1.0-RC1
Show newest version
/*
 * Copyright 2014-2022 the original author or authors.
 *
 * 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
 *
 *      https://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.grails.cli.profile

import groovy.transform.Canonical
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import jline.console.completer.Completer

/**
 * Describes a {@link Command}
 *
 * @author Graeme Rocher
 * @since 3.0
 */
@CompileStatic
@Canonical
class CommandDescription {

    /**
     * The name of the command
     */
    String name

    /**
     * The description of the command
     */
    String description

    /**
     * The usage instructions for the command
     */
    String usage

    /**
     * Any names that should also map to this command
     */
    Collection synonyms = []

    /**
     * A completer for the command
     */
    Completer completer = null

    private final Map arguments = new LinkedHashMap<>()
    private final Map flags = new LinkedHashMap<>()

    /**
     * Returns an argument for the given name or null if it doesn't exist
     * @param name The name
     * @return The argument or null
     */
    CommandArgument getArgument(String name) {
        arguments[name]
    }

    /**
     * Returns a flag for the given name or null if it doesn't exist
     * @param name The name
     * @return The argument or null
     */
    CommandArgument getFlag(String name) {
        flags[name]
    }

    /**
     * Arguments to the command
     */
    Collection getArguments() {
        arguments.values()
    }

    /**
     * Flags to the command. These differ as they are optional and are prefixed with a hyphen (Example -debug)
     */
    Collection getFlags() {
        flags.values()
    }

    /**
     * Adds a synonyms for this command
     *
     * @param synonyms The synonyms
     * @return This command description
     */
    CommandDescription synonyms(String...synonyms) {
        this.synonyms.addAll(synonyms)
        this
    }

    /**
     * Sets the completer
     *
     * @param completer The class of the completer to set
     * @return The description instance
     */
    CommandDescription completer(Class completer) {
        this.completer = completer.newInstance()
        this
    }

    /**
     * Sets the completer
     *
     * @param completer The completer to set
     * @return The description instance
     */
    CommandDescription completer(Completer completer) {
        this.completer = completer
        this
    }

    /**
     * Adds an argument for the given named arguments
     *
     * @param args The named arguments
     */
    @CompileDynamic
    CommandDescription argument(Map args) {
        def arg = new CommandArgument(args)
        def name = arg.name
        if (name) {
            arguments[name] = arg
        }
        this
    }

    /**
     * Adds a flag for the given named arguments
     *
     * @param args The named arguments
     */
    @CompileDynamic
    CommandDescription flag(Map args) {
        def arg = new CommandArgument(args)
        def name = arg.name
        if (name) {
            arg.required = false
            flags[name] = arg
        }
        this
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy