FreeNOS
Public Types | Public Member Functions | Private Attributes
ArgumentParser Class Reference

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 Stringname () 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...
 

Detailed Description

Generic command-line argument parser.

Definition at line 40 of file ArgumentParser.h.

Member Enumeration Documentation

◆ Result

Result codes.

Enumerator
Success 
InvalidArgument 
NotFound 
AlreadyExists 

Definition at line 47 of file ArgumentParser.h.

Constructor & Destructor Documentation

◆ ArgumentParser()

ArgumentParser::ArgumentParser ( )

Constructor.

Definition at line 21 of file ArgumentParser.cpp.

References m_description, and m_name.

◆ ~ArgumentParser()

ArgumentParser::~ArgumentParser ( )
virtual

Destructor.

Definition at line 27 of file ArgumentParser.cpp.

References Vector< T >::count(), HashIterator< K, V >::hasCurrent(), m_flags, and m_positionals.

Member Function Documentation

◆ getUsage()

String ArgumentParser::getUsage ( ) const

Get program usage.

Returns
Program usage string reference.

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().

◆ name()

const String & ArgumentParser::name ( ) const

Retrieve program name.

Returns
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().

◆ parse()

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.

Parameters
argcInput argument count.
argvInput argument values.
outputFilled with parsed arguments on output.
Returns
Result code.

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().

◆ registerFlag()

ArgumentParser::Result ArgumentParser::registerFlag ( char  arg,
const char *  name,
const char *  description 
)

◆ registerPositional()

ArgumentParser::Result ArgumentParser::registerPositional ( const char *  name,
const char *  description,
Size  count = 1 
)

◆ setDescription()

void ArgumentParser::setDescription ( const String desc)

◆ setName()

void ArgumentParser::setName ( const char *  name)

Set program name.

Parameters
nameProgram name string.

Definition at line 90 of file ArgumentParser.cpp.

References m_name, and name().

Referenced by Application::run().

Field Documentation

◆ m_description

String ArgumentParser::m_description
private

Program description.

Definition at line 152 of file ArgumentParser.h.

Referenced by ArgumentParser(), getUsage(), and setDescription().

◆ m_flags

HashTable<String, Argument *> ArgumentParser::m_flags
private

Contains all registered flag arguments by name.

Definition at line 140 of file ArgumentParser.h.

Referenced by getUsage(), parse(), registerFlag(), and ~ArgumentParser().

◆ m_flagsId

HashTable<String, Argument *> ArgumentParser::m_flagsId
private

Contains all registered flag arguments by single character identifier.

Definition at line 143 of file ArgumentParser.h.

Referenced by parse(), and registerFlag().

◆ m_name

String ArgumentParser::m_name
private

Program name.

Definition at line 149 of file ArgumentParser.h.

Referenced by ArgumentParser(), getUsage(), name(), and setName().

◆ m_positionals

Vector<Argument *> ArgumentParser::m_positionals
private

Contains all registered positional arguments.

Definition at line 146 of file ArgumentParser.h.

Referenced by getUsage(), parse(), registerPositional(), and ~ArgumentParser().


The documentation for this class was generated from the following files: