Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit92d1b94

Browse files
committed
Arguments parsing in separated class.
1 parentb6cb000 commit92d1b94

File tree

6 files changed

+83
-37
lines changed

6 files changed

+83
-37
lines changed

‎src/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_executable(coderdbc
1818
${CMAKE_CURRENT_SOURCE_DIR}/helpers/formatter.cpp
1919
${CMAKE_CURRENT_SOURCE_DIR}/parser/dbclineparser.cpp
2020
${CMAKE_CURRENT_SOURCE_DIR}/parser/dbcscanner.cpp
21+
${CMAKE_CURRENT_SOURCE_DIR}/options-parser.cpp
2122
${CMAKE_CURRENT_SOURCE_DIR}/app.cpp
2223
${CMAKE_CURRENT_SOURCE_DIR}/maincli.cpp
2324
)
@@ -37,6 +38,7 @@ enable_testing()
3738

3839
add_executable(
3940
cctest
41+
${CMAKE_CURRENT_SOURCE_DIR}/options-parser.cpp
4042
${CMAKE_CURRENT_SOURCE_DIR}/tests/args-test.cpp
4143
)
4244

‎src/app.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#include<stdint.h>
44
#include<vector>
55
#include<string>
6+
#include<options-parser.h>
67

78
classCoderApp {
89
public:
9-
CoderApp(conststd::vector<std::pair<std::string, std::string>>& params) : Params(params) {}
10+
CoderApp(constOptionsParser::Pairs& params) : Params(params) {}
1011

1112
voidRun();
1213

@@ -21,7 +22,7 @@ class CoderApp {
2122
bool ok{false};
2223
} StrParam_t;
2324

24-
conststd::vector<std::pair<std::string, std::string>>& Params;
25+
constOptionsParser::Pairs& Params;
2526

2627
StrParam_t dbc{};
2728
StrParam_t outdir{};

‎src/maincli.cpp‎

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,12 @@
22
#include<stdlib.h>
33
#include<memory>
44
#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"
396

407
intmain(int argc,char* argv[])
418
{
42-
auto opts =getoptions(argc, argv);
9+
OptionsParser parser;
10+
auto opts = parser.GetOptions(argc, argv);
4311
auto app = std::make_unique<CoderApp>(opts);
4412
app->Run();
4513
}

‎src/options-parser.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include"options-parser.h"
2+
3+
OptionsParser::PairsOptionsParser::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+
}

‎src/options-parser.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include<stdlib.h>
4+
#include<string>
5+
#include<memory>
6+
#include<vector>
7+
8+
classOptionsParser {
9+
public:
10+
11+
using OnePair = std::pair<std::string, std::string>;
12+
using Pairs = std::vector<OnePair>;
13+
14+
PairsGetOptions(int argc,char** argv);
15+
16+
};
17+
18+

‎src/tests/args-test.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#include<gtest/gtest.h>
2+
#include<options-parser.h>
23

34
TEST(ArgParserTest, BasicAssert)
45
{
6+
staticchar* 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+
527
EXPECT_EQ(1,1);
628
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp