C++解析命令行参数(仿C语言args)
说好不造轮子的,就管不住这手
#include <cstdio>
#include <string>
#include <vector> bool ParseChar( const std::string& buf, std::size_t& pos, char& ch, bool& escape )
{
char c; if ( pos == buf.length() )
return false; // Get the character to parse
c = buf.at( pos++ ); if ( c == '\\' )
{
// Parse the escape character if ( pos != buf.length() )
{
// Get the character to escape
c = buf.at( pos++ ); if ( c == '\\' || c == '"' )
{
ch = c;
escape = true;
}
else
{
// Does not support the character, just hold the '\\' character
// We need move the POS back to prepare for the character parsing
pos--;
ch = '\\';
escape = false;
}
}
else
{
// We can't get the character to escape
// Just hold the '\\' character
ch = c;
escape = false;
}
}
else
{
// Copy the character ch = c;
escape = false;
} return true;
} bool ParseToken( const std::string& buf, std::size_t& pos, std::string& token )
{
char c {};
bool escape {};
bool quote {}; // True if parsing a string
bool doing {}; // True if parsing has started // Skip blank characters, if any
while ( pos != buf.length() )
{
c = buf.at( pos ); if ( c != ' ' && c != '\t' )
break; pos++;
} // Clean up the token
token.clear(); while ( ParseChar( buf, pos, c, escape ) )
{
if ( !doing )
{
// Parse the first character if ( c == '"' && !escape )
{
// Just mark the beginning of the string, don't copy it
quote = true;
}
else
{
// Copy the first character of the token
token.push_back( c );
} // '\n' is a single character token
if ( c == '\n' )
return true; // We have parsed any one character, the parsing has started
doing = true;
}
else
{
if ( quote )
{
// Copying the character of the string here if ( c == '"' && !escape )
{
// Mark the ending of a string
return true;
}
else
{
// Copy the character of the string
token.push_back( c );
}
}
else
{
// Copying the character of the token here if ( c == '"' && !escape )
{
// We accidentally encounter a string beginning mark before the token finished
// We need to finish the token and move the POS back to prepare for the string parsing
pos--;
return true;
} if ( c == '\n' )
{
// We accidentally encounter a '\n' before the token finished
// We need to finish the token and move the POS back to prepare for the '\n' parsing
pos--;
return true;
} if ( c == ' ' || c == '\t' )
{
// Mark the ending of a string
return true;
}
else
{
// Copy the character of the token
token.push_back( c );
}
}
}
} // If no any characters are parsed, we are at the end of the buffer
// returns 'false' to finish the parsing return doing;
} int main()
{
std::string cmdline = R"( connect
spawn
name "Billy Herrington"
flags 0xffff ""
desc "boy \\next\" door" )"; std::size_t pos {};
std::string token {}; while ( ParseToken( cmdline, pos, token ) )
{
printf_s( "[%s]\n", token == "\n" ? "\\n" : token.c_str() );
} return ;
}
输出如下
[connect]
[\n]
[spawn]
[1]
[\n]
[name]
[Billy Herrington]
[\n]
[flags]
[0xffff]
[]
[\n]
[desc]
[boy \next" door]
我对换行符做了点特殊处理,如果不需要可以删掉相关的判断代码
C++解析命令行参数(仿C语言args)的更多相关文章
- C语言中使用库函数解析命令行参数
在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...
- boost之program_options库,解析命令行参数、读取配置文件
一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...
- optparse模块解析命令行参数的说明及优化
一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...
- python解析命令行参数
常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...
- linux 中解析命令行参数(getopt_long用法)
linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...
- Windows下解析命令行参数
linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...
- 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数
1 新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...
- 使用 Apache Commons CLI 解析命令行参数示例
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...
- Shell 参数(2) --解析命令行参数工具:getopts/getopt
getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...
- getopt_long函数解析命令行参数
转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...
随机推荐
- rabbitMQ的三种路由模式
rabbitMQ工作流程: 1.声明交换机 2.声明消息队列 3.绑定交换机和队列 4.生产者往交换机里发送新消息 5.交换机根据所选的模式和routingKey决定消息发往哪条消息队列 6.一个消费 ...
- 对最近java基础学习的一次小结
开头想了3分钟,不知道起什么名字好,首先内容有点泛,但也都是基础知识. 对之前所学的java基础知识做了个小结,因为我是跟着网上找的黑马的基础视频看跟着学的,10天的课程硬生生给我看了这么久,也是佛了 ...
- 简单的redis工具类
import java.util.Arrays; import java.util.List;import java.util.Set; import org.apache.commons.lang. ...
- 维诺图(Voronoi Diagram)分析与实现(转)
一.问题描述1.Voronoi图的定义又叫泰森多边形或Dirichlet图,它是由一组由连接两邻点直线的垂直平分线组成的连续多边形组成. 2.Voronoi图的特点(1)每个V多边形内有一个生成元: ...
- Datawhale MySQL 训练营 Task3 表操作
目录 学习内容 1.MySQL 表数据类型 2. 用SQL语句创建表 3. 用SQL语句向表中添加数据 4. 用SQL语句删除表 5. 用SQL语句修改表 作业 参考链接 学习内容 1.MySQL 表 ...
- 温习DL之一:梯度的概念
1.梯度的概念 梯度是一个矢量,表示某一函数在该点处的方向导数沿着该方向取得最大值,即函数在该点处沿着该方向变化最快. 在微积分里面,对多元函数的参数求∂偏导数,把求得的各个参数的偏导数以向量的形式写 ...
- Visual Studio Code搭建NodeJs的开发环境
一.Visual Studio Code搭建NodeJs的开发环境 1.下载安装NodeJs并配置环境变量 可以参考:NodeJs的安装和环境变量配置 2.下载安装 VS Code编辑器 可以参考:V ...
- [SimHash] the Hash-based Similarity Detection Algorithm
The current information explosion has resulted in an increasing number of applications that need to ...
- md5sum命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/cbbbc/article/details/48563023 前言 在网络传输.设备之间转存.复制大文件等时,可能会出现传输 ...
- python的多路复用实现聊天群
在我的<python高级编程和异步io编程>中我讲解了socket编程,这里贴一段用socket实现聊天室的功能的源码,因为最近工作比较忙,后期我会将这里的代码细节分析出来,目前先把代码贴 ...