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

org.apache.jackrabbit.oak.console.commands.OakHelpCommand.groovy Maven / Gradle / Ivy

There is a newer version: 1.72.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.jackrabbit.oak.console.commands

import org.codehaus.groovy.tools.shell.Command
import org.codehaus.groovy.tools.shell.CommandSupport
import org.codehaus.groovy.tools.shell.Groovysh
import org.codehaus.groovy.tools.shell.completion.CommandNameCompleter

/**
 * Copy of org.codehaus.groovy.tools.shell.commands.HelpCommand to customize
 * the banner
 */
class OakHelpCommand extends CommandSupport{
    public static final String COMMAND_NAME = ':help'

    OakHelpCommand(final Groovysh shell) {
        super(shell, COMMAND_NAME, ':h')

        alias('?', ':?')
    }

    protected List createCompleters() {
        return [
                new CommandNameCompleter(registry, false),
                null
        ]
    }

    Object execute(final List args) {
        assert args != null

        if (args.size() > 1) {
            fail(messages.format('error.unexpected_args', args.join(' ')))
        }

        if (args.size() == 1) {
            help(args[0])
        }
        else {
            list()
        }

        return null
    }

    private void help(final String name) {
        assert name

        Command command = registry.find(name)
        if (!command) {
            fail("No such command: $name") // TODO: i18n
        }

        io.out.println()
        io.out.println("usage: @|bold ${command.name}|@ $command.usage") // TODO: i18n
        io.out.println()
        io.out.println(command.help)
        io.out.println()
    }

    private void list() {
        // Figure out the max command name and shortcut length dynamically
        int maxName = 0
        int maxShortcut

        for (Command command in registry.commands()) {
            if (command.hidden) {
                continue
            }

            if (command.name.size() > maxName) {
                maxName = command.name.size()
            }

            if (command.shortcut.size() > maxShortcut) {
                maxShortcut = command.shortcut.size()
            }
        }

        io.out.println()
        io.out.println('For information about @|green Apache Jackrabbit Oak|@, visit:') // TODO: i18n
        io.out.println('    @|cyan http://jackrabbit.apache.org/oak/|@ ')
        io.out.println()

        // List the commands we know about
        io.out.println('Available commands:') // TODO: i18n

        for (Command command in registry.commands()) {
            if (command.hidden) {
                continue
            }

            def n = command.name.padRight(maxName, ' ')
            def s = command.shortcut.padRight(maxShortcut, ' ')

            //
            // TODO: Wrap description if needed
            //

            def d = command.description

            io.out.println("  @|bold ${n}|@  (@|bold ${s}|@) $d")
        }

        io.out.println()
        io.out.println('For help on a specific command type:') // TODO: i18n
        io.out.println('    :help @|bold command|@ ')
        io.out.println()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy