Skip to content

Commit 7655c8a

Browse files
CppCXYtangzx
authored andcommitted
支持路径带空格,强化剩余参数的支持
1 parent b1e306e commit 7655c8a

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

emmy_tool/command_line.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,28 @@ bool CommandLine::Parse(int argc, char** argv)
9898
}
9999
if (option.RestOfAll)
100100
{
101-
optionValue.append(" ").append(value);
101+
// 剩余参数通常会被传递到子进程再处理
102+
// 如果剩余参数中存在路径,且路径存在空格,那么传递到子进程的创建就会失效
103+
// 所以这里要特别的处理
104+
if(!value.empty())
105+
{
106+
// 认为该参数可能是选项
107+
if(value[0] == '-')
108+
{
109+
optionValue.append(" ").append(value);
110+
}
111+
else //认为该参数是值,所以用引号包含起来
112+
{
113+
optionValue.append(" ").append("\""+value+"\"");
114+
}
115+
116+
}
117+
102118
}
103119
else
104120
{
105121
// 认为值是被一对引号包起来的
122+
// windows下引号已经被自动处理了
106123
if (value[0] == '\"' || value[0] == '\'')
107124
{
108125
optionValue = value.substr(1, value.size() - 2);
@@ -115,7 +132,7 @@ bool CommandLine::Parse(int argc, char** argv)
115132
}
116133
}
117134
while (true);
118-
135+
119136
option.Value = std::move(optionValue);
120137
}
121138
}

emmy_tool/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ int doRunAndAttach(CommandLine& commandLine)
6868
std::string dll = commandLine.Get<std::string>("dll");
6969
std::string dir = commandLine.Get<std::string>("work");
7070
std::string exe = commandLine.Get<std::string>("exe");
71-
std::string command = exe + " " + commandLine.Get<std::string>("args");
72-
bool blockOnExit = commandLine.Get<bool>("blockOnExit");
73-
71+
// 路径中可能存在空格
72+
std::string command = "\"" + exe + "\"" + " " + commandLine.Get<std::string>("args");
73+
bool blockOnExit = commandLine.Get<bool>("blockOnExit");
74+
7475
if (!StartProcessAndInjectDll(exe.c_str(),
7576
const_cast<LPSTR>(command.c_str()),
7677
dir.c_str(),

0 commit comments

Comments
 (0)