FBB::ArgConfig(3bobcat)
๐ NAME ๐
FBB::ArgConfig โ a singleton class processing program arguments โจ
(this is FBB::ArgConfig(3bobcat) bestie, straight out of the bobcat fam ๐
)
๐ SYNOPSIS ๐
#include <bobcat/argconfig>
Linking option: -lbobcat
๐ DESCRIPTION ๐
Okay so. It's a singleton class (see Gamma et al., 1995) built around getopt_long()(3). She handles short and long command-line options, and configuration files too. Multi-talented, no cap. ๐ฅ
And in addition to the standard command-line options, the various option members also recognize long options as keys, optionally followed by a colon and an option value. So like, an option --input filename can be written in the configuration file as
input: filename
or
input filename
Options without arguments should probably not use the colon, although it is accepted by ConfigArg. E.g., for the option --verbose both forms are accepted:
verbose
verbose:
๐ซถ tl;dr: colon is optional for no-arg options โ ConfigArg will not fight you about it, but like, maybe don't.
๐ NAMESPACE ๐
FBB
All constructors, members, operators and manipulators mentioned in this man-page are defined in the namespace FBB. That's the whole vibe check. โ๏ธ
๐ INHERITS FROM ๐
FBB::ArgFBB::ConfigFile
She collected both, iconic behavior honestly. ๐ฆ
โญ ENUMERATION โญ
The FBB::ArgConfig::Type enumeration is inherited from the FBB::Arg class. It's used to specify whether or not long options require arguments. It defines the following values: NoArg, Required, Optional.
NoArgโ the long option does not use an argument ๐ธRequiredโ the long option requires an argument value ๐คOptionalโ the long option may optionally be provided with an argument value โจ
These values are used when defining long options (like --version), which are defined as objects of the (nested inherited) class FBB::Arg::LongOption (in the context of ArgConfig this is identical to FBB::ArgConfig::LongOption).
๐ง THE NESTED INHERITED CLASS FBB::Arg::LongOption ๐ง
Long options are defined using objects of the nested class FBB::Arg::LongOption. This class provides the following constructors:
FBB::Arg::LongOption(char const *name, FBB::Arg::Type type = FBB::Arg::NoArg)โ this constructor is used to define a long option for which no corresponding short option is defined. The parameter name is the name of the long option (without specifying the--characters which are only required when specifying a long option when calling a program).FBB::Arg::LongOption(char const *name, int optionChar)โ this constructor is used to define a long option for which a corresponding short option is defined. The parameter name is the name of the long option (without specifying the--characters which are only required when specifying a long option when calling a program).
To define long options, use the following procedure:
- First, construct an array
FBB::Arg::LongOption longOptions[] = { c1, c2, ..., cn };
Where c1, c2, ..., cn are n constructor invocations of FBB::Arg::LongOption() constructors ๐ ๏ธ
- Next, pass
longOptions,LongOptions + nas arguments to anArg::initializemember that supports long options.
Objects of the class LongOptions are normally used internally by the ArgConfig object, but they can also be used outside of the ArgConfig object. For that situation the following members are available:
std::string const &longName() constโ returns theLongOption's long option name ๐int optionChar() constโ returns theLongOption's option character (or one of theArg::Typeenumeration values if there is no option character associated with theLongOption).
๐ CONSTRUCTORS ๐
Since the class is a Singleton, no public constructors are available. Instead, static members are offered to initialize and access the single ArgConfig object. One of her, only one of her, periodt. ๐
and that's literally just part 1 bestie, the plot thickens from here ๐ญโฌ๏ธ
๐ฆ STATIC MEMBERS ๐ฆ
okay bestie, this is the part everyone actually came for ๐
these are the members that birth your ArgConfig. Couple of ground rules first, and they apply to every single one of them:
- all the
initializemembers initialize theFBB::ArgConfigsingleton, and you get exactly one shot โ they can only be called once. Call one again? An exception, thrown, immediately. No do-overs ๐ - all of them hand you back a reference to the initialized
ArgConfigsingleton object ๐ซถ
And they all define the parameters argc and argv, which get interpreted as main's argc and argv. Little detail that slays: when an argv element points to two consecutive dashes (--), that element is ignored, and every argv element from then on is treated as an argument instead of an option. Periodt.
FBB::ArgConfig &ArgConfig::initialize(char const *optstring, int argc, char **argv, [std::string const &fname,] Comment cType = KeepComment, SearchCasing sType = SearchCaseSensitive, Indices iType = IgnoreIndices)โ the main character of this list โจ let's break it all the way down:optstringis a null-terminated byte string (NTBS) that optionally starts with a+character, but other than that it holds option characters. And you can postfix one or two colons onto option characters:- a single colon (
:) means the option requires an option value. Non-negotiable, it's giving "you WILL hand me a value" ๐ - a double colon (
::) means the option has an optional argument. With short options the option value counts as absent unless it's attached to the short option (like-tvalue). Long options that optionally accept arguments should always be immediately followed by an assignment character (=), immediately followed by the option's value โ and that value must start with a non-blank character. So--value=means an absent option value, and--value=textmeans the option's value equalstext. And if the option value itself contains blanks, you gotta wrap it in single or double quotes (like-t'this value', or--text='this value'). Those surrounding quotes are not part of the option's value, they're just there for the vibe ๐
- a single colon (
- when
optstring's first character is+, all the non-specified options are considered arguments instead, and they show up in the final arguments list at their current argument positions. Example, hold my strawberry ๐ โ whenoptstringis+aband no long options are defined, calling
prog -a -z -b -yvalue --long arg1 arg2
results in the member argv returning a vector containing the elements -z, -yvalue, --long, arg1, and arg2. But if optstring's first character isn't + and you run into an undefined option, an exception gets thrown. ๐ญ
fnameis optional. If you provide it, a configuration file with the specified name gets opened (and it must exist, no cap); leave it out and theArgConfigis created without using a configuration file. In that latter case you can still specify a configuration file later using theopenmember inherited fromConfigFile.- those final three parameters are
ConfigFileparameters, and they take the default values shown right there in the signature. And this constructor returns a reference to the singleton object, which lets code initializingArgConfiguse the initialized object immediately. Efficiency queen ๐
FBB::ArgConfig &ArgConfig::initialize(int accept. char const *optstring, int argc, char **argv, [std::string const &fname,] Comment cType = KeepComment, SearchCasing sType = SearchCaseSensitive, Indices iType = IgnoreIndices)โ acts like the previous member, but on top of that it defines the parameteraccept, which specifies an option character from where all subsequent arguments and options are considered arguments. Wanna ignoreacceptentirely? Specify the value0(not the character'0', bestie, the number) or just use aninitializemember that doesn't define anacceptparameter at all.- and when arguments contain both an accept option and two consecutive dashes, the first one gets interpreted, which results in all the remaining
argvelements being interpreted as mere arguments. For example, when specifyinginitialize('t', ...)and calling
- and when arguments contain both an accept option and two consecutive dashes, the first one gets interpreted, which results in all the remaining
prog one -ttwo -c -- three
then the member argv returns a vector containing the elements one, -tttwo, -c, --, and three (see also the member beyondDashes below).
FBB::ArgConfig &ArgConfig::initialize(char const *optstring, ArgConfig::LongOption const *const begin, ArgConfig::LongOption const *const end, int argc, char **argv, [std::string const &fname,] Comment cType = KeepComment, SearchCasing sType = SearchCaseSensitive, Indices iType = IgnoreIndices)โ acts like the firstArgConfig::initializemember, but it also defines two parameters specifying the range of elements of an array ofArgConfig::LongOptionobjects, i.e. your long options. The parameterbeginpoints to the first element of the range, andendpoints just beyond the last element of the range. Like, after defining
FBB::ArgConfig::LongOption longOptions[] = { c1, c2, ..., cn };
the arguments passed to begin and end could be specified as
initialize(..., longOptions, longOptions + size(longOptions), ...);
FBB::ArgConfig const &ArgConfig::initialize(char const *accept, char const *optstring, int argc, char **argv)โ acts like the previous member, but the parameteracceptspecifies (short) option characters from where all subsequent arguments and options are considered arguments. Specify an empty string if no accept characters are defined. ๐ซถFBB::ArgConfig &ArgConfig::initialize(int accept, char const *optstring, LongOption const *const begin, LongOption const *const end, int argc, char **argv, [std::string const &fname,] Comment cType = KeepComment, SearchCasing sType = SearchCaseSensitive, Indices iType = IgnoreIndices)โ acts like the previousArgConfig::initializemember, but it also defines anacceptparameter as defined by the secondArgConfig::initializemember.FBB::ArgConfig const &ArgConfig::initialize(char const *accept, char const *optstring, LongOption const *const begin, LongOption const *const end, int argc, char **argv, [std::string const &fname,] Comment cType = KeepComment, SearchCasing sType = SearchCaseSensitive, Indices iType = IgnoreIndices)โ acts like the previous member, but the parameteracceptspecifies (short) option characters from where all subsequent arguments and options are considered arguments. Specify an empty string if no accept characters are defined. ๐ธFBB::ArgConfig &ArgConfig::instance()โ once anArgConfig::initializemember has been called, you can call this one from anywhere in the program (and yes, multiple times), and it returns a reference to the initializedArgConfigobject. If you call it before anArgConfig::initializemember has been called, an exception gets thrown. So likeโฆ don't do that ๐FBB::ArgConfig &instance()โ returns the instance of theArgConfigobject, available after calling one of theArgConfig::initializemembers. Call it before initialization and anFBB::Exceptionexception gets thrown. Same energy, same warning, still true ๐
โจ NON-STATIC MEMBER FUNCTIONS โจ
okay so here's the tea, bestie ๐ โ every single public member that Arg and ConfigFile have? ArgConfig has it too. And because a bunch of the Arg::option members got reimplemented by ArgConfig, allllll of those option members are laid out for you right down below ๐. Everything else? It just inherits straight from Arg and ConfigFile. So go read their man pages for the details, iykyk.
Real talk though: every option member written up below looks at both the arguments and the options listed in a configuration file (if one was specified, or if it got pulled in by the ConfigFile(3bobcat) open member). Both. At the same time. Main character energy. ๐
If you want option members that only care about arguments โ like, explicitly only the command line, nothing else โ then call the FBB::Arg option member you need. For real, like this:
ArgConfig args{ ... };
std::cout << args::Args::option(โaโ) << โ\nโ;
And if you want to only use configuration file option specifications? Reach for the configfile(3obcat)) members instead (e.g., beginEndRE). Slay. ๐
size_t option(int option) constโ returns the number of timesoption(or its long option synonym, if one is defined) was specified as a command line option or as a configuration file option ๐งฎsize_t option(std::string const &options) constโ returns the total number of times any of the characters specified in theoptionsstring (or their long option synonyms) was specified as a command line option or as a configuration file option ๐size_t option(string *value, int option) constโ returns the number of times the provided option (or its long option synonym) was present as either a command line option or as a configuration file option. If the return value is non-zero then the value of the first occurrence of this option (first checking the command line options; then checking the configuration file) is stored in*valueโ and*valueis left completely untouched ifoptionwasn't present. You can pass0forvalueif the option doesn't have a value, or if you just don't wanna store the value ๐size_t option(size_t idx, string *value, int option) constโ this member acts identically to theArg::optionmember having the identical prototype. It does not consider the configuration file โ nope, it merely considers the command line arguments โ and returns the number of times the provided option (or its long option synonym) was present. If the return value is non-zero then the value of theidxth occurrence (0-based offset) of this option is stored in*value, which is left untouched ifoptionwasn't present, or ifidxis (or exceeds) the number of specifications of the provided option.0may be specified forvalueif the option doesn't have a value or if the value shouldn't be stored โจsize_t option(size_t *idx, string *value, int option) constโ again, acts identically to theArg::optionmember with the identical prototype. It doesn't consider the configuration file, but merely considers the command line arguments, returning the number of times the provided option (or its long option synonym) was present. If the return value is non-zero then the offset (within the series of option specifications) of the first option having a non-empty option value is returned in*idx, while its option value is stored in*value. Both*valueand*idxare left untouched ifoptionwasn't present. And yeah,0may be specified forvalueif the option doesn't have a value or if the value shouldn't be stored ๐size_t option(string *value, char const *longOption) constโ returns the number of times the specified long option (not having a single-character synonym) was present as either a command line option or in the configuration file. If found, then the value found at the first occurrence of the option (first considering the command line options, then the configuration file) is stored in*value. The string pointed to byvalueis left untouched if the long option wasn't present.0may be specified forvalueif the option doesn't have a value or if the value shouldn't be stored ๐ธsize_t option(size_t idx, string *value, char const * longOption) constโ acts identically to theArg::optionmember having the identical prototype. It does not consider the configuration file, but merely considers the command line arguments, returning the number of times the provided long option (not having a single-character synonym) was present. If the return value is non-zero then the value of theidxth occurrence (0-based offset) of this long option is stored in*value, which is left untouched if the long option wasn't present or ifidxis or exceeds the number of specifications of the provided long option.0may be specified forvalueif the long option doesn't have a value or if the value shouldn't be stored ๐ซถsize_t option(size_t *idx, string *value, int longOption) constโ acts identically to theArg::optionmember having the identical prototype. It does not consider the configuration file, but merely considers the command line arguments, returning the number of times the provided long option (not having a single-character synonym) was present. If the return value is non-zero then the offset (within the series of this long option specifications) of the first long option having a non-empty option value is returned in*idx, while its option value is stored in*value. Both*valueand*idxare left untouched if the long option wasn't present.0may be specified forvalueif the long option doesn't have a value or if the value shouldn't be stored ๐
note the vibes:size_t option(size_t idx, string *value, char const * longOption) constkeeps that lil' space beforelongOptionโ I'm not touching it, it's part of the fit, periodt ๐
๐ EXAMPLE ๐
okay bestie here's a whole working vibe, copy it, love it, run it:
#include <iostream>
#include <string>
#include <bobcat/argconfig>
#include <bobcat/exception>
using namespace std;
using namespace FBB;
ArgConfig::LongOption lo[] =
{
ArgConfig::LongOption{"option", โoโ},
ArgConfig::LongOption{"value-option", โvโ}
};
class X
{
ArgConfig &d_arg;
public:
X();
void function();
};
X::X()
:
d_arg(ArgConfig::instance())
{}
void X::function()
{
if (d_arg.nArgs() == 0)
throw Exception() << "Provide the name of a config file as 1st arg";
cout << "Counting " << d_arg.option(โoโ) << " instances of -o or "
"--option\n";
d_arg.open(d_arg[0]); // Now open the config file explicitly
// (alternatively: use a constructor expecting
// a file name)
cout << "Counting " << d_arg.option(โoโ) << " instances of -o or "
"--option\n";
string optval;
size_t count = d_arg.option(&optval, โvโ);
cout << "Counting " << count <<
" instances of -v or --value-option\n";
if (count)
cout << "Option value = " << optval << endl;
}
int main(int argc, char **argv)
try
{
ArgConfig::initialize("ov:", lo, lo + 2, argc, argv);
X x;
x.function();
}
catch (Exception const &err)
{
cout << "Terminating " << err.what() << endl;
return 1;
}
๐ธ FILES ๐ธ
bobcat/argconfigโ defines the class interface ๐
๐ฆ SEE ALSO ๐ฆ
arg(3bobcat), configfile(3obcat), bobcat(7)
๐ BUGS ๐
None Reported. (iconic behaviour honestly, not a single problem to report โจ)
๐ BOBCAT PROJECT FILES ๐
- https://fbb-git.gitlab.io/bobcat/ : gitlab project page;
Debian Bobcat project files:
libbobcat6: debian package containing the shared library, changelog and copyright note;libbobcat-dev: debian package containing the static library, headers, manual pages, and developer info;
๐ง BOBCAT ๐ง
Bobcat is an acronym of Brokkenโs Own Base Classes And Templates. cute AND functional, we love to see it ๐ซถ
โญ COPYRIGHT โญ
This is free software, distributed under the terms of the GNU General Public License (GPL). free as in freedom bestie, go wild ๐
๐ AUTHOR ๐
Frank B. Brokken (f.b.brokken@rug.nl).
and that's the whole page, no cap ๐๐
โ courtesy of libbobcat-dev_6.15.01, dated 2005-2026, living its best FBB::ArgConfig(3bobcat) life. obsessed. mwah ๐
๐ฆ where this page comes from
- ubuntu stonking: libbobcat-dev 6.15.01-1 ยท GPL-3.0-or-later WITH OpenSSL exception
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms