如何解决C ++参数阅读器
我是C ++的新手,我需要一个程序的帮助。
程序应识别以下命令行开关:
-a或--alpha -b或--beta -g或--gamma -d或--delta -e或--epsilon 简短形式可以连接到同一参数中;但是,只有最后一个可以具有参数。简短格式参数可以或可以不由空格分隔。同一开关可以重复使用。
示例:
-abtext --gamma --delta“另一个文本” -e123 在这种情况下,程序应准确地产生以下输出:
-找到 -b参数为“文本” --gamma发现 --delta参数是“另一个文本” -e参数值为123
我已经完成了一些代码,但是我不知道如何完成它,因此它可以正常工作。
#define _CRT_SECURE_NO_WARNINGS
#include "Header.h"
#include <iostream>
#include <ranges>
#include <cstdlib>
namespace cv2a {
bool scan_arguments(const t_arg& parg)
{
for (auto&& x : parg)
{
//const_cast<std::string&>(x) += "aabb";
// std::cout << x << std::endl;
if (x[0] == '-')
{
//for (auto&& ch : std::ranges::subrange(x.begin() + 1,x.end()))
for (auto it = x.begin() + 1; it != x.end(); ++it)
//for (const char * p = x.c_str() + 1; *p != 0; ++ p)
//for (std::size_t i = 1; i < x.size(); ++i)
{
//auto&& ch = *p;
auto&& ch = *it;
//const char * ch = &x[i];
//const char& ch = x[i];
//ch = 'x';
switch (ch)
{
case 'a': std::cout << "-a found" << std::endl; break;
case 'b':
// "-xyzb<text>" "-xyzb" "<text>"
std::cout << "-b found" << std::endl;
// {b[a] ==} *(a+b) === a[b]
if (it + 1 != x.end())
{
std::cout << "\"" << x.substr(it + 1 - x.begin()) << "\"" << std::endl;
std::cout << "\"" << std::string(it + 1,x.end()) << "\"" << std::endl;
std::string tmp(it + 1,x.end());
std::cout << "\"" << tmp << "\"" << std::endl;
{
const char* p = tmp.c_str();
int v = std::atoi(p);
std::cout << v << std::endl;
char* p2;
auto w = std::strtol(p,&p2,0);
std::cout << w << std::endl;
if (*p2 != 0)
{
//*p2 = 'X';
std::cout << "chyba: \"" << p2 << "\"" << std::endl;
std::cout << "tmp: \"" << tmp << "\"" << std::endl;
}
}
std::size_t p2 = 0;
auto w = std::stoi(tmp,0);
std::cout << w << std::endl;
if (p2 != tmp.size())
{
std::cout << "chyba: \"" << tmp.substr(p2) << "\"" << std::endl;
}
int w2 = 0;
auto rv = std::sscanf(tmp.c_str(),"%f",&w2);
if (rv != 1)
{
std::cout << "chyba" << std::endl;
}
std::cout << w2 << std::endl;
// std::cout << *(it + 1);
// "-xyzb<text>"
it = x.end() - 1;
}
else
{
// "-xyzb" "<text>"
}
break;
case 'g': std::cout << "-g found" << std::endl; break;
case 'd': std::cout << "-d found" << std::endl; break;
case 'e': std::cout << "-e found" << std::endl; break;
default:
std::cerr << "UnkNown switch -" << ch << std::endl;
return false;
}
}
}
}
return true;
}
}
//////////////////////////////////////////////////////////
#include "Header.h"
#include <iostream>
int main(int argc,char** argv)
{
//cv2a::t_arg arg(argv + 1,argv + argc);
//auto rv = cv2a::scan_arguments(arg);
auto rv = cv2a::scan_arguments(cv2a::t_arg(argv + 1,argv + argc));
if (!rv)
{
std::cout << "Something wrong with the arguments" << std::endl;
return -1;
}
return 0;
}
/////////////////////////////////////////////////////////////////////
#ifndef cv2amodule_hpp_
#define cv2amodule_hpp_
#include <vector>
#include <string>
namespace cv2a {
using t_arg = std::vector< std::string>;
bool scan_arguments(const t_arg& parg);
}
#endif
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。