CmdLineTool.hpp 2.97 KB
Newer Older
1
/*
2
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2015-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
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
 */

#pragma once

#include <istream>
#include <ostream>

namespace cta {
namespace catalogue {

/**
 * Abstract class implementing common code and data structures for a
 * command-line tool.
 */
class CmdLineTool {
public:
  /**
   * Constructor.
   *
   * @param inStream Standard input stream.
   * @param outStream Standard output stream.
   * @param errStream Standard error stream.
   */
  CmdLineTool(std::istream &inStream, std::ostream &outStream, std::ostream &errStream) noexcept;

  /**
   * Pure-virtual destructor to guarantee this class is abstract.
   */
  virtual ~CmdLineTool() noexcept = 0;

46
47
48
49
50
51
52
53
54
55
56
57
  /**
   * The object's implementation of main() that should be called from the main()
   * of the program.
   *
   * @param argc The number of command-line arguments including the program name.
   * @param argv The command-line arguments.
   * @return The exit value of the program.
   */
  int main(const int argc, char *const *const argv);

protected:

58
59
60
61
62
63
64
65
66
  /**
   * An exception throwing version of main().
   *
   * @param argc The number of command-line arguments including the program name.
   * @param argv The command-line arguments.
   * @return The exit value of the program.
   */
  virtual int exceptionThrowingMain(const int argc, char *const *const argv) = 0;

67
68
69
70
71
72
  /**
   * Prints the usage message of the command-line tool.
   *
   * @param os The output stream to which the usage message is to be printed.
   */
  virtual void printUsage(std::ostream &os) = 0;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

  /**
   * Standard input stream.
   */
  std::istream &m_in;

  /**
   * Standard output stream.
   */
  std::ostream &m_out;

  /**
   * Standard error stream.
   */
  std::ostream &m_err;

89
90
91
92
93
  /**
   * Returns the name of the user running the command-line tool.
   *
   * @return The name of the user running the command-line tool.
   */
94
  static std::string getUsername();
95
96
97
98
99
100

  /**
   * Returns the name of the host on which the command-line tool is running.
   *
   * @return The name of the host on which the command-line tool is running.
   */
101
  static std::string getHostname();
102

103
104
105
106
}; // class CmdLineTool

} // namespace catalogue
} // namespace cta