命令参数的解析

[复制链接]
 楼主| keer_zu 发表于 2020-6-15 16:01 | 显示全部楼层 |阅读模式
TI, TE, IO, ST, ratio
本帖最后由 keer_zu 于 2020-6-16 09:21 编辑

刚写了一个命令参数解析的程序,使用getopt。

主要代码如下:

  1. #include <stdio.h>
  2. #include <string>
  3. #include <map>
  4. #include <stdlib.h>

  5. #include "operation.h"

  6. using namespace std;
  7. std::map<char,operation_context_t> operation_contexts;

  8. #define no_argument 0
  9. #define required_argument 1
  10. #define optional_argument 2


  11. struct option *options = NULL;

  12. static int operation_p(char *arg)
  13. {
  14.     TestItemManager &tm = (TestItemManager&)TestItemManager::GetInstance();
  15.     tm.TestItemPrint();
  16. }

  17. static int operation_test(char *arg)
  18. {
  19.     TestItemManager &tm = (TestItemManager&)TestItemManager::GetInstance();
  20.     tm.TestSpecific(arg);
  21. }

  22. static int operation_a(char *arg)
  23. {
  24.     TestItemManager &tm = (TestItemManager&)TestItemManager::GetInstance();
  25.     tm.TestAll();
  26. }



  27. void operation_contexts_init()
  28. {
  29.         int i = 0;
  30.         char opt;
  31.         opt = 'p';
  32.         operation_contexts.insert(pair<char,operation_context_t>(opt,{.m_option={.name = "p",.has_arg=no_argument,.flag=NULL,.val=opt},.operation=operation_p}));
  33.         opt = 't';
  34.         operation_contexts.insert(pair<char,operation_context_t>(opt,{.m_option={.name = "test",.has_arg=required_argument,.flag=NULL,.val=opt},.operation=operation_test}));
  35.         opt = 'n';
  36.         operation_contexts.insert(pair<char,operation_context_t>(opt,{.m_option={.name = NULL,.has_arg= 0,.flag=NULL,.val=opt},.operation=operation_null}));

  37.         options = (struct option *)malloc(sizeof(struct option) * operation_contexts.size());
  38.         if(options != NULL) {
  39.                 map<char,operation_context_t>::reverse_iterator iter;
  40.                 for(iter = operation_contexts.rbegin(); iter != operation_contexts.rend(); iter++) {

  41.                         options[i].name = ((operation_context_t)(iter->second)).m_option.name;
  42.                         options[i].has_arg = ((operation_context_t)(iter->second)).m_option.has_arg;
  43.                         options[i].flag = ((operation_context_t)(iter->second)).m_option.flag;
  44.                         options[i].val = ((operation_context_t)(iter->second)).m_option.val;
  45.                         i ++;
  46.                 }
  47.         }
  48. }

  49. int run_operation(char opt,char *arg)
  50. {
  51.         map<char,operation_context_t>::iterator iter;  
  52.   
  53.     iter = operation_contexts.find(opt);  
  54.   
  55.     if(iter != operation_contexts.end())  {
  56.                 return operation_contexts[opt].operation(arg);
  57.     }
  58. }



使用用例:

  1. #include <string>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <getopt.h>
  5. #include "operation.h"


  6. extern struct option *options;

  7. int main(int argc, char *argv[])
  8. {
  9.     int opt;
  10.     int digit_optind = 0;
  11.     int option_index = 0;
  12.     std::string str = "a::b:c:d";

  13.         operation_contexts_init();
  14.         
  15.         if(options != NULL) {
  16.                 while((opt = getopt_long_only(argc,argv,str.c_str(),options,&option_index))!= -1) {  
  17.                 run_operation(opt,optarg);
  18.             }  
  19.         }
  20. }


@yyy71cj @dirtwillfly



 楼主| keer_zu 发表于 2020-6-16 09:20 | 显示全部楼层
测试项:
头文件
  1. #ifndef __TEST_ITEM_H__
  2. #define __TEST_ITEM_H__

  3. #include <string>
  4. #include <map>


  5. ///////////////////////////////////////// test item /////////////////////////////////////////
  6. class TestItem
  7. {
  8. public:
  9.         virtual int Test() = 0;
  10. protected:
  11.         std::string m_itemName;
  12. };


  13. ///////////////////////////// ublox ///////////////////////////////

  14. class TestUblox : public TestItem
  15. {
  16. public:
  17.         TestUblox(std::string itemName);
  18.         int Test();
  19. };


  20. ///////////////////////////// emmc ///////////////////////////////

  21. class TestEmmc : public TestItem
  22. {
  23. public:
  24.         TestEmmc(std::string itemName);
  25.         int Test();
  26. };

  27. ///////////////////////////// can ///////////////////////////////

  28. class TestCan : public TestItem
  29. {
  30. public:
  31.         TestCan(std::string itemName);
  32.         int Test();
  33. };


  34. ////////////////////////////////////////// item manager ///////////////////////////////////////

  35. class TestItemManager {
  36. public:
  37.         static TestItemManager& GetInstance();
  38.         TestItemManager();

  39.         int TestAll();
  40.         int TestSpecific(std::string itemName);
  41.         int TestItemPrint();
  42. private:
  43.         std::map<std::string,TestItem*> m_testItems;
  44. };

  45. #endif



.cpp文件:

  1. #include <iostream>
  2. #include "test_item.h"


  3. using namespace std;

  4. ///////////////////////////////////////// TestUblox ///////////////////////////////////////////////
  5. TestUblox::TestUblox(string itemName)
  6. {
  7.         m_itemName = itemName;
  8. }

  9. int TestUblox::Test()
  10. {
  11.         cout << "== ublox test" << endl;
  12. }

  13. ///////////////////////////////////////// TestEmmc ////////////////////////////////////////////////
  14. TestEmmc::TestEmmc(string itemName)
  15. {
  16.         m_itemName = itemName;
  17. }

  18. int TestEmmc::Test()
  19. {
  20.         cout << "== emmc test" << endl;
  21. }


  22. ///////////////////////////////////////// TestCan ////////////////////////////////////////////////
  23. TestCan::TestCan(string itemName)
  24. {
  25.         m_itemName = itemName;
  26. }

  27. int TestCan::Test()
  28. {
  29.         cout << "== can test" << endl;
  30. }


  31. ////////////////////////////////////////// item manager ///////////////////////////////////////

  32. TestItemManager::TestItemManager()
  33. {
  34.         string itemName;
  35.        
  36.         itemName = "ublox";
  37.         m_testItems.insert(pair<string,TestItem*>(itemName,new TestUblox(itemName)));
  38.        
  39.         itemName = "emmc";
  40.         m_testItems.insert(pair<string,TestItem*>(itemName,new TestEmmc(itemName)));
  41.        
  42.         itemName = "can";
  43.         m_testItems.insert(pair<string,TestItem*>(itemName,new TestCan(itemName)));
  44.        
  45. }


  46. TestItemManager& TestItemManager::GetInstance()
  47. {
  48.         static TestItemManager t_inst;
  49.         return t_inst;
  50. }


  51. int TestItemManager::TestAll()
  52. {
  53.         map<string,TestItem*>::iterator iter;
  54.         for(iter = m_testItems.begin(); iter != m_testItems.end(); iter++) {
  55.                 ((TestItem*)(iter->second))->Test();
  56.         }
  57. }

  58. int TestItemManager::TestSpecific(string itemName)
  59. {
  60.         map<string,TestItem*>::iterator iter;  
  61.   
  62.     iter = m_testItems.find(itemName);  
  63.   
  64.     if(iter != m_testItems.end())  {
  65.                 return m_testItems[itemName]->Test();
  66.     }
  67. }

  68. int TestItemManager::TestItemPrint()
  69. {
  70.         map<string,TestItem*>::iterator iter;
  71.         for(iter = m_testItems.begin(); iter != m_testItems.end(); iter++) {
  72.                 cout << iter->first << " ";
  73.         }

  74.         cout << endl;
  75. }




您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1477

主题

12909

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1477

主题

12909

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部