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

Last change on this file since 10303 was 10303, checked in by tbretz, 9 years ago
Added some examples how a proper --help output should look to be properly converted by help2man.
File size: 4.6 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    /*
80     cout << "bla bla bla" << endl << endl;
81     cout << endl;
82     cout << "Environment:" << endl;
83     cout << "environment" << endl;
84     cout << endl;
85     cout << "Examples:" << endl;
86     cout << "test exam" << endl;
87     cout << endl;
88     cout << "Files:" << endl;
89     cout << "files" << endl;
90     cout << endl;
91     */
92}
93
94/*
95 The first line of the --version information is assumed to be in one
96 of the following formats:
97
98   <version>
99   <program> <version>
100   {GNU,Free} <program> <version>
101   <program> ({GNU,Free} <package>) <version>
102   <program> - {GNU,Free} <package> <version>
103
104 and separated from any copyright/author details by a blank line.
105
106 Handle multi-line bug reporting sections of the form:
107
108   Report <program> bugs to <addr>
109   GNU <package> home page: <url>
110   ...
111*/
112void PrintVersion(const char *name)
113{
114    cout <<
115        name << " - FACT++ 1.0\n"
116        "\n"
117        "Written by Thomas Bretz <thomas.bretz@epfl.ch> et al.\n"
118        "\n"
119        "Report bugs to Thomas Bretz <thomas.bretz@epfl.ch>\n"
120        "FACT++ home page: http://www.xxx.com\n"
121        "\n"
122        "Copyright (C) 2011 by the FACT Collaboration.\n"
123        "This is free software; see the source for copying conditions.\n"
124        << endl;
125}
126
127int main(int argc, char *argv[])
128{
129    Configuration conf(argv[0]);
130    conf.SetPrintUsage(PrintUsage);
131    SetupConfiguration(conf);
132
133    po::variables_map vm;
134    try
135    {
136        vm = conf.Parse(argc, argv);
137    }
138    catch (std::exception &e)
139    {
140#if BOOST_VERSION > 104000
141        po::multiple_occurrences *MO = dynamic_cast<po::multiple_occurrences*>(&e);
142        if (MO)
143            cout << "Error: " << e.what() << " of '" << MO->get_option_name() << "' option." << endl;
144        else
145#endif
146            cout << "Error: " << e.what() << endl;
147        cout << endl;
148
149        return -1;
150    }
151
152    if (conf.HasPrint())
153        return -1;
154
155    if (conf.HasVersion())
156    {
157        PrintVersion(argv[0]);
158        return -1;
159    }
160
161    if (conf.HasHelp())
162    {
163        PrintHelp();
164        return -1;
165    }
166
167    // To allow overwriting of DIM_DNS_NODE set 0 to 1
168    setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
169
170    if (conf.Get<int>("console")==0)
171        RunShell<RemoteShell>(conf);
172    else
173        RunShell<RemoteConsole>(conf);
174
175
176    return 0;
177}
Note: See TracBrowser for help on using the repository browser.