File tree Expand file tree Collapse file tree 6 files changed +83
-37
lines changed Expand file tree Collapse file tree 6 files changed +83
-37
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ add_executable(coderdbc
18
18
${CMAKE_CURRENT_SOURCE_DIR} /helpers/formatter.cpp
19
19
${CMAKE_CURRENT_SOURCE_DIR} /parser/dbclineparser.cpp
20
20
${CMAKE_CURRENT_SOURCE_DIR} /parser/dbcscanner.cpp
21
+ ${CMAKE_CURRENT_SOURCE_DIR} /options-parser.cpp
21
22
${CMAKE_CURRENT_SOURCE_DIR} /app.cpp
22
23
${CMAKE_CURRENT_SOURCE_DIR} /maincli.cpp
23
24
)
@@ -37,6 +38,7 @@ enable_testing()
37
38
38
39
add_executable (
39
40
cctest
41
+ ${CMAKE_CURRENT_SOURCE_DIR} /options-parser.cpp
40
42
${CMAKE_CURRENT_SOURCE_DIR} /tests/args-test.cpp
41
43
)
42
44
Original file line number Diff line number Diff line change 3
3
#include < stdint.h>
4
4
#include < vector>
5
5
#include < string>
6
+ #include < options-parser.h>
6
7
7
8
class CoderApp {
8
9
public:
9
- CoderApp (const std::vector<std::pair<std::string, std::string>> & params) : Params(params) {}
10
+ CoderApp (const OptionsParser::Pairs & params) : Params(params) {}
10
11
11
12
void Run ();
12
13
@@ -21,7 +22,7 @@ class CoderApp {
21
22
bool ok{false };
22
23
} StrParam_t;
23
24
24
- const std::vector<std::pair<std::string, std::string>> & Params;
25
+ const OptionsParser::Pairs & Params;
25
26
26
27
StrParam_t dbc{};
27
28
StrParam_t outdir{};
Original file line number Diff line number Diff line change 2
2
#include < stdlib.h>
3
3
#include < memory>
4
4
#include " app.h"
5
-
6
- std::vector<std::pair<std::string, std::string>> getoptions (int argc, char ** argv)
7
- {
8
- std::vector<std::pair<std::string, std::string>> ret{};
9
-
10
- std::pair<std::string, std::string> pair{};
11
-
12
- if (argc <= 0 )
13
- {
14
- return ret;
15
- }
16
-
17
- for (int i = 0 ; i < argc; i++)
18
- {
19
- // key found (must start with '-' (e.g. '-dbc'))
20
- if (argv[i][0 ] == ' -' )
21
- {
22
- pair.first = std::string (argv[i]);
23
- pair.second .clear ();
24
-
25
- if ((i + 1 ) < argc && argv[i + 1 ][0 ] != ' -' )
26
- {
27
- // key param
28
- pair.second = std::string (argv[i + 1 ]);
29
- // unlooped i incremention
30
- ++i;
31
- }
32
-
33
- ret.push_back (pair);
34
- }
35
- }
36
-
37
- return ret;
38
- }
5
+ #include " options-parser.h"
39
6
40
7
int main (int argc, char * argv[])
41
8
{
42
- auto opts = getoptions (argc, argv);
9
+ OptionsParser parser;
10
+ auto opts = parser.GetOptions (argc, argv);
43
11
auto app = std::make_unique<CoderApp>(opts);
44
12
app->Run ();
45
13
}
Original file line number Diff line number Diff line change
1
+ #include " options-parser.h"
2
+
3
+ OptionsParser::Pairs OptionsParser::GetOptions (int argc, char ** argv)
4
+ {
5
+ Pairs ret{};
6
+
7
+ OnePair pair{};
8
+
9
+ if (argc <= 0 )
10
+ {
11
+ return ret;
12
+ }
13
+
14
+ for (int i = 0 ; i < argc; i++)
15
+ {
16
+ // key found (must start with '-' (e.g. '-dbc'))
17
+ if (argv[i][0 ] == ' -' )
18
+ {
19
+ pair.first = std::string (argv[i]);
20
+ pair.second .clear ();
21
+
22
+ if ((i + 1 ) < argc && argv[i + 1 ][0 ] != ' -' )
23
+ {
24
+ // key param
25
+ pair.second = std::string (argv[i + 1 ]);
26
+ // unlooped i incremention
27
+ ++i;
28
+ }
29
+
30
+ ret.push_back (pair);
31
+ }
32
+ }
33
+
34
+ return ret;
35
+ }
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include < stdlib.h>
4
+ #include < string>
5
+ #include < memory>
6
+ #include < vector>
7
+
8
+ class OptionsParser {
9
+ public:
10
+
11
+ using OnePair = std::pair<std::string, std::string>;
12
+ using Pairs = std::vector<OnePair>;
13
+
14
+ Pairs GetOptions (int argc, char ** argv);
15
+
16
+ };
17
+
18
+
Original file line number Diff line number Diff line change 1
1
#include < gtest/gtest.h>
2
+ #include < options-parser.h>
2
3
3
4
TEST (ArgParserTest, BasicAssert)
4
5
{
6
+ static char * testchunks[] =
7
+ {
8
+ (char *)" appname" ,
9
+ (char *)" -out" ,
10
+ (char *)" path/to/out" ,
11
+ (char *)" -dbc" ,
12
+ (char *)" path/to/test.dbc" ,
13
+ (char *)" -rw"
14
+ };
15
+
16
+ OptionsParser parser;
17
+ auto ret = parser.GetOptions (6 , testchunks);
18
+ EXPECT_TRUE (ret.size () == 3 );
19
+
20
+ EXPECT_EQ (ret[0 ].first .compare (" -out" ), 0 );
21
+ EXPECT_EQ (ret[0 ].second .compare (" path/to/out" ), 0 );
22
+ EXPECT_EQ (ret[1 ].first .compare (" -dbc" ), 0 );
23
+ EXPECT_EQ (ret[1 ].second .compare (" path/to/test.dbc" ), 0 );
24
+ EXPECT_EQ (ret[2 ].first .compare (" -rw" ), 0 );
25
+ EXPECT_EQ (ret[2 ].second .size (), 0 );
26
+
5
27
EXPECT_EQ (1 , 1 );
6
28
}
You can’t perform that action at this time.
0 commit comments