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 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...
随机推荐
- UWP Xaml设计器中输入特殊字符
<TextBox Text="欢迎使用小冰科技最新研发的自然语言处理程序.小冰科技旗下还有强大的人脸识别软件——<微识别>,自动追踪和识别人脸:具有科普性质的.清新脱俗的识 ...
- STM8S——Analog/digital converter (ADC)
1.ADC1 and ADC2 are 10-bit successive approximation Anolog to Digital Converters. 所谓successive appro ...
- 【轮子狂魔】抛弃IIS,打造个性的Web Server - WebAPI/Lua/MVC(附带源码)
引言 此篇是<[轮子狂魔]抛弃IIS,向天借个HttpListener - 基础篇(附带源码)>的续篇,也可以说是提高篇,如果你对HttpListener不甚了解的话,建议先看下基础篇. ...
- vi/vim 常用命令 之 一图定天下!
直接上干活,一张图解决~
- java学习(五)Number类、Math类
Number类 顾名思义嘛,搞数字的,以前也用到过,就是相当于内置了一堆数字的类嘛,用哪种类型的就引用下这个包就好了呗 Integer.Long.Byte.Double.Float.Short都是Nu ...
- flask的继承和包含
为了方便使用重复的页面,我们也可以使用继承模板.还有包含模板,一般使用包含,俩个都不是很好理解,我只是用完的理解简单介绍一下,他们的用法打不相同,却又有类似之处 我们访问页面的时候在最上边会有导航的信 ...
- OpenGL 笔记<1> 固定管线实例 + 双缓存测试实例
欲以此分类来记录opengl的学习历程,此为第一篇,所以先来一个固定管线的例子,以及对双缓存的测试. 一.配置环境 写之前,先进行配置,然后再讲内容. 注:第一部分涉及的代码均忽略. [环境配置传送门 ...
- 【转】phpcms v9的ckeditor加入给内容调整行高
今天公司一客户要求一同事给ckeditor加入可以设置行高的功能(他后台是用织梦做的,他是织梦的FANS),我一时闲得慌,也想给咱家的v9加入这个功能,功夫不负有心啊,终于成功了,来给大家分享一下! ...
- 木马分析出现python语言,360的安全人员不禁感叹还有这种操作?
几年前,敲诈者木马还是一个默默无闻的木马种类.然而,由于其极强的破坏力和直接且丰厚的财富回报,敲诈者木马这几年已经一跃成为曝光率最高的木马类型——甚至超越了盗号木马.远控木马.网购木马这传统三强.与此 ...
- 【Unity Shader】Shader基础
目录 Chapter3 Unity Shader 基础 Chapter3 Unity Shader 基础 概述 在Unity需要材质(Material)与Unity Shader配合使用来达到满意的效 ...