ACE Get Opt解析命令列

2021-06-08 12:42:47 字數 934 閱讀 4066

ace_get_opt;類可以解析兩種命令列選項:

a:簡單的單字元單選項,以單短劃線("-")開頭;

b:長選項,以雙短劃線("--")開頭;比如--prefix;

ace_get_opt類的例項使用operator()來迭代命令列選項;如果遇到單選項字元時,它直接返回該字元;如果遇到長選項時,它返回與該長選項等價的短選項字元;選項的引數通過opt_arg()方法返回;如果命令列所有選項都處理完畢,operator()返回eof;

cmd_opts.long_option(ace_text("cool_option"));

這條語句增加乙個長選項--cool_option,並規定,如果命令列上指定了長選項--cool_option,那麼operator()就返回0.

cmd_opts.long_option(ace_text("the_answer"),42);

這條語句增加了乙個長選項--the_answer,並規定,如果operator()在命令列上遇到長選項--the_anwser,就要返回整數值42;

#include "ace/os_ns_string.h"

#include "ace/get_opt.h"

#include "ace/log_msg.h"

//此示例演示長引數開關的使用,你可以用'program -config my.conf'//或用'program --config my.conf'

//這樣形式的用法和 'program -f my.conf'是等價的

intace_tmain (int argc, ace_tchar *argv)

ace_debug ((lm_debug, ace_text ("config file is %s\n"), config_file));

return 0;

}執行結果演示

config file is my.conf

利用ACE Get Opt解析命令

ace get opt通常用來解析程式的命令列引數,也可結合ace argv類解析其他的字串命令.ace tchar szcmd ace text ping l 100 n 25 w 10000 分解字串為引數向量,ace argv的argc 成員函式返回向量數目,argv 返回向量陣列 ace a...

利用ACE Get Opt解析命令

ace get opt通常用來解析程式的命令列引數,也可結合ace ar 類解析其他的字串命令.ace tchar szcmd ace text ping l 100 n 25 w 10000 分解字串為引數向量,ace ar 的argc 成員函式返回向量數目,ar 返回向量陣列 ace ar ar...

命令列解析

不想自已分析命令列的話也可以 lpwstr lpwargv lpwargv commandlinetoargvw getcommandlinew dwcount dwcount的值即為命令列引數的個數 像控制台程式一樣,引數也包括本執行檔案的全路徑 lpwargv為乙個lpwstr 的指標,需要按如...