-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (90 loc) · 2.91 KB
/
Copy pathmain.cpp
File metadata and controls
118 lines (90 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// **************************************************************************
// File [ main.cpp ]
// Author [ littleshamoo ]
// Synopsis [ this is a simple tutorial to demo the command manager ]
// Date [ 2012/04/10 created ]
// **************************************************************************
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#include "sys_cmd.h"
#include "user_cmd.h"
using namespace CommonNs;
void initOpts(OptMgr *mgr);
void initCmds(CmdMgr *mgr);
int main(int argc, char **argv) {
OptMgr *optMgr = new OptMgr;
CmdMgr *cmdMgr = new CmdMgr;
initOpts(optMgr);
initCmds(cmdMgr);
// if -h option is used, show help
optMgr->parse(argc, argv);
if (optMgr->getParsedOpt("h")) {
optMgr->usage();
exit(0);
}
// welcome message
cout << " Welcome to GraphLab \n" ;
cout << " (c) NTUEE Algorithms \n" ;
cout << " V 2012.4 \n" ;
// if -f is used, source the file in batch mode
CmdMgr::Result res = CmdMgr::SUCCESS;
if (optMgr->getParsedOpt("f")) {
char cmd[256];
strcpy(cmd, "source ");
strcat(cmd, optMgr->getParsedValue("f"));
res = cmdMgr->exec(cmd);
}
// keep looping the interactive shell
while (res != CmdMgr::EXIT) {
res = cmdMgr->read();
if (res == CmdMgr::NOT_EXIST) {
fprintf(stderr, "**ERROR main(): command ");
fprintf(stderr, "`%s' not found\n", cmdMgr->getErrorStr());
continue;
}
}
return 0;
}
void initOpts(OptMgr *mgr) {
// set program information
mgr->setName("test");
mgr->setShortDes("test based");
mgr->setDes("test based");
// register options
Opt *opt = new Opt(Opt::BOOL, "print this usage", "");
opt->addFlag("h");
opt->addFlag("help");
mgr->regOpt(opt);
opt = new Opt(Opt::STR_REQ, "execute command file at startup", "FILE");
opt->addFlag("f");
mgr->regOpt(opt);
}
void initCmds(CmdMgr *mgr) {
// user interface
mgr->setComment("#"); // comment
mgr->setPrompt("graphlab> "); // set prompt
mgr->setColor(CmdMgr::YELLOW); // set prompt color
// declear system commands here
Cmd *listCmd = new SysListCmd("ls");
Cmd *cdCmd = new SysCdCmd("cd");
Cmd *exitCmd = new SysExitCmd("exit", mgr);
Cmd *quitCmd = new SysExitCmd("quit", mgr);
Cmd *sourceCmd = new SysSourceCmd("source", mgr);
Cmd *helpCmd = new SysHelpCmd("help", mgr);
Cmd *sysDotCmd = new SysDotCmd("dot");
Cmd *sysDisplayCmd = new SysDisplayCmd("display");
// register system commands here
mgr->regCmd(listCmd);
mgr->regCmd(cdCmd);
mgr->regCmd(exitCmd);
mgr->regCmd(quitCmd);
mgr->regCmd(sourceCmd);
mgr->regCmd(helpCmd);
mgr->regCmd(sysDotCmd);
mgr->regCmd(sysDisplayCmd);
// add your command here
Cmd *testCmd = new TestCmd("test");
mgr->regCmd(testCmd);
}