Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
syscmd.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <array>
35#include <cstdlib>
36#include <map>
37#include <memory>
38#include <string>
39#include <vector>
40
41#ifdef _WIN32
42#define POPEN _popen
43#define PCLOSE _pclose
44#else
45#define POPEN popen
46#define PCLOSE pclose
47#endif
48
49namespace console {
55 class CmdResult {
56 int status_;
57 std::unique_ptr<std::string> ptr_;
58
63
67 friend CmdResult syscmd(const std::string &,
68 const std::vector<std::string> &,
69 const std::map<std::string, std::string> &);
70
71 public:
77 bool ok() const { return ptr_ && status_ == 0; }
78
84 bool error() const { return ptr_ && status_ != 0; }
85
91 bool fail() const { return !ptr_; }
92
97 int status() const { return status_; }
98
105 const std::string &output() const { return *ptr_; }
106 };
107
123 inline CmdResult syscmd(const std::string &exe,
124 const std::vector<std::string> &args = {},
125 const std::map<std::string, std::string> &kwargs = {}) {
126 std::string cmd = exe;
127 for (const std::string &arg : args) cmd += " " + arg;
128 for (const auto &kwarg : kwargs)
129 cmd += " " + kwarg.first + " " + kwarg.second;
130 cmd += " 2>&1";
131 FILE *pipe = POPEN(cmd.c_str(), "r");
132 if (!pipe) {
133 CmdResult cr;
134 cr.status_ = -1;
135 cr.ptr_ = nullptr;
136 return cr;
137 }
138 std::array<char, 256> buffer;
139 std::unique_ptr<std::string> output(new std::string);
140 while (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
141 *output += buffer.data();
142 int status = PCLOSE(pipe);
143 CmdResult cr;
144 cr.status_ = status;
145 cr.ptr_ = std::move(output);
146 return cr;
147 }
148}
149
150#undef POPEN
151#undef PCLOSE
命令执行结果封装类。
Definition syscmd.h:55
bool ok() const
检查命令是否成功执行。
Definition syscmd.h:77
const std::string & output() const
获取命令的标准输出内容。
Definition syscmd.h:105
friend CmdResult syscmd(const std::string &, const std::vector< std::string > &, const std::map< std::string, std::string > &)
友元声明,仅允许 syscmd 创建实例。
Definition syscmd.h:123
CmdResult()
私有默认构造函数。
Definition syscmd.h:62
std::unique_ptr< std::string > ptr_
命令输出内容(含 stderr)
Definition syscmd.h:57
int status_
命令退出状态码(0 成功,-1 管道打开失败)
Definition syscmd.h:56
bool fail() const
检查命令执行是否完全失败。
Definition syscmd.h:91
int status() const
获取命令退出状态码。
Definition syscmd.h:97
bool error() const
检查命令是否执行但返回错误码。
Definition syscmd.h:84
本库所有组件所在的顶层命名空间。
CmdResult syscmd(const std::string &exe, const std::vector< std::string > &args={}, const std::map< std::string, std::string > &kwargs={})
执行系统命令并捕获输出。
Definition syscmd.h:123
#define PCLOSE
Definition syscmd.h:46
#define POPEN
Definition syscmd.h:45