11#include " arg-parser-arg.h"
22#include < stdexcept>
33
4- arg_parser_arg::arg_parser_arg (
5- const std::wstring name,
6- const std::vector<std::wstring> alias,
7- const std::wstring description,
8- const std::vector<std::wstring> default_values,
9- const int arg_count
10- ) : m_name(name), m_aliases(alias), m_description(description), m_arg_count(arg_count), m_values(default_values) {};
11-
12- inline bool arg_parser_arg::operator ==(const std::wstring& other_arg) const
13- {
14- return is_match (other_arg);
15- }
16-
17- bool arg_parser_arg::is_match (const std::wstring& other_arg) const
18- {
19- return !other_arg.empty () && (other_arg == m_name || std::find (m_aliases.begin (), m_aliases.end (), other_arg) != m_aliases.end ());
20- }
21-
22- std::wstring arg_parser_arg::get_help () const
23- {
24-
25- return L" \t " + get_all_flags_string () + L" \n " + arg_parser_add_wstring_behind_multiline_text (arg_parser_format_string_to_length (m_description), L" \t " );
26- }
27-
28- std::wstring arg_parser_arg::get_all_flags_string () const
29- {
30- if (get_alias_string ().empty ()) return m_name;
31- return m_name + L" , " + get_alias_string ();
32- }
33-
34- std::wstring arg_parser_arg::get_usage_text () const
35- {
36- return m_description;
37- }
38-
39- arg_parser_arg arg_parser_arg::add_alias (std::wstring new_alias)
40- {
41- m_aliases.push_back (new_alias);
42- return *this ;
43- }
44-
45- int arg_parser_arg::get_arg_count () const
46- {
47- return m_arg_count;
48- }
49-
50- std::wstring arg_parser_arg::get_name () const
51- {
52- return m_name;
53- }
54-
55- std::wstring arg_parser_arg::get_alias_string () const
56- {
57- // convert alias vector to wstring
58- std::wstring alias_string;
59- for (auto & m_alias : m_aliases) {
60- if (m_alias.empty ()) continue ;
61- alias_string.append (m_alias + L" , " );
62- }
63- if (alias_string.find (L" , " ) != std::wstring::npos) alias_string.erase (alias_string.end () - 2 , alias_string.end ());
64-
65- return alias_string;
66- }
67-
68- arg_parser_arg arg_parser_arg::add_check_func (std::function<bool (const std::wstring&)> check_func)
69- {
70- m_check_funcs.push_back (check_func);
71- return *this ;
72- }
73-
74- void arg_parser_arg::set_is_parsed ()
75- {
76- m_is_parsed = true ;
77- }
78-
79- bool arg_parser_arg::is_parsed ()
80- {
81- return m_is_parsed;
82- }
83-
84- bool arg_parser_arg::is_set ()
85- {
86- return is_parsed () && m_values.size () == m_arg_count;
87- }
88-
89- std::vector<std::wstring> arg_parser_arg::get_values ()
90- {
91- return m_values;
92- }
93-
94- bool arg_parser_arg::parse (std::vector<std::wstring> arg_vect)
95- {
96- if (arg_vect.size () == 0 || !is_match (arg_vect[0 ]))
97- return false ;
98-
99- if (m_arg_count == -1 && arg_vect.size () > 0 ) m_arg_count = arg_vect.size () - 1 ;
100-
101- if (arg_vect.size () < m_arg_count + 1 )
102- throw std::invalid_argument (" Not enough arguments provided." );
103-
104- if (m_arg_count == 0 )
4+ namespace ArgParserArg {
5+ arg_parser_arg::arg_parser_arg (
6+ const std::wstring name,
7+ const std::vector<std::wstring> alias,
8+ const std::wstring description,
9+ const std::vector<std::wstring> default_values,
10+ const int arg_count
11+ ) : m_name(name), m_aliases(alias), m_description(description), m_arg_count(arg_count), m_values(default_values) {};
12+
13+ inline bool arg_parser_arg::operator ==(const std::wstring& other_arg) const
10514 {
106- set_is_parsed ();
107- return true ;
15+ return is_match (other_arg);
10816 }
109-
110- for ( int i = 1 ; i < m_arg_count + 1 ; ++i)
17+
18+ bool arg_parser_arg::is_match ( const std::wstring& other_arg) const
11119 {
112- for (auto & check_func : m_check_funcs)
113- {
114- if (!check_func (arg_vect[i]))
115- throw std::invalid_argument (" Invalid arguments provided." );
20+ return !other_arg.empty () && (other_arg == m_name || std::find (m_aliases.begin (), m_aliases.end (), other_arg) != m_aliases.end ());
21+ }
22+
23+ std::wstring arg_parser_arg::get_help () const
24+ {
25+
26+ return L" \t " + get_all_flags_string () + L" \n " + arg_parser_add_wstring_behind_multiline_text (arg_parser_format_string_to_length (m_description), L" \t " );
27+ }
28+
29+ std::wstring arg_parser_arg::get_all_flags_string () const
30+ {
31+ if (get_alias_string ().empty ()) return m_name;
32+ return m_name + L" , " + get_alias_string ();
33+ }
34+
35+ std::wstring arg_parser_arg::get_usage_text () const
36+ {
37+ return m_description;
38+ }
39+
40+ arg_parser_arg arg_parser_arg::add_alias (std::wstring new_alias)
41+ {
42+ m_aliases.push_back (new_alias);
43+ return *this ;
44+ }
45+
46+ int arg_parser_arg::get_arg_count () const
47+ {
48+ return m_arg_count;
49+ }
50+
51+ std::wstring arg_parser_arg::get_name () const
52+ {
53+ return m_name;
54+ }
55+
56+ std::wstring arg_parser_arg::get_alias_string () const
57+ {
58+ // convert alias vector to wstring
59+ std::wstring alias_string;
60+ for (auto & m_alias : m_aliases) {
61+ if (m_alias.empty ()) continue ;
62+ alias_string.append (m_alias + L" , " );
11663 }
117- m_values.push_back (arg_vect[i]);
64+ if (alias_string.find (L" , " ) != std::wstring::npos) alias_string.erase (alias_string.end () - 2 , alias_string.end ());
65+
66+ return alias_string;
67+ }
68+
69+ arg_parser_arg arg_parser_arg::add_check_func (std::function<bool (const std::wstring&)> check_func)
70+ {
71+ m_check_funcs.push_back (check_func);
72+ return *this ;
73+ }
74+
75+ void arg_parser_arg::set_is_parsed ()
76+ {
77+ m_is_parsed = true ;
78+ }
79+
80+ bool arg_parser_arg::is_parsed ()
81+ {
82+ return m_is_parsed;
11883 }
119- set_is_parsed ();
120- return true ;
121- }
12284
85+ bool arg_parser_arg::is_set ()
86+ {
87+ return is_parsed () && m_values.size () == m_arg_count;
88+ }
12389
124- std::wstring arg_parser_add_wstring_behind_multiline_text (const std::wstring& str, const std::wstring& prefix)
125- {
126- std::wstring formatted_str;
127- std::wstring current_line;
128- std::wistringstream iss (str);
129- while (getline (iss, current_line, L' \n ' ))
90+ std::vector<std::wstring> arg_parser_arg::get_values ()
13091 {
131- if (current_line.empty ())
92+ return m_values;
93+ }
94+
95+ bool arg_parser_arg::parse (std::vector<std::wstring> arg_vect)
96+ {
97+ if (arg_vect.size () == 0 || !is_match (arg_vect[0 ]))
98+ return false ;
99+
100+ if (m_arg_count == -1 && arg_vect.size () > 0 ) m_arg_count = arg_vect.size () - 1 ;
101+
102+ if (arg_vect.size () < m_arg_count + 1 )
103+ throw std::invalid_argument (" Not enough arguments provided." );
104+
105+ if (m_arg_count == 0 )
132106 {
133- formatted_str += L" \n " ;
134- continue ;
107+ set_is_parsed () ;
108+ return true ;
135109 }
136-
137- formatted_str += prefix + current_line + L" \n " ;
110+
111+ for (int i = 1 ; i < m_arg_count + 1 ; ++i)
112+ {
113+ for (auto & check_func : m_check_funcs)
114+ {
115+ if (!check_func (arg_vect[i]))
116+ throw std::invalid_argument (" Invalid arguments provided." );
117+ }
118+ m_values.push_back (arg_vect[i]);
119+ }
120+ set_is_parsed ();
121+ return true ;
138122 }
139- return formatted_str;
140- }
141123
142- std::wstring arg_parser_format_string_to_length (const std::wstring& str, size_t max_width)
143- {
144- std::wstring formatted_str;
145- std::wistringstream lines_stream (str);
146- std::wstring line;
147124
148- while ( std::getline (lines_stream, line, L ' \n ' ) )
125+ std::wstring arg_parser_add_wstring_behind_multiline_text ( const std::wstring& str, const std::wstring& prefix )
149126 {
127+ std::wstring formatted_str;
150128 std::wstring current_line;
151- std::wistringstream word_stream (line);
152- std::wstring word;
129+ std::wistringstream iss (str);
130+ while (getline (iss, current_line, L' \n ' ))
131+ {
132+ if (current_line.empty ())
133+ {
134+ formatted_str += L" \n " ;
135+ continue ;
136+ }
153137
154- while (word_stream >> word)
138+ formatted_str += prefix + current_line + L" \n " ;
139+ }
140+ return formatted_str;
141+ }
142+
143+ std::wstring arg_parser_format_string_to_length (const std::wstring& str, size_t max_width)
144+ {
145+ std::wstring formatted_str;
146+ std::wistringstream lines_stream (str);
147+ std::wstring line;
148+
149+ while (std::getline (lines_stream, line, L' \n ' ))
155150 {
156- if (current_line.size () + word.size () > max_width && !current_line.empty ())
151+ std::wstring current_line;
152+ std::wistringstream word_stream (line);
153+ std::wstring word;
154+
155+ while (word_stream >> word)
157156 {
158- if (!current_line.empty () && current_line.back () == L' ' )
157+ if (current_line.size () + word.size () > max_width && !current_line.empty ())
158+ {
159+ if (!current_line.empty () && current_line.back () == L' ' )
160+ {
161+ current_line.pop_back ();
162+ }
163+ formatted_str += current_line + L" \n " ;
164+ current_line = word + L" " ;
165+ }
166+ else
159167 {
160- current_line. pop_back () ;
168+ current_line += word + L" " ;
161169 }
162- formatted_str += current_line + L" \n " ;
163- current_line = word + L" " ;
164170 }
165- else
171+ if (!current_line. empty () && current_line. back () == L ' ' )
166172 {
167- current_line += word + L" " ;
173+ current_line.pop_back ();
174+ }
175+ if (!current_line.empty ())
176+ {
177+ formatted_str += current_line + L" \n\n " ;
168178 }
169179 }
170- if (!current_line.empty () && current_line.back () == L' ' )
171- {
172- current_line.pop_back ();
173- }
174- if (!current_line.empty ())
180+
181+ // Remove the trailing newline, if any
182+ if (!formatted_str.empty () && formatted_str.back () == L' \n\n ' )
175183 {
176- formatted_str += current_line + L" \n\n " ;
184+ formatted_str.pop_back ();
185+ formatted_str.pop_back ();
177186 }
187+
188+ return formatted_str;
178189 }
179-
180- // Remove the trailing newline, if any
181- if (!formatted_str.empty () && formatted_str.back () == L' \n\n ' )
182- {
183- formatted_str.pop_back ();
184- formatted_str.pop_back ();
185- }
186-
187- return formatted_str;
188190}
0 commit comments