ACE_Get_Opt函数笔记
#include "ace/OS_NS_string.h" #include "ace/Configuration.h" #include "ace/Configuration_Import_Export.h" #include "ace/Get_Opt.h" #include "ace/Log_Msg.h" #include "ace/INET_Addr.h" #include "ace/Service_Object.h" class HA_Status: public ACE_Service_Object { public: virtual int init(int argc, ACE_TCHAR *argv[]); private: ACE_INET_Addr listen_addr_; }; int HA_Status::init(int argc, ACE_TCHAR *argv[]) { // Listing 1 code/ch04 // define the valid options, A colon following an option letter means // the options requires an arguemnt. Use a double colon if the argument // is optional. examp: "a:b:cd::e" has "-a ,-b , -c ,-d, -e". static const ACE_TCHAR options[] = ACE_TEXT(":f:"); ACE_Get_Opt cmd_opts(argc, argv, options); // use the long_option()method to equate a long option string // with one of the short options; if (cmd_opts.long_option(ACE_TEXT("config"),'f', ACE_Get_Opt::ARG_REQUIRED) == -1) return -1; int option; ACE_TCHAR config_file[MAXPATHLEN]; ACE_OS::strcpy(config_file, ACE_TEXT("HAStatus.conf")); // Use operator() (or the get_opt() method) to iterate through the // command line options.It returns the short option character when // located, and the short option equivalent when a long option is processed. // operator() returns EOF when all the options have been processed. while((option = cmd_opts()) != EOF) switch (option) { case 'f': // The option's argument is accessed via the opt_arg() method. ACE_OS::strncpy(config_file, cmd_opts.opt_arg(), MAXPATHLEN); break; case ':': //If it is a ':',get_opt() return a ':' when a required argument is missing. ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("-%c requires an argument\n"), cmd_opts.opt_opt()), -1); default: ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("Parse error.\n")), -1); } // Listint 1 // Listint 2 code/ch04 ACE_Configuration_Heap config; if (config.open() == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("config")), -1); ACE_Registry_ImpExp config_importer(config); if (config_importer.import_config(config_file) == -1) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%p\n"), config_file), -1); ACE_Configuration_Section_Key status_section; if (config.open_section(config.root_section(), ACE_TEXT("HAStatus"), 0, status_section) == -1) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%p\n"), ACE_TEXT("Can't open HAStatus sections")), -1); u_int status_port; if (config.get_integer_value(status_section, ACE_TEXT("ListenPort"), status_port) == -1) ACE_ERROR_RETURN((LM_ERROR, "HAStatus ListenPort does not exist\n"), -1); this->listen_addr_.set(static_cast<u_short> (status_port)); return 0; } int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { HA_Status status; status.init(argc, argv); return 0; } /* ACE_Get_Opt (int argc, ACE_TCHAR **argv, const ACE_TCHAR *optstring, int skip_args = 1, int report_error = 0, int ordering = PERMUTE_ARGS, int long_only = 0); # 1.start parsing at an arbitrary index ACE_Get_Opt can be directed to start processing the argument vector at an arbi- trary point specified by the skip_args parameter. The default value is 1, which causes ACE_Get_Opt to skip argv[0] (traditionally, the program name) when parsing a command line passed to main(). When using ACE_Get_Opt to parse options received when initializing a dynamic service skip_args is often specified as 0, because arguments passed to services initialized via the ACE Service Configurator framework start in argv[0]. # 2.Report errors while parsing By default, ACE_Get_Opt is silent about parsing errors; it simply returns the appropriate value from operator() (or the get_opt() method), allowing your application to handle and report errors in the most sensible way. If, however, you’d rather have ACE_Get_Opt display an error message when it detects an error in the specified argument vector, the constructor’s report_errors argu- ment should be non-zero. In this case, ACE_Get_Opt will use ACE_ERROR with the LM_ERROR severity to report the error. # 3.Alternate long option specification If "W" is included in the options definitions string, ACE_Get_Opt treats -W similarly to --. For example, -W foo will be parsed the same as --foo. This can be useful when manipulating argument vectors to change parameters into long options by inserting an element with -W instead of inserting -- on an existing element. # 4.About argument ordering Some applications require you to specify all options at the beginning of the command line, while others allow you to mix options and other non-option tokens (such as file names). ACE_Get_Opt supports selection of use cases defined by enumerators defined in ACE_Get_Opt. ## 1.ACE_Get_Opt::PERMUTE_ARGS: As the argument vector is parsed, the elements are dynamically rearranged so that those with valid options(and their arguments) appear at the front of the argument vector, in their original relative ordering. Non-option elements are placed after the option elements. They can be processed by some other part of your system, or processed as known non-options. When operator() returns EOF to indicate the end of options, opt_ind() returns the index to the first non-option element in the argumet vector. ## 2.ACE_Get_Opt::REQUIRED_ORDER: The argument vector is not reordered and all options and their their arguments must be at the front of the argument vector. If a non-option element is encountered, operator() returns EOF; opt_ind() returns the index of the non-option element. (What is the non-option element?) ## 3.ACE_Get_Opt::RETURN_IN_ORDER: The argument vector is not reordered. Any non-option element causes operator() to return 1 and the actual element is accessible via the opt_arg() method. This mode is useful for situations in which options and other arguments can be specified in any order and in which the relative ordering makes a difference. This is a situation where it may be useful to parse options, examine non-options, and and continue parsing after the non-options using the skip_args argument to specify the new starting point. ## The other two method to change the argument order 1. If the POSIXLY_CORRECT environment variable is set, the ordering mode is set to REQUIRE_ORDER 2. A + or - character at the beginning of the options string. + changes the ordering mode to REQUIRE_ORDER; - changes it to RETURN_IN_ORDER. If both are at the start of the options string, the last ont is used. # 5.Long optins only If the long_only parameter to the ACE_Get_Opt constructor is non-zero, command line tokens that begin with a single - are checked as long options. For example, if the long_only argument were set to 1, the user could type either --config or -config. */ /* # ACE_Configuration is a class that defines the configuration interface for the following two classes available for accessing and manipulating configuration information: ## 1. ACE_Configuration_Heap is available on all platforms. It keeps all information in memory. The memory allocation can be customized to use a persistent backing store, but the most common use is with dynamically-allocated heap memory. ## 2. ACE_Configuration_Win32Registry is available only on Windows. It implements the ACE_Configuration interface to access and manipulate information in the Windows registry. # Configuration data is organized hierarchically in sections, analogous to a filesystem directory tree. Each configuration object contains a root section that has no name, similar to the filesystem root in UNIX.All other sections are created hierar- chically beneath the root section and are named by the application. Sections can be nested to an arbitrary depth. # Configuration Backing Stores ## 1. ACE_Registry_ImpExp uses a text format that includes type information with each value. This allows type information to be preserved across export/import, even on machines with different byte orders. ## 2. ACE_Ini_ImpExp uses the older Windows "INI" file format which does not have type information associated with the values. Therefore, configuration data exported using ACE_Ini_ImpExp is always imported as string data, regardless of the original type. */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗