home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (Shell.groovy)

This example Groovy source code file (Shell.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

command, command, commandregistry, executing, io, io, list, logger, object, result, shell, shell, string, string

The Groovy Shell.groovy source code

 * Copyright 2003-2007 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
 *     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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package org.codehaus.groovy.tools.shell

import org.codehaus.groovy.tools.shell.util.Logger

 * A simple shell for invoking commands from a command-line.
 * @version $Id: Shell.groovy 8432 2007-10-07 08:55:37Z user57 $
 * @author <a href="mailto:jason@planet57.com">Jason Dillon
class Shell
    protected final Logger log = Logger.create(this.class)

    final CommandRegistry registry = new CommandRegistry()

    final IO io

    Shell(final IO io) {
        assert io
        this.io = io
    Shell() {
        this(new IO())
    protected List parseLine(final String line) {
        assert line != null
        return line.trim().tokenize()
    Command findCommand(final String line) {
        assert line
        // TODO: Introduce something like 'boolean Command.accepts(String)' to ask
        //       commands if they can take the line?
        //       Would like to get '!66' to invoke the 'history recall' bits, but currently has
        //       to be '! 66' for it to work with an alias like:
        //           alias ! history recall
        //       Or maybe allow commands to register specific syntax hacks into the registry?
        //       then ask the registry for the command for a given line?
        def args = parseLine(line)
        assert args.size() > 0
        def name = args[0]
        def command = registry[name]
        return command
    boolean isExecutable(final String line) {
        return findCommand(line) != null
    Object execute(final String line) {
        assert line
        def command = findCommand(line)
        def result = null
        if (command) {
            def args = parseLine(line)
            if (args.size() == 1) {
                args = []
            else {
                args = args[1..-1]
            log.debug("Executing command($command.name): $command; w/args: $args")
            result = command.execute(args)
            log.debug("Result: ${String.valueOf(result)}")
        return result
    Command register(final Command command) {
        return registry << command
    def leftShift(final String line) {
        return execute(line)
    def leftShift(final Command command) {
        return register(command)

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Shell.groovy source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.