source: trunk/FACT++/src/chatclient.cc@ 10798

Last change on this file since 10798 was 10628, checked in by tbretz, 14 years ago
Unified help output more.
File size: 3.2 KB
Line 
1#include <boost/filesystem.hpp>
2
3#include "FACT.h"
4#include "Configuration.h"
5#include "ChatClient.h"
6
7using namespace std;
8
9template <class T>
10void RunShell(Configuration &conf)
11{
12 // A normal kill will call its destructor! (Very nice feature ;) )
13 static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
14
15 WindowLog &win = shell.GetStreamIn();
16 WindowLog &wout = shell.GetStreamOut();
17
18 if (conf.Has("log"))
19 if (!wout.OpenLogFile(conf.Get<string>("log")))
20 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
21
22 shell.Run();
23}
24
25
26// ========================================================================
27void SetupConfiguration(Configuration &conf)
28{
29 const string n = conf.GetName()+".log";
30
31 po::options_description config("Program options");
32 config.add_options()
33 ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
34 ("log,l", var<string>(n), "Write log-file")
35 ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
36 ;
37
38 conf.AddEnv("dns", "DIM_DNS_NODE");
39
40 conf.AddOptions(config);
41}
42
43/*
44 Extract usage clause(s) [if any] for SYNOPSIS.
45 Translators: "Usage" and "or" here are patterns (regular expressions) which
46 are used to match the usage synopsis in program output. An example from cp
47 (GNU coreutils) which contains both strings:
48 Usage: cp [OPTION]... [-T] SOURCE DEST
49 or: cp [OPTION]... SOURCE... DIRECTORY
50 or: cp [OPTION]... -t DIRECTORY SOURCE...
51 */
52void PrintUsage()
53{
54 cout << "\n"
55 "The chatclient is a simple Dim based chatclient.\n"
56 "\n"
57 "The chatclient is always started with user intercation. "
58 "Just enter a message. It will be broadcasted through the chatserv, "
59 "which need to be running."
60 "\n"
61 "Usage: chatclient [-c type] [OPTIONS]\n"
62 " or: chatclient [OPTIONS]\n";
63 cout << endl;
64
65}
66
67void PrintHelp()
68{
69 /* Additional help text which is printed after the configuration
70 options goes here */
71}
72
73int main(int argc, const char *argv[])
74{
75 Configuration conf(argv[0]);
76 conf.SetPrintUsage(PrintUsage);
77 SetupConfiguration(conf);
78
79 po::variables_map vm;
80 try
81 {
82 vm = conf.Parse(argc, argv);
83 }
84 catch (std::exception &e)
85 {
86#if BOOST_VERSION > 104000
87 po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
88 if (MO)
89 cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
90 else
91#endif
92 cout << "Error: " << e.what() << endl;
93 cout << endl;
94
95 return -1;
96 }
97
98 if (conf.HasPrint())
99 return -1;
100
101 if (conf.HasVersion())
102 {
103 FACT::PrintVersion(argv[0]);
104 return -1;
105 }
106
107 if (conf.HasHelp())
108 {
109 PrintHelp();
110 return -1;
111 }
112
113 // To allow overwriting of DIM_DNS_NODE set 0 to 1
114 setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
115
116 if (conf.Get<int>("console")==0)
117 RunShell<ChatShell>(conf);
118 else
119 RunShell<ChatConsole>(conf);
120
121
122 return 0;
123}
Note: See TracBrowser for help on using the repository browser.