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 
20 Application::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 
30 {
31 }
32 
34 {
35  return Success;
36 }
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 
91 void Application::usage() const
92 {
93  String s = m_parser.getUsage();
94  output(s);
95 }
96 
98 {
99  return output(*string);
100 }
101 
103 {
104  return m_parser;
105 }
106 
107 const ArgumentParser & Application::parser() const
108 {
109  return m_parser;
110 }
111 
113 {
114  return m_arguments;
115 }
116 
117 void Application::setVersion(const String & version)
118 {
119  m_version = version;
120 }
ArgumentContainer::get
const char * get(const char *name) const
Get argument by name.
Definition: ArgumentContainer.cpp:49
Application::usage
void usage() const
Print usage and terminate.
Definition: Application.cpp:91
Application::~Application
virtual ~Application()
Class destructor.
Definition: Application.cpp:29
Log::Debug
@ Debug
Definition: Log.h:115
Application::setVersion
void setVersion(const String &version)
Set program version.
Definition: Application.cpp:117
Log::setMinimumLogLevel
void setMinimumLogLevel(Level level)
Set the minimum logging level.
Definition: Log.cpp:38
Application::run
virtual int run()
Run the application.
Definition: Application.cpp:38
Application::m_version
String m_version
Program version.
Definition: Application.h:171
String
Abstraction of strings.
Definition: String.h:41
ArgumentParser
Generic command-line argument parser.
Definition: ArgumentParser.h:40
Application::exec
virtual Result exec()=0
Execute the application event loop.
Application::Application
Application(int argc, char **argv)
Class constructor.
Definition: Application.cpp:20
Application::m_argv
char ** m_argv
Input argument values.
Definition: Application.h:160
Application::output
virtual Result output(const char *string) const =0
Print text to output.
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
Application::ShowUsage
@ ShowUsage
Definition: Application.h:59
ArgumentContainer
Generic command-line argument parser.
Definition: ArgumentContainer.h:40
Application::ExitFailure
static const uint ExitFailure
Exit code for failure termination.
Definition: Application.h:46
ArgumentParser::Success
@ Success
Definition: ArgumentParser.h:49
Application::m_arguments
ArgumentContainer m_arguments
Parsed Arguments.
Definition: Application.h:168
WeakSingleton< Log >::instance
static Log * instance()
Retrieve the instance.
Definition: Singleton.h:86
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerFlag
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Definition: ArgumentParser.cpp:100
Application::ExitSuccess
static const uint ExitSuccess
Exit code for successful termination.
Definition: Application.h:43
Application::initialize
virtual Result initialize()
Initialize the application.
Definition: Application.cpp:33
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
Application::m_parser
ArgumentParser m_parser
Program argument parser object.
Definition: Application.h:165
ArgumentParser::getUsage
String getUsage() const
Get program usage.
Definition: ArgumentParser.cpp:42
Application::m_argc
int m_argc
Input argument count.
Definition: Application.h:157
ArgumentParser::setName
void setName(const char *name)
Set program name.
Definition: ArgumentParser.cpp:90
Application.h
ArgumentParser::parse
Result parse(int argc, char **argv, ArgumentContainer &output)
Parse input arguments.
Definition: ArgumentParser.cpp:136