Repo-Init
 
Loading...
Searching...
No Matches
InputParser Class Reference

#include <InputParser.hpp>

Public Member Functions

 InputParser (const int &argc, char **argv)
 
const std::string & getCmdOption (const std::string &option) const
 
std::vector< std::pair< std::string, std::string > > getCmdOptions () const
 
bool cmdOptionExists (const std::string &option) const
 

Private Attributes

std::vector< std::string > _tokens
 

Detailed Description

Parses command line inputs

Definition at line 11 of file InputParser.hpp.

Constructor & Destructor Documentation

◆ InputParser()

InputParser::InputParser ( const int & argc,
char ** argv )
inline

Constructs a new InputParser object

Parameters
[in]argcNumber of input arguments
[in]argvInput arguments

Definition at line 21 of file InputParser.hpp.

22 {
23 for (int i = 1; i < argc; ++i)
24 {
25 _tokens.emplace_back(argv[i]);
26 }
27 }
std::vector< std::string > _tokens

Member Function Documentation

◆ cmdOptionExists()

bool InputParser::cmdOptionExists ( const std::string & option) const
inlinenodiscard

Checks whether provided command line option exists.

Parameters
[in]optionOption to check
Returns
true If the provided option is found
false If the provided option is not found

Definition at line 77 of file InputParser.hpp.

78 {
79 return std::find(_tokens.begin(), _tokens.end(), option) != _tokens.end();
80 }
Here is the caller graph for this function:

◆ getCmdOption()

const std::string & InputParser::getCmdOption ( const std::string & option) const
inlinenodiscard

Gets single command line input

Parameters
[in]optionOption to check
Returns
const std::string& Found command line input. Empty string if not found

Definition at line 34 of file InputParser.hpp.

35 {
36 std::vector<std::string>::const_iterator itr;
37 itr = std::find(_tokens.begin(), _tokens.end(), option);
38 if (itr != _tokens.end() && ++itr != _tokens.end())
39 {
40 return *itr;
41 }
42 static const std::string empty_string;
43 return empty_string;
44 }
Here is the caller graph for this function:

◆ getCmdOptions()

std::vector< std::pair< std::string, std::string > > InputParser::getCmdOptions ( ) const
inlinenodiscard

Gets all command line options

Returns
std::vector<std::pair<std::string, std::string>> All command line options

Definition at line 50 of file InputParser.hpp.

51 {
52 std::vector<std::pair<std::string, std::string>> options;
53 for (auto itr = _tokens.begin(); itr != _tokens.end(); ++itr)
54 {
55 if (!itr->empty() && itr->at(0) == '-')
56 {
57 auto nextItr = std::next(itr);
58 if (nextItr != _tokens.end() && !nextItr->empty() && nextItr->at(0) != '-')
59 {
60 options.emplace_back(*itr, *(nextItr));
61 }
62 else
63 {
64 options.emplace_back(*itr, "");
65 }
66 }
67 }
68 return options;
69 }
Here is the caller graph for this function:

Member Data Documentation

◆ _tokens

std::vector<std::string> InputParser::_tokens
private

Definition at line 13 of file InputParser.hpp.


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