当前位置: 首页 > news >正文

官方网站 优帮云网页游戏制作平台

官方网站 优帮云,网页游戏制作平台,中国菲律宾合作,广东专注网站建设怎么样自己写CPP脚本小工具1. 思路描述2. 代码实现2.1 代码文件CppTool.cpp2.2 CMakeLists.txt3. 工具示例3.1 帮助信息3.2 工具用法3.3 实际使用1. 思路描述 实现一个简单的命令行工具。内容包括: 命令帮助信息参数检查,参数解析等功能。执行其他命令。将指…

自己写CPP脚本小工具

  • 1. 思路描述
  • 2. 代码实现
    • 2.1 代码文件CppTool.cpp
    • 2.2 CMakeLists.txt
  • 3. 工具示例
    • 3.1 帮助信息
    • 3.2 工具用法
    • 3.3 实际使用


1. 思路描述

实现一个简单的命令行工具。内容包括:

  1. 命令帮助信息
  2. 参数检查,参数解析等功能。
  3. 执行其他命令。
  4. 将指令封装成一个类,在main()中调用类的对象,执行命令。

2. 代码实现

2.1 代码文件CppTool.cpp

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <iomanip>
#include <algorithm>
#include <stdexcept>class CommandLineTool {
public:CommandLineTool(int argc, char* argv[]) {parseArguments(argc, argv);}void execute() {if (showHelp) {displayHelp();return;}if (showVersion) {displayVersion();return;}if (showTime) {displayCurrentTime();return;}if (!commandToExecute.empty()) {executeCommand();return;}// throw std::runtime_error("No valid command specified. Use -h for help.");}private:bool showHelp = false;bool showVersion = false;bool showTime = false;std::string commandToExecute;std::string outputFile;void parseArguments(int argc, char* argv[]) {if (argc < 2) {// throw std::runtime_error("No arguments provided. Use -h for help.");displayHelp();}std::vector<std::string> args(argv + 1, argv + argc);for (auto it = args.begin(); it != args.end(); ++it) {if (*it == "-h" || *it == "--help") {showHelp = true;} else if (*it == "-v" || *it == "--version") {showVersion = true;} else if (*it == "-t" || *it == "--time") {showTime = true;} else if (*it == "-e" || *it == "--execute") {if (++it == args.end()) {throw std::runtime_error("Missing command for -e/--execute");}commandToExecute = *it;} else if (*it == "-o" || *it == "--output") {if (++it == args.end()) {throw std::runtime_error("Missing filename for -o/--output");}outputFile = *it;} else {throw std::runtime_error("Unknown argument: " + *it);}}}void displayHelp() {std::cout << "Command Line Tool v1.0\n"<< "Usage: cltool [OPTIONS]\n\n"<< "Options:\n"<< "  -h, --help\t\tShow this help message\n"<< "  -v, --version\t\tDisplay version information\n"<< "  -t, --time\t\tDisplay current date and time\n"<< "  -e, --execute CMD\tExecute specified command\n"<< "  -o, --output FILE\tSave output to specified file\n";}void displayVersion() {std::cout << "Command Line Tool Version 1.0\n";}void displayCurrentTime() {std::time_t now = std::time(nullptr);std::tm* timeinfo = std::localtime(&now);std::cout << "Current time: "<< std::put_time(timeinfo, "%Y-%m-%d %H:%M:%S")<< std::endl;}void executeCommand() {std::cout << "Executing command: " << commandToExecute << "\n";// 实际执行命令的代码system(commandToExecute.c_str());// 示例输出std::cout << "Command executed successfully\n";if (!outputFile.empty()) {std::cout << "Output saved to: " << outputFile << "\n";// 实际保存到文件的代码}}
};int main(int argc, char* argv[]) {try {CommandLineTool tool(argc, argv);tool.execute();} catch (const std::exception& e) {std::cerr << "Error: " << e.what() << std::endl;return 1;}return 0;
}

2.2 CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(CppTool)set(CMAKE_CXX_STANDARD 11)add_executable(CppTool CppTool.cpp)
  1. 此处默认读者会使用Cmake编译Cpp文件,如未安装Cmake工具,请参考网上其他教程安装Cmake工具,并添加环境变量。

  2. 编译命令如下:

  • 在CMakeLists.txt文件所在目录,新建下级目录build,用于存放临时文件及编译产物。依次按步骤执行下列命令:
mkdir build
cd build
cmake ..
make

3. 工具示例

3.1 帮助信息

Command Line Tool v1.0
Usage: cltool [OPTIONS]Options:-h, --help            Show this help message-v, --version         Display version information-t, --time            Display current date and time-e, --execute CMD     Execute specified command-o, --output FILE     Save output to specified file

3.2 工具用法

# 显示帮助
./CppTool -h# 显示版本
./CppTool --version# 显示当前时间
./CppTool -t# 执行命令
./CppTool -e "ls -l"# 保存输出到文件
./CppTool -t -o time.txt

3.3 实际使用

C:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>CppTool.exe
Error: No arguments provided. Use -h for help.C:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>CppTool.exe --help
Command Line Tool v1.0
Usage: cltool [OPTIONS]Options:-h, --help            Show this help message-v, --version         Display version information-t, --time            Display current date and time-e, --execute CMD     Execute specified command-i, --ipinfo          Display network IP information-o, --output FILE     Save output to specified fileC:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>CppTool.exe -t
Current time: 2025-08-16 19:45:09C:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>CppTool.exe -e "ipconfig"
Executing command: ipconfigWindows IP 配置以太网适配器 以太网 2:媒体状态  . . . . . . . . . . . . : 媒体已断开连接连接特定的 DNS 后缀 . . . . . . . :以太网适配器 以太网 3:连接特定的 DNS 后缀 . . . . . . . :本地链接 IPv6 地址. . . . . . . . : fe80::c863:71e5:c8fd:a135%12IPv4 地址 . . . . . . . . . . . . : 192.168.112.98子网掩码  . . . . . . . . . . . . : 255.255.254.0默认网关. . . . . . . . . . . . . : 192.168.112.1无线局域网适配器 WLAN:媒体状态  . . . . . . . . . . . . : 媒体已断开连接连接特定的 DNS 后缀 . . . . . . . :无线局域网适配器 本地连接* 3:媒体状态  . . . . . . . . . . . . : 媒体已断开连接连接特定的 DNS 后缀 . . . . . . . :无线局域网适配器 本地连接* 12:媒体状态  . . . . . . . . . . . . : 媒体已断开连接连接特定的 DNS 后缀 . . . . . . . :以太网适配器 蓝牙网络连接:媒体状态  . . . . . . . . . . . . : 媒体已断开连接连接特定的 DNS 后缀 . . . . . . . :
Command executed successfullyC:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>
C:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>CppTool.exe -e "date /T"
Executing command: date /T
2025/08/16 周六
Command executed successfullyC:\Users\xxx\CLionProjects\hello0614\cmake-build-debug>
http://www.dtcms.com/a/445111.html

相关文章:

  • c 转网站开发重庆网站建设机构
  • 网站采用什么字体品牌网上授权
  • 自己建设购物网站宁波海曙网站建设
  • 移动网站开发服务花都网站建设公司天蝎信息
  • 广州微信网站制作北京运营推广网站建设
  • 全景网站制作网站怎么做才 吸引人
  • 网页制作网站设计稿杭州的电商网站建设
  • 网页制作与网站建设策划书案例昆明网站制作推荐
  • 网站建设域名的购买增城头条新闻
  • 网站缓存设置怎么做手机怎么开发软件app
  • 精英学校老师给学生做的网站wordpress企业主题模板
  • 手机网站开发实例成都网站建设 工资
  • 网站开发工具发展史活动汪活动策划网站
  • 怎么做信息采集的网站太原网站建设托管
  • 启东市住房城乡建设局网站免费造网站
  • 需要网站建设的是哪一类人太仓市建设招标网站
  • 天津市城乡建设网站网页制作基础教程使用spry构件
  • vi设计公司 焕识网站打开速度优化
  • 优秀网站推荐app设计网站推荐
  • 西安网站设计费用个人养老保险缴费查询
  • 网络推广和网站推广平台沈阳今天最新通知
  • 照片后期网站网站建立企业
  • 阿里云轻应用服务器 建设网站wordpress文章列表怎么折叠起来
  • 网站打开太慢什么原因怎么推广自己做的网站
  • 主题资源网站制作平台编程软件大全
  • 小程序app分销网站建设沧州手机建站哪家好
  • 网站后期的维护管理网络系统脆弱性的不安全因素
  • 如何建立内外网网站淮北专业三合一网站开发
  • 如何自己动手做网站深圳高端网站定制公司
  • 机械加工网站色彩搭配婚纱摄影网页制作