Gromacs  2026.0-dev-20241113-4b16639
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions
gmx::test::CommandLine Class Reference

#include <testutils/include/testutils/cmdlinetest.h>

Description

Helper class for tests that need to construct command lines.

This class helps in writing tests for command-line handling. The constructor method takes an array of const char pointers, specifying the command-line arguments, each as one array element. It is also possible to construct the command line by adding individual arguments with append() and addOption(). The argc() and argv() methods can then be used to obtain argc and argv (non-const char pointers) arrays for passing into methods that expect these.

Note that although the interface allows passing the argc and argv pointers to methods that modify them (typically as f(&argc(), argv())), currently the CommandLine object is not in a consistent state internally if the parameters are actually modified. Reading the command line is possible afterwards, but modification is not.

If you need to construct command lines that refer to files on the file system, see CommandLineTestHelper and CommandLineTestBase for additional convenience utilities.

All constructors and methods that modify this class may throw an std::bad_alloc. Const methods and accessors do not throw.

Public Member Functions

 CommandLine ()
 Initializes an empty command-line object.
 
 CommandLine (const ArrayRef< const char *const > &cmdline)
 Initializes a command-line object from an array. More...
 
 CommandLine (const ArrayRef< const std::string > &cmdline)
 Initializes a command-line object from an array. More...
 
 CommandLine (const CommandLine &other)
 Creates a deep copy of a command-line object.
 
void initFromArray (const ArrayRef< const char *const > &cmdline)
 Initializes a command-line object in-place from an array. More...
 
void append (const char *arg)
 Appends an argument to the command line. More...
 
void append (const std::string &arg)
 Convenience overload taking a std::string.
 
void addOption (const char *name)
 Adds an option to the command line, typically a boolean. More...
 
void addOption (const char *name, const char *value)
 Adds an option-value pair to the command line. More...
 
void addOption (const char *name, const std::string &value)
 Convenience overload taking a std::string.
 
void addOption (const char *name, int value)
 Overload taking an int.
 
void addOption (const char *name, double value)
 Overload taking a double.
 
void merge (const CommandLine &args)
 Appends all arguments from args to the command line. More...
 
int & argc ()
 Returns argc for passing into C-style command-line handling.
 
char ** argv ()
 Returns argv for passing into C-style command-line handling.
 
int argc () const
 Returns argc for passing into C-style command-line handling.
 
const char *const * argv () const
 Returns argv for passing into C-style command-line handling.
 
const char * arg (int i) const
 Returns a single argument.
 
std::string toString () const
 Returns the command line formatted as a single string.
 
bool contains (std::string_view name) const
 Whether the command line contains the given option.
 
std::optional< std::string_view > argumentOf (std::string_view name) const
 The value of the argument following the given option, if found.
 

Constructor & Destructor Documentation

gmx::test::CommandLine::CommandLine ( const ArrayRef< const char *const > &  cmdline)

Initializes a command-line object from an array.

Parameters
[in]cmdlineArray of command-line arguments.

cmdline should include the binary name as the first element if that is desired in the output.

This constructor is not explicit to make it possible to create a CommandLine object directly from a C array.

gmx::test::CommandLine::CommandLine ( const ArrayRef< const std::string > &  cmdline)

Initializes a command-line object from an array.

Parameters
[in]cmdlineArray of command-line arguments.

cmdline should include the binary name as the first element if that is desired in the output.

This constructor is not explicit to make it possible to create a CommandLine object directly from a C array.

Member Function Documentation

void gmx::test::CommandLine::addOption ( const char *  name)

Adds an option to the command line, typically a boolean.

Parameters
[in]nameName of the option to append, which should start with "-".
void gmx::test::CommandLine::addOption ( const char *  name,
const char *  value 
)

Adds an option-value pair to the command line.

Parameters
[in]nameName of the option to append, which should start with "-".
[in]valueValue of the argument to append.
void gmx::test::CommandLine::append ( const char *  arg)

Appends an argument to the command line.

Parameters
[in]argArgument to append.

Strong exception safety.

void gmx::test::CommandLine::initFromArray ( const ArrayRef< const char *const > &  cmdline)

Initializes a command-line object in-place from an array.

Parameters
[in]cmdlineArray of command-line arguments.

cmdline should include the binary name as the first element if that is desired in the output.

This function does the same as the constructor that takes a ArrayRef. Any earlier contents of the object are discarded.

Strong exception safety.

void gmx::test::CommandLine::merge ( const CommandLine args)

Appends all arguments from args to the command line.

If the first argument of args does not start with a -, it is skipped, assuming it is a gmx module name and thus useless.


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