HelpCmd.h 2.3 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17
// HelpCmd.h:
//
////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
18
// $Id: HelpCmd.h,v 1.4 2008-09-08 15:56:18 fgygi Exp $
Francois Gygi committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

#ifndef HELPCMD_H
#define HELPCMD_H

#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;

#include "UserInterface.h"
#include "Sample.h"

class HelpCmd : public Cmd
{
  public:

  Sample *s;

  HelpCmd(Sample *sample) : s(sample) {};

39 40
  const char *name(void) const { return "help"; }
  const char *help_msg(void) const
Francois Gygi committed
41
  {
42
    return
Francois Gygi committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    "\n help\n\n"
    " syntax: help [command_name]\n\n"
    "   The help command gives a short description of a command. If used\n"
    "   without arguments, help prints a list of valid commands.\n\n";
  }

  int action(int argc, char **argv)
  {
    if ( ui->onpe0() )
    {
      if ( argc == 1 )  // no arguments
      {
        cout << endl << " valid commands are: " << endl << endl;
        list<Cmd*>::iterator cmd = ui->cmdlist.begin();
        int n = 0;
        while ( cmd != ui->cmdlist.end() )
        {
          n++;
          cout.setf(ios::left,ios::adjustfield);
          cout << " " << setw(16) << (*cmd)->name();
          cout.setf(ios::right,ios::adjustfield);
          if ( n%4 == 0 ) cout << endl;
          cmd++;
        }
        if ( n%4 != 0 ) cout << endl;
        cout << endl;
      }
      else if ( argc == 2 ) // one argument
      {
        // search command list
        Cmd *cmdptr = ui->findCmd(argv[1]);

        if ( cmdptr )
        {
          cout << cmdptr->help_msg();
        }
        else
        {
          cout << " help: " << argv[1] << " is not a valid command" << endl;
        }
      }
      else
      {
        cout << " use: help [command_name]" << endl;
      }
    }
    return 0;
  }
};
#endif