-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDDECmd.cpp
169 lines (143 loc) · 5.28 KB
/
DDECmd.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
////////////////////////////////////////////////////////////////////////////////
//! \file DDECmd.cpp
//! \brief The DDECmd class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "DDECmd.hpp"
#include <WCL/Path.hpp>
#include <WCL/VerInfoReader.hpp>
#include <Core/CmdLineException.hpp>
#include <Core/BadLogicException.hpp>
#include <Core/StringUtils.hpp>
#include "ServersCmd.hpp"
#include "RequestCmd.hpp"
#include "AdviseCmd.hpp"
#include "PokeCmd.hpp"
#include "ExecuteCmd.hpp"
#include "ListenCmd.hpp"
#include "FetchCmd.hpp"
#include "CmdLineArgs.hpp"
#include <ostream>
////////////////////////////////////////////////////////////////////////////////
//! The table of command line switches.
static Core::CmdLineSwitch s_switches[] =
{
{ USAGE, TXT("?"), nullptr, Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Display the program options syntax") },
{ USAGE, TXT("h"), TXT("help"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Display the program options syntax") },
{ VERSION, TXT("v"), TXT("version"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Display the program version") },
{ MANUAL, nullptr, TXT("manual"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Display the manual") },
};
static size_t s_switchCount = ARRAY_SIZE(s_switches);
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
DDECmd::DDECmd()
: m_parser(s_switches, s_switches+s_switchCount)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
DDECmd::~DDECmd()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Run the application.
int DDECmd::run(int argc, tchar* argv[], tistream& /*in*/, tostream& out, tostream& err)
{
// Command specified?
if ( (argc > 1) && ((argv[1][0] != TXT('/')) && (argv[1][0] != TXT('-'))) )
{
// Get command and execute.
WCL::ConsoleCmdPtr command = createCommand(argc, argv);
return command->execute(out, err);
}
m_parser.parse(argc, argv, Core::CmdLineParser::ALLOW_ANY_FORMAT);
// Request for command line syntax?
if (m_parser.isSwitchSet(USAGE))
{
showUsage(out);
return EXIT_SUCCESS;
}
// Request for version?
else if (m_parser.isSwitchSet(VERSION))
{
showVersion(out);
return EXIT_SUCCESS;
}
// Request for the manual?
else if (m_parser.isSwitchSet(MANUAL))
{
showManual(err);
return EXIT_SUCCESS;
}
throw Core::CmdLineException(TXT("No DDE command specified"));
}
////////////////////////////////////////////////////////////////////////////////
//! Create the Comand object.
WCL::ConsoleCmdPtr DDECmd::createCommand(int argc, tchar* argv[])
{
ASSERT(argc > 1);
// Validate command line.
const tchar* command = argv[1];
// Create command.
if (tstricmp(command, TXT("servers")) == 0)
{
return WCL::ConsoleCmdPtr(new ServersCmd(argc, argv));
}
else if (tstricmp(command, TXT("request")) == 0)
{
return WCL::ConsoleCmdPtr(new RequestCmd(argc, argv));
}
else if (tstricmp(command, TXT("advise")) == 0)
{
return WCL::ConsoleCmdPtr(new AdviseCmd(argc, argv, *this));
}
else if (tstricmp(command, TXT("poke")) == 0)
{
return WCL::ConsoleCmdPtr(new PokeCmd(argc, argv));
}
else if (tstricmp(command, TXT("execute")) == 0)
{
return WCL::ConsoleCmdPtr(new ExecuteCmd(argc, argv));
}
else if (tstricmp(command, TXT("listen")) == 0)
{
return WCL::ConsoleCmdPtr(new ListenCmd(argc, argv, *this));
}
else if (tstricmp(command, TXT("fetch")) == 0)
{
return WCL::ConsoleCmdPtr(new FetchCmd(argc, argv, *this));
}
throw Core::CmdLineException(Core::fmt(TXT("Unknown DDE command: '%s'"), command));
}
////////////////////////////////////////////////////////////////////////////////
//! Get the name of the application.
tstring DDECmd::applicationName() const
{
return TXT("DDECmd");
}
////////////////////////////////////////////////////////////////////////////////
//! Display the program options syntax.
void DDECmd::showUsage(tostream& out) const
{
out << std::endl;
out << TXT("USAGE: ") << applicationName() << (" <command> [options] ...") << std::endl;
out << std::endl;
size_t width = 16;
out << TXT("where <command> is one of:-") << std::endl;
out << std::endl;
out << TXT("servers") << tstring(width-7, TXT(' ')) << ("List the running servers and their topics") << std::endl;
out << TXT("request") << tstring(width-7, TXT(' ')) << ("Retrieve the value for one or more items") << std::endl;
out << TXT("advise") << tstring(width-6, TXT(' ')) << ("Listen for updates to one or more items") << std::endl;
out << TXT("poke") << tstring(width-4, TXT(' ')) << ("Set the value for a single item") << std::endl;
out << TXT("execute") << tstring(width-7, TXT(' ')) << ("Send a command for execution") << std::endl;
out << TXT("listen") << tstring(width-6, TXT(' ')) << ("Act as a DDE server") << std::endl;
out << TXT("fetch") << tstring(width-5, TXT(' ')) << ("Fetch the value for one or more items") << std::endl;
out << std::endl;
out << TXT("For help on an individual command use:-") << std::endl;
out << std::endl;
out << TXT("DDECmd <command> -?") << std::endl;
out << std::endl;
out << TXT("Non-command options:-") << std::endl;
out << std::endl;
out << m_parser.formatSwitches(Core::CmdLineParser::UNIX);
}