HelpCommand.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "HelpCommand.h"
00019 
00020 #include "memory/Memory.h"
00021 #include "util/StringBuffer.h"
00022 #include "util/StringUtils.h"
00023 
00024 namespace oasys {
00025 
00026 HelpCommand::HelpCommand()
00027     : TclCommand("help")
00028 {
00029     add_to_help("help <cmd>", "Print the help documentation for cmd");
00030 }
00031 
00032 int
00033 HelpCommand::exec(int argc, const char** argv, Tcl_Interp* interp)
00034 {
00035     (void)interp;
00036     
00037     const TclCommandList *cmdlist = NULL;
00038     TclCommandList::const_iterator iter;
00039     
00040     cmdlist = TclCommandInterp::instance()->commands();
00041 
00042     if (argc == 1) 
00043     {
00044         StringBuffer buf;
00045         int len = 0;
00046 
00047         buf.append("For help on a particular command, type \"help <cmd>\".\n");
00048         buf.append("The registered commands are: \n\t");
00049                    
00050         std::vector<std::string> cmd_names;
00051         for (iter = cmdlist->begin(); iter != cmdlist->end(); ++iter) {
00052             cmd_names.push_back((*iter)->name());
00053         }
00054         std::sort(cmd_names.begin(), cmd_names.end(), StringLessThan());
00055 
00056         for (std::vector<std::string>::iterator j =  cmd_names.begin();
00057              j != cmd_names.end(); ++j)
00058         {
00059             if (len > 60) {
00060                 buf.appendf("\n\t");
00061                 len = 0;
00062             }
00063 
00064             len += buf.appendf("%s ", j->c_str());
00065         }
00066         set_result(buf.c_str());
00067         return TCL_OK;
00068     } 
00069     else if (argc == 2) 
00070     {
00071         for (iter = cmdlist->begin(); iter != cmdlist->end(); iter++) {
00072             if (!strcmp((*iter)->name(), argv[1])) {
00073                 const char *help = (*iter)->help_string();
00074 
00075                 if (!help || (help && help[0] == '\0')) {
00076                     help = "(no help, sorry)";
00077                 }
00078 
00079                 if ((*iter)->hasBindings()) {
00080                     append_resultf("%s cmd_info\n\t%s", (*iter)->name(),
00081                                    "Lists settable parameters.\n\n");
00082                 }
00083 
00084                 append_result(help);
00085                 
00086                 return TCL_OK;
00087             }
00088         }
00089 
00090         resultf("no registered command '%s'", argv[1]);
00091         return TCL_ERROR;
00092         
00093     } else {
00094         wrong_num_args(argc, argv, 2, 3, 3);
00095         return TCL_ERROR;
00096     }
00097 }
00098 
00099 } // namespace oasys

Generated on Thu Jun 7 12:54:27 2007 for DTN Reference Implementation by  doxygen 1.5.1