11#include " command_line.h"
22
3- void CommandLine::AddTarget (const std::string& name, bool isParse)
4- {
3+ void CommandLine::AddTarget (const std::string &name, bool isParse) {
54 _targets.insert ({name, isParse});
65}
76
8- std::string CommandLine::GetTarget () const noexcept
9- {
7+ std::string CommandLine::GetTarget () const noexcept {
108 return _currentTarget;
119}
1210
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 ()) {
1713 return _argvs[index];
18- }
19- else
20- {
14+ } else {
2115 return " " ;
2216 }
2317}
2418
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 ) {
2921 return false ;
3022 }
3123 _currentTarget = argv[1 ];
3224
33- if (_targets.count (_currentTarget) == 0 )
34- {
25+ if (_targets.count (_currentTarget) == 0 ) {
3526 return false ;
3627 }
3728
3829 bool isParse = _targets.at (_currentTarget);
3930 _argvs.reserve (argc);
40- for (int i = 0 ; i != argc; i++)
41- {
31+ for (int i = 0 ; i != argc; i++) {
4232 _argvs.emplace_back (argv[i]);
4333 }
4434
45- if (!isParse)
46- {
35+ if (!isParse) {
4736 return true ;
4837 }
4938
5039 // index = 0 的参数是程序名
51- for (int index = 1 ; index < argc; index++)
52- {
40+ for (int index = 1 ; index < argc; index++) {
5341 std::string current = argv[index];
54- if (current.empty ())
55- {
42+ if (current.empty ()) {
5643 continue ;
5744 }
5845 // not empty
59- if (current[0 ] == ' -' )
60- {
46+ if (current[0 ] == ' -' ) {
6147 // 仅仅支持-dir这种形式
6248 std::string optionName = current.substr (1 );
6349 // 如果该参数不存在
64- if (_args.count (optionName) == 0 )
65- {
50+ if (_args.count (optionName) == 0 ) {
6651 return false ;
6752 }
68- CommandLineOption& option = _args[optionName];
53+ CommandLineOption & option = _args[optionName];
6954
70- if (option.Type == CommandLineValueType::Boolean)
71- {
55+ if (option.Type == CommandLineValueType::Boolean) {
7256 option.Value = " true" ;
7357 continue ;
7458 }
@@ -78,57 +62,43 @@ bool CommandLine::Parse(int argc, char** argv)
7862
7963 // 该选项之后没有接参数
8064 // 目前没有支持bool选项的必要
81- if (argc <= (index + 1 ) && !option.RestOfAll )
82- {
65+ if (argc <= (index + 1 ) && !option.RestOfAll ) {
8366 return false ;
8467 }
8568
86- do
87- {
88- if (argc <= (index + 1 ))
89- {
69+ do {
70+ if (argc <= (index + 1 )) {
9071 break ;
9172 }
9273
9374 std::string value = argv[++index];
94- if (value.empty ())
95- {
75+ if (value.empty ()) {
9676 continue ;
9777 }
98- if (option.RestOfAll )
99- {
78+ if (option.RestOfAll ) {
10079 // 剩余参数通常会被传递到子进程再处理
10180 // 如果剩余参数中存在路径,且路径存在空格,那么传递到子进程的创建就会失效
10281 // 所以这里要特别的处理
103- if (!value.empty ())
104- {
82+ if (!value.empty ()) {
10583 // 认为该参数可能是选项
106- if (value[0 ] == ' -' )
107- {
84+ if (value[0 ] == ' -' ) {
10885 optionValue.append (" " ).append (value);
109- }
110- else // 认为该参数是值,所以用引号包含起来
86+ } else // 认为该参数是值,所以用引号包含起来
11187 {
11288 optionValue.append (" " ).append (" \" " + value + " \" " );
11389 }
11490 }
115- }
116- else
117- {
91+ } else {
11892 // 认为值是被一对引号包起来的
11993 // windows下引号已经被自动处理了
120- if (value[0 ] == ' \" ' || value[0 ] == ' \' ' )
121- {
94+ if (value[0 ] == ' \" ' || value[0 ] == ' \' ' ) {
12295 optionValue = value.substr (1 , value.size () - 2 );
123- }
124- else
125- {
96+ } else {
12697 optionValue = value;
12798 }
12899 break ;
129100 }
130- }
131- while (true );
101+ } while (true );
132102
133103 option.Value = std::move (optionValue);
134104 }
0 commit comments