1
1
#include " command_line.h"
2
2
3
- void CommandLine::AddTarget (const std::string& name, bool isParse)
4
- {
3
+ void CommandLine::AddTarget (const std::string &name, bool isParse) {
5
4
_targets.insert ({name, isParse});
6
5
}
7
6
8
- std::string CommandLine::GetTarget () const noexcept
9
- {
7
+ std::string CommandLine::GetTarget () const noexcept {
10
8
return _currentTarget;
11
9
}
12
10
13
- std::string CommandLine::GetArg (int index) const noexcept
14
- {
15
- if (static_cast <std::size_t >(index) < _argvs.size ())
16
- {
11
+ std::string CommandLine::GetArg (int index) const noexcept {
12
+ if (static_cast <std::size_t >(index) < _argvs.size ()) {
17
13
return _argvs[index];
18
- }
19
- else
20
- {
14
+ } else {
21
15
return " " ;
22
16
}
23
17
}
24
18
25
- bool CommandLine::Parse (int argc, char ** argv)
26
- {
27
- if (argc < 2 )
28
- {
19
+ bool CommandLine::Parse (int argc, char **argv) {
20
+ if (argc < 2 ) {
29
21
return false ;
30
22
}
31
23
_currentTarget = argv[1 ];
32
24
33
- if (_targets.count (_currentTarget) == 0 )
34
- {
25
+ if (_targets.count (_currentTarget) == 0 ) {
35
26
return false ;
36
27
}
37
28
38
29
bool isParse = _targets.at (_currentTarget);
39
30
_argvs.reserve (argc);
40
- for (int i = 0 ; i != argc; i++)
41
- {
31
+ for (int i = 0 ; i != argc; i++) {
42
32
_argvs.emplace_back (argv[i]);
43
33
}
44
34
45
- if (!isParse)
46
- {
35
+ if (!isParse) {
47
36
return true ;
48
37
}
49
38
50
39
// index = 0 的参数是程序名
51
- for (int index = 1 ; index < argc; index++)
52
- {
40
+ for (int index = 1 ; index < argc; index++) {
53
41
std::string current = argv[index];
54
- if (current.empty ())
55
- {
42
+ if (current.empty ()) {
56
43
continue ;
57
44
}
58
45
// not empty
59
- if (current[0 ] == ' -' )
60
- {
46
+ if (current[0 ] == ' -' ) {
61
47
// 仅仅支持-dir这种形式
62
48
std::string optionName = current.substr (1 );
63
49
// 如果该参数不存在
64
- if (_args.count (optionName) == 0 )
65
- {
50
+ if (_args.count (optionName) == 0 ) {
66
51
return false ;
67
52
}
68
- CommandLineOption& option = _args[optionName];
53
+ CommandLineOption & option = _args[optionName];
69
54
70
- if (option.Type == CommandLineValueType::Boolean)
71
- {
55
+ if (option.Type == CommandLineValueType::Boolean) {
72
56
option.Value = " true" ;
73
57
continue ;
74
58
}
@@ -78,57 +62,43 @@ bool CommandLine::Parse(int argc, char** argv)
78
62
79
63
// 该选项之后没有接参数
80
64
// 目前没有支持bool选项的必要
81
- if (argc <= (index + 1 ) && !option.RestOfAll )
82
- {
65
+ if (argc <= (index + 1 ) && !option.RestOfAll ) {
83
66
return false ;
84
67
}
85
68
86
- do
87
- {
88
- if (argc <= (index + 1 ))
89
- {
69
+ do {
70
+ if (argc <= (index + 1 )) {
90
71
break ;
91
72
}
92
73
93
74
std::string value = argv[++index];
94
- if (value.empty ())
95
- {
75
+ if (value.empty ()) {
96
76
continue ;
97
77
}
98
- if (option.RestOfAll )
99
- {
78
+ if (option.RestOfAll ) {
100
79
// 剩余参数通常会被传递到子进程再处理
101
80
// 如果剩余参数中存在路径,且路径存在空格,那么传递到子进程的创建就会失效
102
81
// 所以这里要特别的处理
103
- if (!value.empty ())
104
- {
82
+ if (!value.empty ()) {
105
83
// 认为该参数可能是选项
106
- if (value[0 ] == ' -' )
107
- {
84
+ if (value[0 ] == ' -' ) {
108
85
optionValue.append (" " ).append (value);
109
- }
110
- else // 认为该参数是值,所以用引号包含起来
86
+ } else // 认为该参数是值,所以用引号包含起来
111
87
{
112
88
optionValue.append (" " ).append (" \" " + value + " \" " );
113
89
}
114
90
}
115
- }
116
- else
117
- {
91
+ } else {
118
92
// 认为值是被一对引号包起来的
119
93
// windows下引号已经被自动处理了
120
- if (value[0 ] == ' \" ' || value[0 ] == ' \' ' )
121
- {
94
+ if (value[0 ] == ' \" ' || value[0 ] == ' \' ' ) {
122
95
optionValue = value.substr (1 , value.size () - 2 );
123
- }
124
- else
125
- {
96
+ } else {
126
97
optionValue = value;
127
98
}
128
99
break ;
129
100
}
130
- }
131
- while (true );
101
+ } while (true );
132
102
133
103
option.Value = std::move (optionValue);
134
104
}
0 commit comments