source: trunk/FACT++/src/test3.cc@ 10378

Last change on this file since 10378 was 10372, checked in by tbretz, 14 years ago
Changed second argument in Parse to const as well as in all main functions.
File size: 4.9 KB
Line 
1#include <boost/regex.hpp>
2#include <boost/filesystem.hpp>
3
4#include "Shell.h"
5#include "Configuration.h"
6#include "RemoteControl.h"
7
8using namespace std;
9
10template <class T>
11void RunShell(Configuration &conf)
12{
13 // A normal kill will call its destructor! (Very nice feature ;) )
14 static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
15
16 WindowLog &win = shell.GetStreamIn();
17 WindowLog &wout = shell.GetStreamOut();
18
19 if (conf.Has("log"))
20 if (!wout.OpenLogFile(conf.Get<string>("log")))
21 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
22
23 shell.Run();
24}
25
26
27// ========================================================================
28void SetupConfiguration(Configuration &conf)
29{
30 const string n = conf.GetName()+".log";
31
32 po::options_description config("Program options");
33 config.add_options()
34 ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
35 ("log,l", var<string>(n), "Write log-file")
36 ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
37 ;
38
39 conf.AddEnv("dns", "DIM_DNS_NODE");
40
41 conf.AddOptions(config);
42}
43
44/*
45 Extract usage clause(s) [if any] for SYNOPSIS.
46 Translators: "Usage" and "or" here are patterns (regular expressions) which
47 are used to match the usage synopsis in program output. An example from cp
48 (GNU coreutils) which contains both strings:
49 Usage: cp [OPTION]... [-T] SOURCE DEST
50 or: cp [OPTION]... SOURCE... DIRECTORY
51 or: cp [OPTION]... -t DIRECTORY SOURCE...
52 */
53void PrintUsage()
54{
55 cout << "\n"
56 "The console connects to all available Dim Servers and allows to "
57 "easily access all of their commands.\n"
58 "\n"
59 "Usage: test3 [-c type] [OPTIONS]\n"
60 " or: test3 [OPTIONS]\n"
61 "\n"
62 "Options:\n"
63 "The following describes the available commandline options. "
64 "For further details on how command line option are parsed "
65 "and in which order which configuration sources are accessed "
66 "please refer to the class reference of the Configuration class.";
67 cout << endl;
68
69}
70
71void PrintHelp()
72{
73 cout <<
74 "The default is that the program is started without user intercation. "
75 "All actions are supposed to arrive as DimCommands. Using the -c "
76 "option, a local shell can be initialized. With h or help a short "
77 "help message about the usuage can be brought to the screen."
78 << endl;
79
80 /*
81 cout << "bla bla bla" << endl << endl;
82 cout << endl;
83 cout << "Environment:" << endl;
84 cout << "environment" << endl;
85 cout << endl;
86 cout << "Examples:" << endl;
87 cout << "test exam" << endl;
88 cout << endl;
89 cout << "Files:" << endl;
90 cout << "files" << endl;
91 cout << endl;
92 */
93}
94
95/*
96 The first line of the --version information is assumed to be in one
97 of the following formats:
98
99 <version>
100 <program> <version>
101 {GNU,Free} <program> <version>
102 <program> ({GNU,Free} <package>) <version>
103 <program> - {GNU,Free} <package> <version>
104
105 and separated from any copyright/author details by a blank line.
106
107 Handle multi-line bug reporting sections of the form:
108
109 Report <program> bugs to <addr>
110 GNU <package> home page: <url>
111 ...
112*/
113void PrintVersion(const char *name)
114{
115 string n = boost::filesystem::basename(name);
116 if (n.substr(0, 3)=="lt-")
117 n = n.substr(3);
118
119 cout <<
120 n << " - FACT++ 1.0\n"
121 "\n"
122 "Written by Thomas Bretz <thomas.bretz@epfl.ch> et al.\n"
123 "\n"
124 "Report bugs to Thomas Bretz <thomas.bretz@epfl.ch>\n"
125 "FACT++ home page: http://www.xxx.com\n"
126 "\n"
127 "Copyright (C) 2011 by the FACT Collaboration.\n"
128 "This is free software; see the source for copying conditions.\n"
129 << endl;
130}
131
132int main(int argc, const char *argv[])
133{
134 Configuration conf(argv[0]);
135 conf.SetPrintUsage(PrintUsage);
136 SetupConfiguration(conf);
137
138 po::variables_map vm;
139 try
140 {
141 vm = conf.Parse(argc, argv);
142 }
143 catch (std::exception &e)
144 {
145#if BOOST_VERSION > 104000
146 po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
147 if (MO)
148 cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
149 else
150#endif
151 cout << "Error: " << e.what() << endl;
152 cout << endl;
153
154 return -1;
155 }
156
157 if (conf.HasPrint())
158 return -1;
159
160 if (conf.HasVersion())
161 {
162 PrintVersion(argv[0]);
163 return -1;
164 }
165
166 if (conf.HasHelp())
167 {
168 PrintHelp();
169 return -1;
170 }
171
172 // To allow overwriting of DIM_DNS_NODE set 0 to 1
173 setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
174
175 if (conf.Get<int>("console")==0)
176 RunShell<RemoteShell>(conf);
177 else
178 RunShell<RemoteConsole>(conf);
179
180
181 return 0;
182}
Note: See TracBrowser for help on using the repository browser.