FreeNOS
|
Generic command-line argument parser. More...
#include <ArgumentParser.h>
Public Types | |
enum | Result { Success, InvalidArgument, NotFound, AlreadyExists } |
Result codes. More... | |
Public Member Functions | |
ArgumentParser () | |
Constructor. More... | |
virtual | ~ArgumentParser () |
Destructor. More... | |
String | getUsage () const |
Get program usage. More... | |
const String & | name () const |
Retrieve program name. More... | |
void | setName (const char *name) |
Set program name. More... | |
void | setDescription (const String &desc) |
Set program description. More... | |
Result | registerFlag (char arg, const char *name, const char *description) |
Register a flag Argument. More... | |
Result | registerPositional (const char *name, const char *description, Size count=1) |
Register a positional argument. More... | |
Result | parse (int argc, char **argv, ArgumentContainer &output) |
Parse input arguments. More... | |
Private Attributes | |
HashTable< String, Argument * > | m_flags |
Contains all registered flag arguments by name. More... | |
HashTable< String, Argument * > | m_flagsId |
Contains all registered flag arguments by single character identifier. More... | |
Vector< Argument * > | m_positionals |
Contains all registered positional arguments. More... | |
String | m_name |
Program name. More... | |
String | m_description |
Program description. More... | |
Generic command-line argument parser.
Definition at line 40 of file ArgumentParser.h.
Result codes.
Enumerator | |
---|---|
Success | |
InvalidArgument | |
NotFound | |
AlreadyExists |
Definition at line 47 of file ArgumentParser.h.
ArgumentParser::ArgumentParser | ( | ) |
Constructor.
Definition at line 21 of file ArgumentParser.cpp.
References m_description, and m_name.
|
virtual |
Destructor.
Definition at line 27 of file ArgumentParser.cpp.
References Vector< T >::count(), HashIterator< K, V >::hasCurrent(), m_flags, and m_positionals.
String ArgumentParser::getUsage | ( | ) | const |
Get program usage.
Definition at line 42 of file ArgumentParser.cpp.
References Vector< T >::count(), HashTable< K, V >::count(), ConstHashIterator< K, V >::hasCurrent(), m_description, m_flags, m_name, m_positionals, and usage().
Referenced by Application::usage().
const String & ArgumentParser::name | ( | ) | const |
Retrieve program name.
Definition at line 85 of file ArgumentParser.cpp.
References m_name.
Referenced by Cat::cat(), BootImageCreate::exec(), BootImageCreate::readBootSymbols(), registerFlag(), registerPositional(), and setName().
ArgumentParser::Result ArgumentParser::parse | ( | int | argc, |
char ** | argv, | ||
ArgumentContainer & | output | ||
) |
Parse input arguments.
This function parses the given arguments and fills the argument container with parsed arguments.
argc | Input argument count. |
argv | Input argument values. |
output | Filled with parsed arguments on output. |
Definition at line 136 of file ArgumentParser.cpp.
References ArgumentContainer::addFlag(), ArgumentContainer::addPositional(), Vector< T >::count(), HashTable< K, V >::get(), ArgumentContainer::getPositionals(), InvalidArgument, List< T >::last(), String::length(), m_flags, m_flagsId, m_positionals, NotFound, Argument::setValue(), String::split(), String::substring(), and Success.
Referenced by Application::run().
ArgumentParser::Result ArgumentParser::registerFlag | ( | char | arg, |
const char * | name, | ||
const char * | description | ||
) |
Register a flag Argument.
Definition at line 100 of file ArgumentParser.cpp.
References HashTable< K, V >::insert(), m_flags, m_flagsId, name(), Argument::setDescription(), Argument::setIdentifier(), and Success.
Referenced by Application::Application(), BootImageCreate::BootImageCreate(), Echo::Echo(), Init::Init(), ListFiles::ListFiles(), Mount::Mount(), NetCat::NetCat(), NetPing::NetPing(), Shutdown::Shutdown(), SievePrime::SievePrime(), SysControl::SysControl(), and UnixName::UnixName().
ArgumentParser::Result ArgumentParser::registerPositional | ( | const char * | name, |
const char * | description, | ||
Size | count = 1 |
||
) |
Register a positional argument.
name | Argument name |
description | Argument single line description |
count | Maximum number to set for this positional argument |
Definition at line 119 of file ArgumentParser.cpp.
References AlreadyExists, Vector< T >::at(), Vector< T >::count(), Argument::getCount(), Vector< T >::insert(), m_positionals, name(), Argument::setCount(), Argument::setDescription(), and Success.
Referenced by BootImageCreate::BootImageCreate(), Cat::Cat(), CreateFile::CreateFile(), Decompress::Decompress(), DhcpClient::DhcpClient(), Echo::Echo(), FileStatus::FileStatus(), ListFiles::ListFiles(), Login::Login(), MakeNode::MakeNode(), MpiProxy::MpiProxy(), NetCat::NetCat(), NetCtl::NetCtl(), NetPing::NetPing(), NetSend::NetSend(), Remove::Remove(), Shell::Shell(), SievePrime::SievePrime(), Sleep::Sleep(), and SysControl::SysControl().
void ArgumentParser::setDescription | ( | const String & | desc | ) |
Set program description.
desc | Program description string. |
Definition at line 95 of file ArgumentParser.cpp.
References m_description.
Referenced by BenchMark::BenchMark(), BootImageCreate::BootImageCreate(), Cat::Cat(), CreateFile::CreateFile(), Decompress::Decompress(), DhcpClient::DhcpClient(), Echo::Echo(), FileStatus::FileStatus(), Hostname::Hostname(), Init::Init(), ListFiles::ListFiles(), Login::Login(), MakeNode::MakeNode(), Mount::Mount(), MpiPing::MpiPing(), MpiPrime::MpiPrime(), MpiProxy::MpiProxy(), NetCat::NetCat(), NetCtl::NetCtl(), NetPing::NetPing(), NetSend::NetSend(), ProcessList::ProcessList(), Remove::Remove(), Shell::Shell(), Shutdown::Shutdown(), SievePrime::SievePrime(), Sleep::Sleep(), SysControl::SysControl(), SysInfo::SysInfo(), and UnixName::UnixName().
void ArgumentParser::setName | ( | const char * | name | ) |
Set program name.
name | Program name string. |
Definition at line 90 of file ArgumentParser.cpp.
References m_name, and name().
Referenced by Application::run().
|
private |
Program description.
Definition at line 152 of file ArgumentParser.h.
Referenced by ArgumentParser(), getUsage(), and setDescription().
Contains all registered flag arguments by name.
Definition at line 140 of file ArgumentParser.h.
Referenced by getUsage(), parse(), registerFlag(), and ~ArgumentParser().
Contains all registered flag arguments by single character identifier.
Definition at line 143 of file ArgumentParser.h.
Referenced by parse(), and registerFlag().
|
private |
Program name.
Definition at line 149 of file ArgumentParser.h.
Referenced by ArgumentParser(), getUsage(), name(), and setName().
Contains all registered positional arguments.
Definition at line 146 of file ArgumentParser.h.
Referenced by getUsage(), parse(), registerPositional(), and ~ArgumentParser().