3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_UTILS_COMMANDLINE
7#define ELEMENTA_UTILS_COMMANDLINE
13#include "elementa/base/serial_channels.h"
79 using Base = std::vector<elementa::base::Strings>;
86 void check(elementa::base::Strings::size_type minnumelems = 0)
const;
89 std::string
to_string(
const std::string & preffix =
"",
90 const std::string & sep =
" ")
const;
97 class Vector:
public std::vector<CLData>
101 using Base = std::vector<CLData>;
105 Base::const_iterator find(
const std::string &
shortname)
const;
106 std::string to_string(
const std::string & preff,
107 const std::string & sep =
" ")
const;
116 class Vector:
public std::vector<CLOption>
121 using Base = std::vector<CLOption>;
127 Base::const_iterator
find(
const std::string & n,
128 bool shortn =
true)
const;
148 std::string{
"Command line error: "} +
161 "Invalid option specification",details)}
173 "Invalid value for option",details)}
185 "Invalid group of strings",details)}
234 {
return(!
operator==(oo)); }
255 "Invalid preffix in a CL argument",
268 "Unknown option",details)}
280 "Mandatory option not found",
293 "Mandatory value not found",details)}
305 "Option must be alone",details)}
317 "Option dependencies are not met",
330 "Incompatible options",details)}
352 const std::string preff =
"-");
360 {
return(nameofprog_); }
363 const std::string &
preffix(
void)
const noexcept
375 std::string
help(
const std::string & shortname =
"",
376 unsigned charwidth = 80);
391 bool present(
const std::string & shortn, std::string & val)
const;
398 bool present(
const std::string & shortn)
const;
413 {
return(argvdata_.to_string(preff_,
", ")); }
418 const std::string nameofprog_;
421 const std::string preff_;
424 std::string helpOpt(
const CLOption & opt,
bool withdepinfo,
size_t depth,
426 void recursivehelp(std::vector<CLOption::Vector::size_type> parents,
427 std::string & h,
unsigned charwidth,
size_t depth = 0);
428 std::string formHelp(
bool hierarchized,
unsigned charwidth);
429 std::string preffixedOpt(
const CLOption & opt,
bool shortn =
true);
430 std::string getoptfromtxt(
const std::string & o)
const;
Base class for all errors that can be thrown about CL options.
A number of groups of strings.
std::string to_string(const std::string &preffix="", const std::string &sep=" ") const
Return a string with all the groups as sets.
void check(elementa::base::Strings::size_type minnumelems=0) const
Check that no group of strings intersects other or has repeated strs.
A number of options as they are found in a command line.
Base::const_iterator find(const std::string &n, bool shortn=true) const
Find an option through its name N, either SHORTN or not.
bool therearedeps(void) const
Return TRUE if any option in the vector has dependencies.
void checkSpec(void) const
Check that the vector of options is valid.
std::vector< CLOption > Base
Shortcut.
ELE_CLASS_ENUM(ValKind, kFreeText, kNatural, kPositive, kInteger, kNumber, kEnum, kNone)
Kind of values that the option may have.
std::string shortname
Shortname of the option.
const std::string & progName(void) const noexcept
Return a reference to the program name.
StringGroups dependencies
Dependencies.
bool operator!=(const CLOption &oo) const
The opposite to operator==.
ELE_CLASS_ENUM(PresenceKind, kMandatory, kOptional, kAlone)
Kind of presence an option may have.
bool present(const std::string &shortn, std::string &val) const
If that option is present after processing, return TRUE and fill VAL.
PresenceKind presence
Presence of the option.
void checkValue(const std::string &v) const
Check that the value is valid for the option. Throw InvOptValue if not.
bool dependsOn(const CLOption &other) const
Return TRUE if this option depends on OTHER.
std::string help
Description of the option for showing help.
bool present(const std::string &shortn) const
If that option is present after processing, return TRUE.
void checkSpec(void) const
Check that the option specification is valid. Throw InvOptSpec if not.
std::string helpValues(void) const
Return a string with the help about the values of the option.
ValKind valkind
Kind of values it may have.
const CLOption & optDef(const std::string &shortn) const
Return a reference to the definition of the given option.
std::string value
Value given to the option, or "" if none.
elementa::base::Strings depOpts(const std::string &shortn) const
Return all the options dependent on SHORTN, or empty if none.
elementa::base::Strings values
Only used if valkind==ValKind::kEnum.
std::string dflt
Default value. If empty, opt cannot have empty value.
std::string help(const std::string &shortname="", unsigned charwidth=80)
Return a string with formatted help.
const std::string & preffix(void) const noexcept
Return a reference to the preffix.
elementa::base::Strings::size_type whichIsDflt(void) const
If it is an enum, return the index in VALUES of DFLT.
CLOptionManager(const std::string &progname, const CLOption::Vector &opts, const CLOption::StringGroups &incomps, const std::string preff="-")
Constructor from the options.
void process(int argc, char *argv[])
Read and process the command line parameters according to the options.
std::string longname
Long alternative name (may be empty).
std::string processed_tostring(void) const
Return the content of the command line after processing, as a text.
std::string shortname
Short option name; its unique identifier.
virtual ~CLOptionManager(void)=default
Just to assure polymorphic deletions.
bool mandatoryval
TRUE if a value must be provided for the option.
bool operator==(const CLOption &oo) const
Return TRUE if both options have the same shortname.
Specification for an option in the command line.
A class in charge of parsing and managing command line options.
Data provided in the command line about an option.
const char * explanation(void) const noexcept
Return the explanation only. It will live as long as this exception.
Base class for all errors / exceptions in Elementa. Just derive from it.
#define ELE_CLASS_EXCOVERRIDE(C)
Shortening macro that must be used inside classes derived from Exc.
std::string concatWithMiddle(const std::string &s1, const std::string &s2, const std::string &m=". ")
Concatenate two strings putting a middle one only if the second is not empty.