FreeNOS
ArgumentContainer.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 "ArgumentContainer.h"
19 
21 {
22 }
23 
25 {
26  // cleanup flags
28  it.hasCurrent();)
29  {
30  delete it.current();
31  it.remove();
32  }
33 
34  // cleanup positionals
35  for (Size i = 0; i < m_positionals.count(); i++)
36  delete m_positionals[i];
37 }
38 
40 {
41  return m_positionals;
42 }
43 
45 {
46  return m_flags;
47 }
48 
49 const char * ArgumentContainer::get(const char *name) const
50 {
51  // Search flag arguments
52  const Argument * const *arg = m_flags.get(name);
53  if (arg)
54  return *(*arg)->getValue();
55 
56  // Search positional arguments
57  for (Size i = 0; i < m_positionals.count(); i++)
58  {
59  const Argument *a = m_positionals[i];
60 
61  if (a->getName().equals(name))
62  return *a->getValue();
63  }
64  return ZERO;
65 }
66 
68 {
69  m_positionals.insert(arg);
70  return Success;
71 }
72 
74 {
75  m_flags.insert(arg->getName(), arg);
76  return Success;
77 }
ArgumentContainer::get
const char * get(const char *name) const
Get argument by name.
Definition: ArgumentContainer.cpp:49
HashTable< String, Argument * >
HashIterator
Iterate through a HashTable.
Definition: HashIterator.h:39
ArgumentContainer::m_positionals
Vector< Argument * > m_positionals
Contains all positional arguments.
Definition: ArgumentContainer.h:113
ArgumentContainer::Success
@ Success
Definition: ArgumentContainer.h:49
ArgumentContainer.h
ArgumentContainer::m_flags
HashTable< String, Argument * > m_flags
Contains all flag arguments.
Definition: ArgumentContainer.h:110
Argument
Represents program command line argument.
Definition: Argument.h:35
ArgumentContainer::addPositional
Result addPositional(Argument *arg)
Add positional argument.
Definition: ArgumentContainer.cpp:67
HashTable::get
virtual const V * get(const K &key) const
Returns the first value for the given key.
Definition: HashTable.h:287
ArgumentContainer::Result
Result
Result codes.
Definition: ArgumentContainer.h:47
ArgumentContainer::ArgumentContainer
ArgumentContainer()
Constructor.
Definition: ArgumentContainer.cpp:20
Vector::count
virtual Size count() const
Returns the number of items inside the Vector.
Definition: Vector.h:204
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
ArgumentContainer::getFlags
const HashTable< String, Argument * > & getFlags() const
Get flag arguments.
Definition: ArgumentContainer.cpp:44
Vector::insert
virtual int insert(const T &item)
Adds the given item to the Vector, if possible.
Definition: Vector.h:93
ArgumentContainer::~ArgumentContainer
virtual ~ArgumentContainer()
Destructor.
Definition: ArgumentContainer.cpp:24
ArgumentContainer::addFlag
Result addFlag(Argument *arg)
Add flag argument.
Definition: ArgumentContainer.cpp:73
HashIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item.
Definition: HashIterator.h:83
Argument::getValue
const String & getValue() const
Retrieve argument option value (if any)
Definition: Argument.cpp:54
Vector< Argument * >
String::equals
virtual bool equals(const String &str) const
Alias for compareTo().
Definition: String.cpp:262
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Argument::getName
const String & getName() const
Retrieve argument name.
Definition: Argument.cpp:44
HashTable::insert
virtual bool insert(const K &key, const V &value)
Inserts the given item to the HashTable.
Definition: HashTable.h:133
ArgumentContainer::getPositionals
const Vector< Argument * > & getPositionals() const
Get positional arguments.
Definition: ArgumentContainer.cpp:39