【发布时间】:2021-02-02 16:39:29
【问题描述】:
我正在使用 boost 从我的应用程序运行命令行命令。我正在使用以下已封装到辅助函数中的代码:
tuple<string, int> Utility::RunCommand(const string& arguments) const
{
string response;
int exitCode;
try
{
ipstream iStream;
auto childProcess = child(arguments, std_out > iStream);
string line;
while (getline(iStream, line) && !line.empty())
{
response += line;
}
childProcess.wait();
exitCode = childProcess.exit_code();
}
catch (...)
{
// log error
throw;
}
return make_tuple(response, exitCode);
}
现在我有一个只能在具有某些属性的机器上运行的命令。此方法返回这些机器上的预期响应和错误代码。在其他机器上,它会抛出异常。
我尝试在应该失败的机器上手动运行该命令,它会返回以下输出:
POWERSHELL
PS C:\Users\xyz> dummy-cmd
dummy-cmd : The term 'dummy-cmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ dummy-cmd
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dummy-cmd:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
COMMAND PROMPT
C:\Users\xyz>dummy-cmd
'dummy-cmd' is not recognized as an internal or external command,
operable program or batch file.
如何让它运行,使其返回非零错误代码而不是抛出异常?
【问题讨论】:
-
问题是你的可执行文件不存在。这就是 Powershell 所抱怨的。
-
您是否正在编译代码以获得
dummy-cmd.exe? -
@doron 是的,我知道那部分。我想知道是否有办法调用该命令,使其返回错误代码而不是抛出异常?
-
@FredLarson No. dummy-cmd.exe 在某些机器上存在而在其他机器上不存在。
标签: c++ exception boost command-line