ʻOhana
Population structure, admixture history, and selection using learning methods.
Public Types | Public Member Functions
jade::basic_args< TChar > Class Template Reference

A template for a class that helps process command-line arguments. More...

#include <jade.args.hpp>

+ Collaboration diagram for jade::basic_args< TChar >:

Public Types

typedef TChar char_type
 The character type. More...
 

Public Member Functions

template<typename TValue >
 basic_args (const std::initializer_list< TValue > &values)
 Initializes a new instance of the class based on the specified arguments. The first argument is ignored. More...
 
 basic_args (const int argc, const char_type *argv[])
 Initializes a new instance of the class based on the specified arguments. The first argument is ignored. More...
 
size_t get_length () const
 
bool is_empty () const
 
template<typename TValue >
TValue pop ()
 
template<typename TValue >
TValue read (char_type const *const long_name, char_type const *const short_name, const TValue fallback=TValue())
 Reads and removes an option with one argument. If the option was not specified, then the fallback value is returned. More...
 
bool read_flag (char_type const *const long_name, char_type const *const short_name)
 Reads and removes an option with no arguments. If the options was not specified, then false is returned. More...
 
void validate_empty () const
 Throws an exception if there are more arguments to be processed. More...
 
void validate_length (const size_t length) const
 Throws an exception if the number of remaining arguments is not the expected number. More...
 

Detailed Description

template<typename TChar>
class jade::basic_args< TChar >

A template for a class that helps process command-line arguments.

Definition at line 18 of file jade.args.hpp.

Member Typedef Documentation

◆ char_type

template<typename TChar >
typedef TChar jade::basic_args< TChar >::char_type

The character type.

Definition at line 21 of file jade.args.hpp.

Constructor & Destructor Documentation

◆ basic_args() [1/2]

template<typename TChar >
template<typename TValue >
jade::basic_args< TChar >::basic_args ( const std::initializer_list< TValue > &  values)
inlineexplicit

Initializes a new instance of the class based on the specified arguments. The first argument is ignored.

Parameters
valuesThe command line arguments.

Definition at line 30 of file jade.args.hpp.

31  : _m ()
32  #ifndef NDEBUG
33  , is_flag_read (false)
34  #endif // NDEBUG
35  {
36  auto iter = values.begin();
37 
38  if (iter == values.end())
39  throw error("invalid command-line arguments");
40 
41  while (++iter != values.end())
42  {
43  if (*iter == nullptr)
44  throw error("invalid command-line arguments");
45 
46  _m.emplace_back(*iter);
47  }
48  }

◆ basic_args() [2/2]

template<typename TChar >
jade::basic_args< TChar >::basic_args ( const int  argc,
const char_type argv[] 
)
inline

Initializes a new instance of the class based on the specified arguments. The first argument is ignored.

Parameters
argcThe argument count.
argvThe argument values.

Definition at line 54 of file jade.args.hpp.

57  : _m ()
58  #ifndef NDEBUG
59  , is_flag_read (false)
60  #endif // NDEBUG
61  {
62  if (argc < 1 || argv == nullptr)
63  throw error("invalid command-line arguments");
64 
65  for (auto i = 1; i < argc; i++)
66  {
67  const auto s = argv[i];
68  if (s == nullptr)
69  throw error("invalid command-line arguments");
70 
71  _m.emplace_back(s);
72  }
73  }

Member Function Documentation

◆ get_length()

template<typename TChar >
size_t jade::basic_args< TChar >::get_length ( ) const
inline
Returns
The number of arguments remaining to be processed.

Definition at line 78 of file jade.args.hpp.

79  {
80  return _m.size();
81  }
+ Here is the caller graph for this function:

◆ is_empty()

template<typename TChar >
bool jade::basic_args< TChar >::is_empty ( ) const
inline
Returns
True if there are no more arguments to be processed.

Definition at line 86 of file jade.args.hpp.

87  {
88  return _m.empty();
89  }

◆ pop()

template<typename TChar >
template<typename TValue >
TValue jade::basic_args< TChar >::pop ( )
inline
Returns
The next argument from the stack or throws an exception if there are no more arguments to be processed.

Definition at line 96 of file jade.args.hpp.

97  {
98  if (_m.empty())
99  throw error("not enough arguments");
100 
101  const auto value = _parse<TValue>(0);
102  _m.erase(_m.begin());
103  return value;
104  }
+ Here is the caller graph for this function:

◆ read()

template<typename TChar >
template<typename TValue >
TValue jade::basic_args< TChar >::read ( char_type const *const  long_name,
char_type const *const  short_name,
const TValue  fallback = TValue() 
)
inline

Reads and removes an option with one argument. If the option was not specified, then the fallback value is returned.

Parameters
long_nameThe long option name.
short_nameThe short option name.
fallbackThe optional fallback value.
Returns
The value after the flag or the fallback value.

Definition at line 117 of file jade.args.hpp.

121  {
122  assert(!is_flag_read);
123 
124  const auto index = _find(long_name, short_name);
125  if (index < 0)
126  return fallback;
127 
128  if (index + 1 >= int(get_length()))
129  throw error() << "missing argument for option " << long_name;
130 
131  const auto value = _parse<TValue>(size_t(index + 1));
132  _m.erase(_m.begin() + index, _m.begin() + index + 2);
133  return value;
134  }
+ Here is the call graph for this function:

◆ read_flag()

template<typename TChar >
bool jade::basic_args< TChar >::read_flag ( char_type const *const  long_name,
char_type const *const  short_name 
)
inline

Reads and removes an option with no arguments. If the options was not specified, then false is returned.

Returns
True if the flag is found; otherwise, false.
Parameters
long_nameThe long option name.
short_nameThe short option name.

Definition at line 142 of file jade.args.hpp.

145  {
146  const auto index = _find(long_name, short_name);
147  if (index < 0)
148  return false;
149 
150  #ifndef NDEBUG
151  is_flag_read = true;
152  #endif // NDEBUG
153 
154  _m.erase(_m.begin() + index);
155  return true;
156  }

◆ validate_empty()

template<typename TChar >
void jade::basic_args< TChar >::validate_empty ( ) const
inline

Throws an exception if there are more arguments to be processed.

Definition at line 161 of file jade.args.hpp.

162  {
163  if (_m.empty())
164  return;
165 
166  if (_m[0][0] == '-')
167  throw error() << "unexpected option " << _m[0];
168 
169  throw error() << "unexpected argument '" << _m[0] << "'";
170  }
+ Here is the caller graph for this function:

◆ validate_length()

template<typename TChar >
void jade::basic_args< TChar >::validate_length ( const size_t  length) const
inline

Throws an exception if the number of remaining arguments is not the expected number.

Parameters
lengthThe number of remaining arguments.

Definition at line 176 of file jade.args.hpp.

179  {
180  if (_m.size() != length)
181  throw error("invalid syntax; try --help");
182  }

The documentation for this class was generated from the following file:
jade::basic_args::get_length
size_t get_length() const
Definition: jade.args.hpp:78