Loading...
Searching...
No Matches
by::flag Class Referenceabstract

Base class for command-line flag handling. More...

#include <flag.hpp>

Inheritance diagram for by::flag:
by::instance by::typeProvidable by::clonable by::bufferSrcFlag by::dirFlag by::fileFlag by::helpFlag by::logStructureFlag by::verFlag by::verboseFlag

Public Types

enum  res { MATCH , EXIT_PROGRAM , NOT_MATCH }
 

Public Member Functions

virtual const nchargetName () const =0
 
virtual const nchargetDescription () const =0
 
virtual res take (interpreter &ip, starter &s, cli &c, flagArgs &a, errReport &rpt) const
 
nbool canTake (const std::string &pattern) const
 
- Public Member Functions inherited from by::instance
 instance (id newId)
 
 instance (const me &rhs)
 
me & operator= (const me &rhs)=default
 
void * operator new (size_t sz) noexcept
 
void operator delete (void *pt, size_t sz) noexcept
 
virtual id getId () const
 
virtual nbool isHeap () const
 Checks if this instance is allocated on the heap.
 
const lifegetBindTag () const
 Returns a pointer to the associated life object for reference counting.
 
- Public Member Functions inherited from by::typeProvidable
nbool operator== (const me &rhs) const
 
nbool operator!= (const me &rhs) const
 
virtual const typegetType () const =0
 
nbool isSub (const type &it) const
 
nbool isSub (const type *it) const BY_SIDE_FUNC(isSub)
 
nbool isSub (const me &it) const
 
nbool isSub (const me *it) const BY_SIDE_FUNC(isSub)
 
nbool isSuper (const type &it) const
 
nbool isSuper (const type *it) const BY_SIDE_FUNC(isSuper)
 
nbool isSuper (const me &it) const
 
nbool isSuper (const me *it) const BY_SIDE_FUNC(isSuper)
 
template<typename T >
nint isSub () const
 
template<typename T >
nint isSuper () const
 
template<typename T >
T * cast ()
 
template<typename T >
const T * cast () const BY_CONST_FUNC(cast< T >()) virtual void *cast(const type &to)
 Safe cast to target type using type hierarchy information.
 
const void * cast (const type &to) const BY_CONST_FUNC(cast(to)) void *cast(const type *it) BY_SIDE_FUNC(cast)
 
- Public Member Functions inherited from by::clonable
virtual me * clone () const =0
 
virtual me * cloneDeep () const
 
virtual void onCloneDeep (const me &from)
 

Protected Member Functions

virtual ncnt getArgCount () const
 
void _delArgs (flagArgs &a, ncnt deleteOptionCnt) const
 Deletes arguments from the flagArgs list.
 
virtual const strings & _getRegExpr () const =0
 
virtual res _onTake (const flagArgs &tray, cli &c, interpreter &ip, starter &s, errReport &rpt) const =0
 
- Protected Member Functions inherited from by::instance
nbool _setId (id new1)
 

Additional Inherited Members

- Static Public Member Functions inherited from by::instance
static vaultgetVault ()
 
- Static Protected Member Functions inherited from by::instance
static instancer_getMgr ()
 

Detailed Description

Base class for command-line flag handling.

Handles flags commonly seen in shell-based programs. Note this class doesn't represent a flag like --version, but rather handles whether such flags exist and what actions to take.

Flag descriptions

Each flag defines not only its name but also a description of its functionality. These are used by helpFlag.

Regex pattern matching

Each flag class has a defined pattern to search for. For example, verFlag searches for the --version string in program arguments. If found, it retrieves version info from buildFeature and outputs it. This behavior works by calling flag::take(), which uses the regex defined in each derived class's _getRegExpr() to find the desired pattern. Since regex is used, flag order is ignored.

Multiple patterns can be defined in regex:

const strings& me::_getRegExpr() const {
static strings inner{"^\\-S$", "^\\--show-structure$"};
return inner;
}
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34

Matches either ^\-S$ or ^\--show-structure$.

Consuming multiple flag arguments

Consider verFlag: when --version exists in arguments, it outputs version. After operating, it removes --version from flagArgs to prevent verFlag from running again.

Now consider bufferSrcFlag, used like: --script "main() void: print(\"wow!\")"

Breaking this down: --script indicates flag start, followed by space and "<your code>" which is the frontend code body argument to execute. Some flags consume not just the matched pattern but additional arguments. Each derived flag class can override getArgCount() to specify how many additional arguments to extract.

Terminating flags

bufferSrcFlag needs preliminary work before program start. Conversely, verFlag immediately outputs version and terminates without interpretation once matched (as most programs do). To terminate immediately after matching, override _onTake() to return EXIT_PROGRAM. For continued operation like bufferSrcFlag, return MATCH.

Member Function Documentation

◆ _delArgs()

void by::flag::_delArgs ( flagArgs & a,
ncnt deleteOptionCnt ) const
protected

Deletes arguments from the flagArgs list.

This method is used internally to remove arguments that have been consumed by this flag, preventing them from being processed again by other flags.

Parameters
aThe flagArgs instance from which arguments are to be deleted.
deleteOptionCnta count indicating how much arguments to delete.

◆ canTake()

nbool by::flag::canTake ( const std::string & pattern) const
Parameters
patternit's used to determine whether a pattern for a flag or not. it usually starts with one or two hyphens.

◆ getArgCount()

virtual ncnt by::flag::getArgCount ( ) const
protectedvirtual

when this flag matched to one of args, the value of returning this func will be count of continuous argument of this flag.

Reimplemented in by::bufferSrcFlag.

◆ take()

virtual res by::flag::take ( interpreter & ip,
starter & s,
cli & c,
flagArgs & a,
errReport & rpt ) const
virtual
Returns
false if flag wants to exit program.

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