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;
 
 
Display help pages.

Author(s):
Jason Dillon
Since:
2.5
 
 @Command(name="help")
 @Preferences(path = "commands/help")
 public class HelpCommand
     extends CommandActionSupport
 {
     private final HelpPageManager helpPages;
 
     // TODO: maybe use an enum here to say; --include groups,commands,aliases (exclude meta) etc...
     
     @Preference
     @Option(name="c", longName="include-commands", optionalArg=true)
     private Boolean includeCommands = true;
 
     @Preference
     @Option(name="a", longName="include-aliases", optionalArg=true)
     private Boolean includeAliases = true;
 
     @Preference
     @Option(name="g", longName="include-groups", optionalArg=true)
     private Boolean includeGroups = true;
 
     @Preference
     @Option(name="m", longName="include-meta", optionalArg=true)
     private Boolean includeMeta = true;
 
     @Preference
     @Option(name="A", longName="include-all", optionalArg=true)
     private Boolean includeAll;
     
     @Argument
     private String name;
 
     @Inject
     public HelpCommand(final HelpPageManager helpPages) {
         assert helpPages != null;
         this. = helpPages;
     }
 
     @Inject
     public HelpCommand installCompleters(final @Named("alias-name"Completer c1,
                                          final @Named("node-path"Completer c2,
                                          final @Named("meta-help-page-name"Completer c3)
     {
         setCompleters(new AggregateCompleter(c1c2c3), null);
         return this;
     }
 
     public Object execute(final CommandContext contextthrows Exception {
         assert context != null;
        IO io = context.getIo();
        // If there is no argument given, display all help pages in context
        if ( == null) {
            displayAvailable(context);
            return .;
        }
        // First try a direct match
        HelpPage page = .getPage();
        // if not direct match, then look for similar pages
        if (page == null) {
            Collection<HelpPagepages = .getPages(filter(new Filter<HelpPage>()
            {
                public boolean accept(final HelpPage page) {
                    assert page != null;
                    return page.getName().contains() || page.getDescription().contains();
                }
            }));
            if (pages.size() == 1) {
                // if there is only one match, treat as a direct match
                page = pages.iterator().next();
            }
            else if (pages.size() > 1) {
                // else show matching pages
                io.out.println(getMessages().format("info.matching-pages"));
                HelpPageUtil.render(io.outpages);
                return .;
            }
        }
        // if not page matched, complain
        if (page == null) {
            io.err.println(getMessages().format("error.help-not-found"));
            return .;
        }
        // else render the matched page
        page.render(io.out);
        return .;
    }
    private void displayAvailable(final CommandContext context) {
        assert context != null;
        Collection<HelpPagepages = .getPages(filter());
        IO io = context.getIo();
        io.out.println(getMessages().format("info.available-pages"));
        HelpPageUtil.render(io.outpages);
    }
    private AggregateFilter<HelpPagefilter(final Filter<HelpPage>... filters) {
        // filters may be null
        
        AndFilter<HelpPagefilter = new AndFilter<HelpPage>();
        if ( == null || !) {
            if ( != null && !) {
                filter.not(new TypeFilter<HelpPage>(AliasHelpPage.class));
            }
            if ( != null && !) {
                filter.not(new TypeFilter<HelpPage>(MetaHelpPage.class));
            }
            if ( != null && !) {
                filter.not(new TypeFilter<HelpPage>(CommandHelpPage.class));
            }
            if ( != null && !) {
                filter.not(new TypeFilter<HelpPage>(GroupHelpPage.class));
            }
        }
        if (filters != null) {
            Collections.addAll(filter.getFilters(), filters);
        }
        return filter;
    }
New to GrepCode? Check out our FAQ X