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 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...
随机推荐
- python 小技巧之获取固定下面包含的某种类型文件的个数
遇到这样一个问题.我想要统计某个文件夹下有多少个py文件怎么办. 用python能解决吗?答案,能. 解决办法,使用glob 代码如下: import glob path_file_number=gl ...
- express的web server设置流程
对于express的设置,一直是拿来就用,只知其然,今天查了一下文档,记录详细过程如下. 1.实现基本常用功能需要的模块path 用来处理路径字符串拼接,设置模板路径和静态资源路径时使用cookie- ...
- ecCodes 学习 利用ecCodes Python API对GRIB文件进行读写
参考 https://www.ecmwf.int/assets/elearning/eccodes/eccodes2/story_html5.htmlhttps://confluence.ecmwf. ...
- java练习(一)数组、集合的运用
有这么一个有趣的问题,问:有这么一个不重复的自然数数组,自然数长度为N,而数组长度为N-2,依次随机把自然数放进数组中,请找出2个没有被放进去的自然数.例如:这个自然数数组是[0, 1, 2, 3, ...
- int str input的运用
- c语言数字图像处理(八):噪声模型及均值滤波器
图像退化/复原过程模型 高斯噪声 PDF(概率密度函数) 生成高斯随机数序列 算法可参考<http://www.doc.ic.ac.uk/~wl/papers/07/csur07dt.pdf&g ...
- Siki_Unity_3-3_背包系统
Unity 3-3 背包系统(基于UGUI) 任务1&2&3:演示.介绍.类图分析 背包面板.箱子面板.锻造合成面板.装备佩戴面板.商店面板等 面板的显示和隐藏.保存和加载.拾起物品. ...
- SICP读书笔记 2.4
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- docker私服搭建nexus3
docker私服搭建有官方的registry镜像,也有改版后的NexusOss3.x,因为maven的原因搭建了nexus,所以一并将docker私服也搭建到nexus上. nexus的安装过程就单独 ...
- maven:新建的maven工程需要添加一下插件
//下面的代码是改变maven的默认使用Java版本,本机是jdk1.7所以用1.7<plugins> <plugin> <groupId>org.apache.m ...