A template for a class that helps process command-line arguments.
More...
#include <jade.args.hpp>
|
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...
|
|
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.
◆ char_type
template<typename TChar >
◆ basic_args() [1/2]
template<typename TChar >
template<typename TValue >
Initializes a new instance of the class based on the specified arguments. The first argument is ignored.
- Parameters
-
values | The command line arguments. |
Definition at line 30 of file jade.args.hpp.
33 , is_flag_read (
false)
36 auto iter = values.begin();
38 if (iter == values.end())
39 throw error(
"invalid command-line arguments");
41 while (++iter != values.end())
44 throw error(
"invalid command-line arguments");
46 _m.emplace_back(*iter);
◆ basic_args() [2/2]
template<typename TChar >
Initializes a new instance of the class based on the specified arguments. The first argument is ignored.
- Parameters
-
argc | The argument count. |
argv | The argument values. |
Definition at line 54 of file jade.args.hpp.
59 , is_flag_read (
false)
62 if (argc < 1 || argv ==
nullptr)
63 throw error(
"invalid command-line arguments");
65 for (
auto i = 1; i < argc; i++)
67 const auto s = argv[i];
69 throw error(
"invalid command-line arguments");
◆ get_length()
template<typename TChar >
- Returns
- The number of arguments remaining to be processed.
Definition at line 78 of file jade.args.hpp.
◆ is_empty()
template<typename TChar >
- Returns
- True if there are no more arguments to be processed.
Definition at line 86 of file jade.args.hpp.
◆ pop()
template<typename TChar >
template<typename TValue >
- 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.
99 throw error(
"not enough arguments");
101 const auto value = _parse<TValue>(0);
102 _m.erase(_m.begin());
◆ read()
template<typename TChar >
template<typename TValue >
Reads and removes an option with one argument. If the option was not specified, then the fallback value is returned.
- Parameters
-
long_name | The long option name. |
short_name | The short option name. |
fallback | The optional fallback value. |
- Returns
- The value after the flag or the fallback value.
Definition at line 117 of file jade.args.hpp.
122 assert(!is_flag_read);
124 const auto index = _find(long_name, short_name);
129 throw error() <<
"missing argument for option " << long_name;
131 const auto value = _parse<TValue>(
size_t(index + 1));
132 _m.erase(_m.begin() + index, _m.begin() + index + 2);
◆ read_flag()
template<typename TChar >
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_name | The long option name. |
short_name | The short option name. |
Definition at line 142 of file jade.args.hpp.
146 const auto index = _find(long_name, short_name);
154 _m.erase(_m.begin() + index);
◆ validate_empty()
template<typename TChar >
Throws an exception if there are more arguments to be processed.
Definition at line 161 of file jade.args.hpp.
167 throw error() <<
"unexpected option " << _m[0];
169 throw error() <<
"unexpected argument '" << _m[0] <<
"'";
◆ validate_length()
template<typename TChar >
Throws an exception if the number of remaining arguments is not the expected number.
- Parameters
-
length | The number of remaining arguments. |
Definition at line 176 of file jade.args.hpp.
180 if (_m.size() != length)
181 throw error(
"invalid syntax; try --help");
The documentation for this class was generated from the following file: