C++基于Boost库实现命令行解析

时间:2022-09-19 13:37:41

第一次尝试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <boost/program_options.hpp>
 
// 定义命名空间
namespace opt = boost::program_options;
 
int main(int argc, char const *argv[])
{
    opt::options_description desc("Usage: 32位端口快速扫描器\n\n options: \n");
 
    desc.add_options()
        ("Address,a", opt::value<std::string>()->default_value("127.0.0.1"), "输入一个IP地址"),
        ("StartPort,s", opt::value<int>()->default_value(1024), "传入扫描起始端口"),
        ("EndPort,e", opt::value<int>()->default_value(65535), "传入扫描结束端口"),
        ("Help,h", "弹出帮助菜单");
 
    // 解析参数将值传递给virtual_map
    opt::variables_map virtual_map;
 
    try
    {
        opt::store(opt::parse_command_line(argc, argv, desc), virtual_map);
    }
    catch (...)
    {
        std::cout << "error \n";
        return 0;
    }
 
    // 参数解析完毕,处理
    if (virtual_map.count("Help"))
    {
        printf("帮助");
    }
    if (virtual_map.count("Address"))
    {
        std::cout << "找到" << virtual_map["Address"].as<std::string>() << std::endl;
    }
    if (virtual_map.empty())
    {
        std::cout << "no options\n";
    }
    return 0;
}

第二次尝试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream> 
#include <vector> 
#include <string>
#include <boost/program_options.hpp> 
 
namespace opt = boost::program_options;
 
int main(int argc, char const *argv[])
{
    int start_port = 1024, end_port = 65535;
    std::vector<std::string> address;
 
    opt::options_description opt("\nUsage: 32位端口快速扫描器 Ver:1.0 \n\n options: \n");
    opt.add_options()
        ("address,a", opt::value<std::vector<std::string> >()->multitoken(), "指定地址")
        ("start_port,s", opt::value<int>(&start_port)->default_value(1024), "开始端口")
        ("end_port,e", opt::value<int>(&end_port)->default_value(65535), "结束端口")
        ("help", "帮助菜单");
 
    opt::variables_map vm;
    try
    {
        opt::store(parse_command_line(argc, argv, opt), vm);
    }
    catch (...){
        std::cout << "command error!\n";
        return 0;
    }
 
    opt::notify(vm);
    if (vm.count("help"))
    {
        std::cout << opt << std::endl;
        return 0;
    }
    if (vm.count("address") && vm.count("start_port") && vm.count("end_port"))
    {
        //遍历选项值 
        for (auto& str : vm["address"].as<std::vector<std::string> >())
            std::cout << str << " ";
        int x = vm["start_port"].as<int>();
        std::cout << x << std::endl;
    }
    return 0;
}

最终版

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <boost/program_options.hpp>
 
namespace opt = boost::program_options;
 
int main(int argc, char const *argv[])
{
    opt::options_description des_cmd("\n Usage: 32位端口快速扫描器 Ver:1.0 \n\n Options: \n");
    des_cmd.add_options()
        ("address,a", opt::value<std::string>()->default_value("127.0.0.1"), "指定地址")
        ("start_port,s", opt::value<int>()->default_value(1024), "开始端口")
        ("end_port,e", opt::value<int>()->default_value(65535), "结束端口")
        ("help,h", "帮助菜单");
 
    opt::variables_map virtual_map;
    try
    {
        opt::store(opt::parse_command_line(argc, argv, des_cmd), virtual_map);
    }
    catch (...){ return 0; }
    
    // 定义消息
    opt::notify(virtual_map);
    
    // 无参数直接返回
    if (virtual_map.empty())
    {
        return 0;
    }
    else if (virtual_map.count("help") || virtual_map.count("h"))
    {
        std::cout << des_cmd << std::endl;
        return 0;
    }
    else if (virtual_map.count("address") && virtual_map.count("start_port") && virtual_map.count("end_port"))
    {
        std::cout << "Addr = " << virtual_map["address"].as<std::string>() << std::endl;
        std::cout << "StartPort = " << virtual_map["start_port"].as<int>() << std::endl;
        std::cout << "EndPort = " << virtual_map["end_port"].as<int>() << std::endl;
    }
    else
    {
        std::cout << "option error" << std::endl;
    }
    return 0;
}

命令行下使用help输出帮助菜单,当传入三个参数时,即可解析到第二个判断上,执行相应的函数即可。

C++基于Boost库实现命令行解析

文章出处:https://www.cnblogs.com/lyshark

以上就是C++基于Boost库实现命令行解析的详细内容,更多关于C++ Boost库实现命令行解析的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/LyShark/p/14454024.html