说好不造轮子的,就管不住这手

#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)的更多相关文章

  1. C语言中使用库函数解析命令行参数

    在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...

  2. boost之program_options库,解析命令行参数、读取配置文件

    一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...

  3. optparse模块解析命令行参数的说明及优化

    一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...

  4. python解析命令行参数

    常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...

  5. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  6. Windows下解析命令行参数

    linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...

  7. 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数

     1  新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...

  8. 使用 Apache Commons CLI 解析命令行参数示例

    很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...

  9. Shell 参数(2) --解析命令行参数工具:getopts/getopt

    getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...

  10. getopt_long函数解析命令行参数

    转载:http://blog.csdn.net/hcx25909/article/details/7388750 每一天你都在使用大量的命令行程序,是不是感觉那些命令行参数用起来比较方便,他们都是使用 ...

随机推荐

  1. Form,选择并转移导航菜单

    1.代码实例 <!DOCTYPE html> <html> <head> <title>选择并转移导航菜单</title> <meta ...

  2. 为什么 Action/ViewController/ProperttyEditor不可见或不可用?

    英文版:https://documentation.devexpress.com/eXpressAppFramework/112818/Concepts/Extend-Functionality/De ...

  3. react.js插件开发,x-dailog弹窗浮层组件

    react.js插件开发,x-dailog弹窗浮层组件 我认为,每一个组件都应该有他自带的样式和属性事件回调配置.所以我会给x-dialog默认一套简单的样式,和各种默认的配置项.所有react插件示 ...

  4. Unity Shader 学习之旅

    Unity Shader 学习之旅 unityshader图形图像 纸上学来终觉浅,绝知此事要躬行 美丽的梦和美丽的诗一样 都是可遇而不可求的——席慕蓉 一.渲染流水线 示例图 Tips:什么是 GP ...

  5. Leetcode_3. Find the longest substring without repeating characters

    3. Find the longest substring without repeating characters Given a string, find the length of the lo ...

  6. Node.js中module文件定义的top-level变量为何是私有的

    在Node.js中,module文件里面使用var,const或者let定义的top-level变量为何是私有的,只能在这个模块文件中使用呢? 原因就是,在模块文件中的内容执行之前,node.js会降 ...

  7. 【探路者】Beta发布用户使用报告

    用户数量:18 一.用户列表及评论.  用户序号 用户来源 用户下载软件途径 用户姓名 用户描述(信息) 使用次数 用户评价 1  张恩聚  QQ发送可运行jar包  周楠  吉林大学在读研究生  5 ...

  8. 2017年软件工程第十二次作业-PSP总结报告

    回顾1 1.回想一下你曾经对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你认为过去接触到的课程是否符合你对计算机专业的期待,为什么?经过一个学期 ...

  9. 20162325 金立清 S2 W3 C13

    20162325 2017-2018-2 <程序设计与数据结构>第3周学习总结 教材学习内容概要 查找是在一组项内找到指定目标或是确定目标不存在的过程 高效的查找使得比较的次数最少 Com ...

  10. iOS 开发学习-类的创建与实现,与java语言的对比

    Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { //在{}中定义属性(全局变量/实例变量 ...