Gromacs  2024.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Classes | Public Member Functions
gmx::CommandLineParser Class Reference

#include <gromacs/commandline/cmdlineparser.h>

Description

Implements command-line parsing for Options objects.

Typical usage (without error checking):

gmx::Options options("name", "description");
// Fill up options
gmx::CommandLineParser(&options).parse(&argc, argv);
options.finish();
*

Classes

class  Impl
 Private implementation class for CommandLineParser. More...
 

Public Member Functions

 CommandLineParser (Options *options)
 Creates a command-line parser that sets values for options. More...
 
CommandLineParserskipUnknown (bool bEnabled)
 Makes the parser skip unknown options and keep them in argv. More...
 
CommandLineParserallowPositionalArguments (bool bEnabled)
 Makes the parser accept positional arguments. More...
 
void parse (int *argc, char *argv[])
 Parses the command line. More...
 

Constructor & Destructor Documentation

gmx::CommandLineParser::CommandLineParser ( Options options)
explicit

Creates a command-line parser that sets values for options.

Parameters
[in]optionsOptions object whose options should be set.
Exceptions
std::bad_allocif out of memory.

Member Function Documentation

CommandLineParser & gmx::CommandLineParser::allowPositionalArguments ( bool  bEnabled)

Makes the parser accept positional arguments.

Parameters
[in]bEnabledWhether to skip and keep positional arguments.
Returns
*this

Arguments that are not options (ie. no leading hyphen), and which come before all options are acceptable if this has been enabled. If so, these arguments are left in argc and argv in parse().

The default is false: unknown leading arguments result in exceptions and argc and argv are not modified.

Does not throw.

void gmx::CommandLineParser::parse ( int *  argc,
char *  argv[] 
)

Parses the command line.

Exceptions
std::bad_allocif out of memory.
InvalidInputErrorif any errors were detected in the input.

All command-line arguments are parsed, and an aggregate exception with all the detected errors (including unknown options, where applicable) is thrown in the end.

If skipUnknown() was not called, or last called with a false value, the input arguments are not modified. If skipUnknown() was last called with a true value, only unknown options will be retained in argc and argv.

All positional arguments are retained in the argument list, but such arguments must precede all options.

argv[0] is never modified.

CommandLineParser & gmx::CommandLineParser::skipUnknown ( bool  bEnabled)

Makes the parser skip unknown options and keep them in argv.

Parameters
[in]bEnabledWhether to skip and keep unknown options.
Returns
*this

Setting this option to true has dual effect: unknown options are silently skipped, and all recognized options are removed from argc and argv in parse(). These effects should be easy to separate into different flags if there is need for it.

The default is false: unknown options result in exceptions and argc and argv are not modified.

Does not throw.


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