PLearn 0.1
HelpCommand.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // HelpCommand.cc
00004 // 
00005 // Copyright (C) 2003 Pascal Vincent 
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: HelpCommand.cc 10081 2009-04-03 17:43:08Z nouiz $ 
00037  ******************************************************* */
00038 
00040 #include "HelpCommand.h"
00041 
00042 #include <plearn/io/PPath.h>
00043 #include <plearn/sys/Popen.h>
00044 #include <plearn/db/getDataSet.h>
00045 #include <plearn/base/general.h>        
00046 #include <plearn/base/stringutils.h>
00047 #include <plearn/io/fileutils.h>        
00048 #include <plearn/base/HelpSystem.h>
00049 
00050 namespace PLearn {
00051 using namespace std;
00052 
00054 PLearnCommandRegistry HelpCommand::reg_(new HelpCommand);
00055 
00056 void HelpCommand::helpOverview()
00057 {
00058     pout << 
00059         "To run a .plearn script type:                       " + prgname() + " scriptfile.plearn \n"
00060         "To run a command type:                              " + prgname() + " command [ command arguments ] \n\n"
00061         "To get help on the script file format:              " + prgname() + " help scripts \n"
00062         "To get a short description of available commands:   " + prgname() + " help commands \n"
00063         "To get detailed help on a specific command:         " + prgname() + " help <command_name> \n"
00064         "To get help on a specific PLearn object:            " + prgname() + " help <object_type_name> \n"
00065         "To get help on datasets:                            " + prgname() + " help datasets \n\n" 
00066         ""
00067         "Global parameter:\n"
00068         "                 --no-progress: do not print progress bar\n"
00069         "                 --no-version: do not print the version \n"
00070         "                 --windows_endl: use Windows end of line\n"
00071         "                 --profile: print some profiling information\n"
00072         "                     Must have been compiled with '-logging=dbg-profile'\n"
00073         "                 --profile-wall: same as above except we only print the wall time\n"
00074         "                 --verbosity LEVEL: The level of log to print.\n"
00075         "                     Must have been compiled with the same level of more\n"
00076         "                     Available level:\n"
00077         "                             VLEVEL_MAND    // Mandatory\n"
00078         "                             VLEVEL_IMP     // Important\n"
00079         "                             VLEVEL_NORMAL  // Normal (Default)\n"
00080         "                             VLEVEL_DBG     // Debug Info\n"
00081         "                             VLEVEL_EXTREME // Extreme Verbosity\n"
00082         "                 --enable-logging module1,module2,module3,...:\n"
00083         "                     Option to enable logging for the specified modules,\n"
00084         "                     specified a comma-separated list of modules (without spaces).\n"
00085         "                     Special keywords __ALL__ and __NONE__ can be specified to log\n"
00086         "                     for all modules or no modules respectively.\n"
00087         "                 --servers\n"
00088         "                 --global-calendars"
00089          << endl;
00090 }
00091 
00092 void HelpCommand::helpScripts()
00093 {
00094     pout << 
00095         "You can run plearn with the name of a plearn script file as argument\n"
00096         "A plearn script file should have a name ending in .plearn\n\n"
00097         "A plearn script must contain at least one runnable PLearn object\n"
00098         "A typical runnable PLearn object is 'PTester' \n"
00099         "\n"
00100         "You can type '" + prgname() + " help xxx' to get a description and the list of build options\n"
00101         "for any PLearn object xxx linked with the program\n"
00102         "\n"
00103         "A plearn script can use macro variable definitions and expansion. Macro commands start by a $\n"
00104         "ex: $DEFINE{toto}{[1,2,3,4]}  ${toto}  $INCLUDE{otherfile.pscript} \n"
00105         "Macro variable definitions can also be provided on the command line in the form \n"
00106         "varname=varvalue with each such pair separated by a blank, thus\n"
00107         "allowing for scripts with arguments\n"
00108         "In addition, the following variables are automatically defined from the script's filepath: \n"
00109         "FILEPATH DIRPATH FILENAME FILEBASE FILEEXT \n" 
00110         "Ex: if the absolute path to the script file is /home/me/foo.plearn \n"
00111         " Then we'll get: \n"
00112         "FILEPATH = /home/me/foo.plearn \n"
00113         "DIRPATH  = /home/me \n"
00114         "FILENAME = foo.plearn \n"
00115         "FILEBASE = foo \n"
00116         "FILEEXT  = .plearn \n"
00117         "\n"
00118         "The additional variables are also available:\n"
00119         "DATE     = Date in YYYYMMDD format\n"
00120         "TIME     = Time in HHMMSS format\n"
00121         "DATETIME = Date and time in YYYYMMDD:HHMMSS format\n"
00122          << endl;
00123 }
00124 
00125 void HelpCommand::helpCommands()
00126 {
00127     pout << "To run a command, type:"
00128          << "  % " + prgname()  + " command_name command_arguments \n" << endl;
00129 
00130     pout << "Available commands are: " << endl;
00131     pout << HelpSystem::helpCommands() << endl;
00132 
00133     pout << "For more details on a specific command, type: \n" 
00134          << "  % " << prgname() << " help <command_name> \n"
00135          << endl;
00136 }
00137 
00138 void HelpCommand::helpDatasets()
00139 {
00140     pout << getDataSetHelp() << endl;
00141 }
00142 
00143 void HelpCommand::helpAboutPLearnScript(const string& fname)
00144 {
00145     if(!isfile(fname))
00146         PLERROR("Could not open script file %s", fname.c_str());
00147     pout << 
00148         "Help about a .plearn script file not yet implemented \n"
00149          << endl;
00150 }
00151 
00152 void HelpCommand::helpAboutPyPLearnScript(const string& fname)
00153 {
00154     if(!isfile(fname))
00155         PLERROR("Could not open script file %s", fname.c_str());
00156     
00157     pout << "#######################################################" << endl;
00158     pout << "### Help on pyplearn script " << fname << endl;
00159     pout << "#######################################################" << endl;
00160     string command;
00161 #ifdef WIN32
00162     command = "python.exe "+PPath("PLEARNDIR:scripts/pyplearn_driver.py").absolute();
00163 #else
00164     command = "pyplearn_driver.py";
00165 #endif
00166     vector<string> helptext = execute(command+" "+fname+" --help");
00167     vector<string>::const_iterator it = helptext.begin();
00168     while(it!=helptext.end())
00169         pout << *it++ << endl;
00170 }
00171 
00172 
00174 void HelpCommand::run(const vector<string>& args)
00175 {
00176     if(args.size()==0)
00177         helpOverview();//TODO: move to HelpSystem
00178     else
00179     {
00180         string about = args[0];
00181         string aboutext = extract_extension(about);
00182         
00183         if(args.size() > 1)//is option level present?
00184             OptionBase::setCurrentOptionLevel(
00185                 OptionBase::optionLevelFromString(args[1]));
00186 
00187         // Note: some of these functions could be moved to HelpSystem
00188 
00189         if(aboutext==".plearn") // help is asked about a plearn script
00190             helpAboutPLearnScript(about);
00191         else if(aboutext==".pyplearn") // help is asked about a pyplearn script
00192             helpAboutPyPLearnScript(about);
00193         else if(about=="scripts")
00194             helpScripts();
00195         else if(about=="commands")
00196             helpCommands();
00197         else if(about=="datasets")
00198             helpDatasets();//TODO: move to HelpSystem
00199         else if(PLearnCommandRegistry::is_registered(about))
00200             pout << HelpSystem::helpOnCommand(about) << flush;
00201         else 
00202             pout << HelpSystem::helpOnClass(about) << flush;
00203     }
00204 }
00205 
00206 } // end of namespace PLearn
00207 
00208 
00209 /*
00210   Local Variables:
00211   mode:c++
00212   c-basic-offset:4
00213   c-file-style:"stroustrup"
00214   c-file-offsets:((innamespace . 0)(inline-open . 0))
00215   indent-tabs-mode:nil
00216   fill-column:79
00217   End:
00218 */
00219 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines