Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
LUPI::ArgumentBase Class Referenceabstract

#include <LUPI.hpp>

Inheritance diagram for LUPI::ArgumentBase:

Public Member Functions

 ArgumentBase (ArgumentType a_argumentType, std::string const &a_name, std::string const &a_descriptor, int a_minimumNeeded, int a_maximumNeeded)
virtual ~ArgumentBase ()=0
ArgumentType argumentType () const
std::string const & name () const
std::vector< std::string > const & names ()
bool hasName (std::string const &a_name) const
std::string const & descriptor () const
int minimumNeeded () const
int maximumNeeded () const
std::size_t counts () const
virtual std::string const & value (std::size_t a_index=0) const
std::vector< std::string > const & values () const
virtual bool isOptionalArgument () const
virtual bool requiresAValue () const
virtual int parse (ArgumentParser const &a_argumentParser, int a_index, int a_argc, char **a_argv)
std::string usage (bool a_requiredOption) const
void printStatus (std::string a_indent) const

Friends

void ArgumentParser::addAlias (std::string const &a_name, std::string const &a_alias)

Detailed Description

Base class for argument and option sub-classes.

Definition at line 186 of file LUPI.hpp.

Constructor & Destructor Documentation

◆ ArgumentBase()

LUPI::ArgumentBase::ArgumentBase ( ArgumentType a_argumentType,
std::string const & a_name,
std::string const & a_descriptor,
int a_minimumNeeded,
int a_maximumNeeded )

ArgumentBase constructor.

Parameters
a_argumentType[in] The type of argument to create.
a_name[in] The name of the argument.
a_descriptor[in] The string printed with arugment's help.
a_minimumNeeded[in] The minimum number of times the argument must be entered.
a_maximumNeeded[in] The maximum number of times the argument can be entered.

Definition at line 34 of file LUPI_argumentParser.cc.

34 :
35 m_argumentType( a_argumentType ),
36 m_names( ),
37 m_descriptor( a_descriptor ),
38 m_minimumNeeded( a_minimumNeeded ),
39 m_maximumNeeded( a_maximumNeeded ),
40 m_counts( 0 ) {
41
42 if( m_minimumNeeded < 0 ) throw std::runtime_error( "ERROR 1000 in ArgumentBase::ArgumentBase: m_minimumNeeded must not be negative." );
43 if( m_maximumNeeded > -1 ) {
44 if( m_minimumNeeded > m_maximumNeeded )
45 throw std::runtime_error( "ERROR 1010 in ArgumentBase::ArgumentBase: for argument '" + a_name + "' m_maximumNeeded less than m_minimumNeeded." );
46 }
47
48 if( m_argumentType == ArgumentType::Positional ) {
49 if( a_name[0] == '-' )
50 throw std::runtime_error( "ERROR 1020 in ArgumentBase::ArgumentBase: positional argument name '" + a_name + "' cannot start with a '-'." );
51 }
52
53 addAlias( a_name );
54}

Referenced by LUPI::OptionAppend::OptionAppend(), LUPI::OptionBoolean::OptionBoolean(), LUPI::OptionCounter::OptionCounter(), LUPI::OptionStore::OptionStore(), and LUPI::Positional::Positional().

◆ ~ArgumentBase()

LUPI::ArgumentBase::~ArgumentBase ( )
pure virtual

ArgumentBase destructor.

Definition at line 60 of file LUPI_argumentParser.cc.

60 {
61
62}

Member Function Documentation

◆ argumentType()

ArgumentType LUPI::ArgumentBase::argumentType ( ) const
inline

Definition at line 207 of file LUPI.hpp.

207{ return( m_argumentType ); }

◆ counts()

std::size_t LUPI::ArgumentBase::counts ( ) const
inline

Definition at line 214 of file LUPI.hpp.

214{ return( m_counts ); }

Referenced by parse(), LUPI::OptionBoolean::printStatus2(), LUPI::OptionCounter::printStatus2(), and LUPI::OptionStore::printStatus3().

◆ descriptor()

std::string const & LUPI::ArgumentBase::descriptor ( ) const
inline

Definition at line 211 of file LUPI.hpp.

211{ return( m_descriptor ); }

◆ hasName()

bool LUPI::ArgumentBase::hasName ( std::string const & a_name) const

Returns true if a_name is one of the names for this and false otherwise.

Parameters
a_name[in] The name to search for.
Returns
Returns true if a_name is a match for one of the names for this and false otherwise.

Definition at line 72 of file LUPI_argumentParser.cc.

72 {
73
74 for( auto iter = m_names.begin( ); iter != m_names.end( ); ++iter ) {
75 if( a_name == *iter ) return( true );
76 }
77 return( false );
78}

◆ isOptionalArgument()

virtual bool LUPI::ArgumentBase::isOptionalArgument ( ) const
inlinevirtual

Reimplemented in LUPI::Positional.

Definition at line 218 of file LUPI.hpp.

218{ return( true ); }

Referenced by usage().

◆ maximumNeeded()

int LUPI::ArgumentBase::maximumNeeded ( ) const
inline

Definition at line 213 of file LUPI.hpp.

213{ return( m_maximumNeeded ); }

Referenced by parse().

◆ minimumNeeded()

int LUPI::ArgumentBase::minimumNeeded ( ) const
inline

Definition at line 212 of file LUPI.hpp.

212{ return( m_minimumNeeded ); }

◆ name()

std::string const & LUPI::ArgumentBase::name ( ) const
inline

Definition at line 208 of file LUPI.hpp.

208{ return( m_names[0] ); }

Referenced by LUPI::ArgumentParser::addAlias(), parse(), printStatus(), usage(), and value().

◆ names()

std::vector< std::string > const & LUPI::ArgumentBase::names ( )
inline

Definition at line 209 of file LUPI.hpp.

209{ return( m_names ); }

◆ parse()

int LUPI::ArgumentBase::parse ( ArgumentParser const & a_argumentParser,
int a_index,
int a_argc,
char ** a_argv )
virtual

Counts each time a specific argument is found.

Parameters
a_index[in] The index of the current command argument in a_argv.
a_argc[in] The number of command arguments.
a_argv[in] The list of command arguments.
Returns
The value of a_index + 1.

Definition at line 127 of file LUPI_argumentParser.cc.

127 {
128
129 ++m_counts;
130 if( m_argumentType != ArgumentType::Positional ) ++a_index;
131
132 if( ( m_argumentType == ArgumentType::Store ) || ( m_argumentType == ArgumentType::Append ) || ( m_argumentType == ArgumentType::Positional ) ) {
133 int maximumNeeded1 = maximumNeeded( );
134 if( m_argumentType == ArgumentType::Positional ) {
135 if( maximumNeeded1 < 0 ) maximumNeeded1 = a_argc; }
136 else {
137 if( ( maximumNeeded1 < static_cast<int>( counts( ) ) ) && ( maximumNeeded1 > -1 ) )
138 throw std::runtime_error( "ERROR 1220 in ArgumentBase::parse: too many values for optional argument " + name( ) + " entered." );
139 maximumNeeded1 = 1;
140 }
141
142 for( int index = 0; index < maximumNeeded1; ++index ) {
143 if( a_index == a_argc ) {
144 if( m_argumentType == ArgumentType::Positional ) break;
145 throw std::runtime_error( "ERROR 1200 in ArgumentBase::parse: missing value for argument " + name( ) + "." );
146 }
147 if( ( m_argumentType == ArgumentType::Positional ) && a_argumentParser.isOptionalArgument( a_argv[a_index] ) ) break;
148
149 m_values.push_back( a_argv[a_index] );
150 if( index > 0 ) ++m_counts;
151 ++a_index;
152 }
153 }
154
155 return( a_index );
156}
std::size_t counts() const
Definition LUPI.hpp:214
std::string const & name() const
Definition LUPI.hpp:208
int maximumNeeded() const
Definition LUPI.hpp:213

◆ printStatus()

void LUPI::ArgumentBase::printStatus ( std::string a_indent) const

Prints generic information about the status of this.

Parameters
a_indent[in] The amount of indentation to start the first line with.
Returns
The value of a_index + 1.

Definition at line 203 of file LUPI_argumentParser.cc.

203 {
204
205 std::string name1 = name( );
206 if( name1.size( ) < 32 ) name1.resize( 32, ' ' );
207
208 std::cout << a_indent << name1 << ": number entered " << std::to_string( m_counts )
209 << "; number needed (" << std::to_string( m_minimumNeeded ) << "," << std::to_string( m_maximumNeeded ) << ")"
210 << printStatus2( ) << std::endl;
211 printStatus3( a_indent + " " );
212}
std::string to_string(G4FermiAtomicMass mass)

◆ requiresAValue()

virtual bool LUPI::ArgumentBase::requiresAValue ( ) const
inlinevirtual

Reimplemented in LUPI::OptionAppend, LUPI::OptionStore, and LUPI::Positional.

Definition at line 219 of file LUPI.hpp.

219{ return( false ); }

Referenced by usage().

◆ usage()

std::string LUPI::ArgumentBase::usage ( bool a_requiredOption) const

Returns the usage string for this argument.

Parameters
a_requiredOption[in] The index of the current command argument in a_argv.
Returns
The value of a_index + 1.

Definition at line 166 of file LUPI_argumentParser.cc.

166 {
167
168 std::string usageString;
169
170 if( isOptionalArgument( ) ) {
171 if( a_requiredOption ) {
172 if( m_minimumNeeded != 0 ) return( usageString ); }
173 else {
174 if( m_minimumNeeded == 0 ) return( usageString );
175 }
176 }
177 usageString += " ";
178
179 std::string value;
180 if( isOptionalArgument( ) && requiresAValue( ) ) value = " VALUE";
181 if( isOptionalArgument( ) && ( m_minimumNeeded == 0 ) ) {
182 usageString += "[" + name( ) + value + "]"; }
183 else {
184 usageString += name( ) + value;
185 }
186
187 if( !isOptionalArgument( ) ) {
188 if( ( m_minimumNeeded != 1 ) || ( m_maximumNeeded != 1 ) )
189 usageString += "[" + std::to_string( m_minimumNeeded ) + "," + std::to_string( m_maximumNeeded ) + "]";
190 }
191
192 return( usageString );
193}
virtual bool requiresAValue() const
Definition LUPI.hpp:219
virtual bool isOptionalArgument() const
Definition LUPI.hpp:218
virtual std::string const & value(std::size_t a_index=0) const

◆ value()

std::string const & LUPI::ArgumentBase::value ( std::size_t a_index = 0) const
virtual

Returns the value at index a_index. If a_index exceeds the number of values entered, a throw is executed.

Parameters
a_index[in] The 0-based index into the m_values std::vector whose content is returned.
Returns
Returns the value entered at index a_index.

Reimplemented in LUPI::OptionStore.

Definition at line 88 of file LUPI_argumentParser.cc.

88 {
89
90 if( ( m_argumentType == ArgumentType::True ) || ( m_argumentType == ArgumentType::False ) || ( m_argumentType == ArgumentType::Count ) )
91 throw Exception( "Argument type for " + name( ) + " does not support calling value() method." );
92
93 if( a_index >= m_values.size( ) ) throw Exception( "Index = " + std::to_string( a_index ) + " out-of-bounds for argument \"" + name( ) + "\"." );
94
95 return( m_values[a_index] );
96}

Referenced by usage(), and LUPI::OptionStore::value().

◆ values()

std::vector< std::string > const & LUPI::ArgumentBase::values ( ) const
inline

Definition at line 217 of file LUPI.hpp.

217{ return( m_values ); }

Referenced by LUPI::OptionAppend::printStatus3(), LUPI::Positional::printStatus3(), and LUPI::OptionStore::value().

◆ ArgumentParser::addAlias

void ArgumentParser::addAlias ( std::string const & a_name,
std::string const & a_alias )
friend

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