FreeNOS
Application.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "Application.h"
19
20Application::Application(int argc, char **argv)
21{
22 m_argc = argc;
23 m_argv = argv;
24 m_parser.registerFlag('h', "help", "show program help");
25 m_parser.registerFlag('v', "version", "show program version");
26 m_parser.registerFlag('d', "debug", "set log level to debug");
27}
28
32
37
39{
40 // Set application name
41 if (m_argc < 1)
42 {
43 usage();
44 return ExitFailure;
45 }
46 else
48
49 // Parse commandline arguments
51 {
52 usage();
53 return ExitFailure;
54 }
55
56 // If the help argument is given, show the usage
57 if (m_arguments.get("help"))
58 {
59 usage();
60 return ExitFailure;
61 }
62
63 // If the version argument is given, show version
64 if (m_arguments.get("version"))
65 {
67 output("\n");
68 return ExitSuccess;
69 }
70
71 // Enable debug logging if specified
72 if (m_arguments.get("debug") && Log::instance())
73 {
75 }
76
77 // Initialize the application first
78 Result r = initialize();
79 if (r == ShowUsage)
80 usage();
81 if (r != Success)
82 return ExitFailure;
83
84 // Run the application
85 if (exec() == Success)
86 return ExitSuccess;
87 else
88 return ExitFailure;
89}
90
92{
94 output(s);
95}
96
98{
99 return output(*string);
100}
101
106
108{
109 return m_parser;
110}
111
113{
114 return m_arguments;
115}
116
117void Application::setVersion(const String & version)
118{
119 m_version = version;
120}
ArgumentContainer m_arguments
Parsed Arguments.
virtual Result initialize()
Initialize the application.
char ** m_argv
Input argument values.
String m_version
Program version.
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
void setVersion(const String &version)
Set program version.
virtual ~Application()
Class destructor.
Application(int argc, char **argv)
Class constructor.
static const uint ExitFailure
Exit code for failure termination.
Definition Application.h:46
ArgumentParser m_parser
Program argument parser object.
static const uint ExitSuccess
Exit code for successful termination.
Definition Application.h:43
virtual int run()
Run the application.
void usage() const
Print usage and terminate.
virtual Result output(const char *string) const =0
Print text to output.
virtual Result exec()=0
Execute the application event loop.
ArgumentParser & parser()
Get program arguments parser.
int m_argc
Input argument count.
Generic command-line argument parser.
const char * get(const char *name) const
Get argument by name.
Generic command-line argument parser.
String getUsage() const
Get program usage.
Result parse(int argc, char **argv, ArgumentContainer &output)
Parse input arguments.
void setName(const char *name)
Set program name.
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
void setMinimumLogLevel(Level level)
Set the minimum logging level.
Definition Log.cpp:38
@ Debug
Definition Log.h:115
Abstraction of strings.
Definition String.h:42
static Log * instance()
Retrieve the instance.
Definition Singleton.h:86