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

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