Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009-2011 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, 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.sonatype.gshell.commands.standard;
 
 
 import java.util.List;
 import java.util.Map;

Define an alias or list defined aliases.

Author(s):
Jason Dillon
Since:
2.5
 
 @Command(name="alias")
 public class AliasCommand
     extends CommandActionSupport
     implements CliProcessorAware
 {
     private final AliasRegistry aliasRegistry;
 
     @Argument(index = 0)
     private String name;
 
     @Argument(index = 1)
     private List<Stringtarget;
 
     @Inject
     public AliasCommand(final AliasRegistry aliasRegistry) {
         assert aliasRegistry != null;
         this. = aliasRegistry;
     }
 
     @Inject
     public AliasCommand installCompleters(@Named("alias-name"final Completer c1) {
         assert c1 != null;
         setCompleters(c1null);
         return this;
     }
 
     public void setProcessor(final CliProcessor processor) {
         assert processor != null;
         processor.setStopAtNonOption(true);
     }
 
     public Object execute(final CommandContext contextthrows Exception {
         assert context != null;
 
         if ( == null) {
             return listAliases(context);
         }
         
         return defineAlias(context);
     }
 
     private Object listAliases(final CommandContext contextthrows Exception {
         assert context != null;
         IO io = context.getIo();
 
         .debug("Listing defined aliases");
 
         Map<String,Stringaliases = .getAliases();
 
         if (aliases.isEmpty()) {
             io.println(getMessages().format("info.no-aliases"));
         }
         else {
             // Determine the maximum name length
             int maxNameLen = 0;
             for (String name : aliases.keySet()) {
                 if (name.length() > maxNameLen) {
                     maxNameLen = name.length();
                 }
             }
            io.out.println(getMessages().format("info.defined-aliases"));
            String nameFormat = "%-" + maxNameLen + 's';
            for (Map.Entry<String,Stringentry : aliases.entrySet()) {
                String formattedName = String.format(nameFormatentry.getKey());
                io.out.format("  @|bold %s|@ "formattedName);
                io.out.println(getMessages().format("info.alias-to"entry.getValue()));
            }
        }
        return .;
    }
    private Object defineAlias(final CommandContext contextthrows Exception {
        assert context != null;
        IO io = context.getIo();
        if ( == null) {
            io.error(getMessages().format("error.missing-arg"), getMessages().getMessage("command.argument.target.token"));
            return .;
        }
        String alias = Strings.join(.toArray(), " ");
        .debug("Defining alias: {} -> {}"alias);
        .registerAlias(alias);
        return .;
    }
New to GrepCode? Check out our FAQ X