关闭

getopt_long使用方法

110人阅读 评论(0) 收藏 举报
分类:
看开源代码  感觉这以后总会用到,写的也简洁
static const char* shortopts = "p:o:m:d:g:b:a:r:x:l:sv";

enum { OPT_HELP = 1, OPT_VERSION, OPT_VALIDATE, OPT_EDGESTATS, OPT_EXACT, OPT_MAXINDEL, OPT_TR, OPT_MAXEDGES };

static const struct option longopts[] = { 
    { "verbose",               no_argument,       NULL, 'v' }, //NULL 表示getopt_long返回val('v')
    { "out-prefix",            required_argument, NULL, 'o' },
    { "min-overlap",           required_argument, NULL, 'm' },
    { "bubble",                required_argument, NULL, 'b' },
    { "cut-terminal",          required_argument, NULL, 'x' },
    { "min-branch-length",     required_argument, NULL, 'l' },
    { "resolve-small",         required_argument, NULL, 'r' },
    { "max-divergence",        required_argument, NULL, 'd' },
    { "max-gap-divergence",    required_argument, NULL, 'g' },
    { "max-indel",             required_argument, NULL, OPT_MAXINDEL },
    { "max-edges",             required_argument, NULL, OPT_MAXEDGES },
    { "smooth",                no_argument,       NULL, 's' },
    { "transitive-reduction",  no_argument,       NULL, OPT_TR },
    { "edge-stats",            no_argument,       NULL, OPT_EDGESTATS },
    { "exact",                 no_argument,       NULL, OPT_EXACT },
    { "help",                  no_argument,       NULL, OPT_HELP },
    { "version",               no_argument,       NULL, OPT_VERSION },
    { "validate",              no_argument,       NULL, OPT_VALIDATE},
    { NULL, 0, NULL, 0 } 
};

for (char c; (c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1;) //不NULL 则返回longopts下表
    {
        std::istringstream arg(optarg != NULL ? optarg : "");//optarg 指向需要val的key 对应的val
        switch (c)
        {
            case 'o': arg >> prefix; break;
            case 'm': arg >> opt::minOverlap; break;
            case '?': die = true; break; //需要val 没有传 或 参数不是 -h --help 函数出错返回?
            case 'v': opt::verbose++; break;
            case 'l': arg >> opt::trimLengthThreshold; break;
            case 'b': arg >> opt::numBubbleRounds; break;
            case 'd': arg >> opt::maxBubbleDivergence; break;
            case 'g': arg >> opt::maxBubbleGapDivergence; break;
            case 's': opt::bSmoothGraph = true; break;
            case 'x': arg >> opt::numTrimRounds; break;
            case 'r': arg >> opt::resolveSmallRepeatLen; break;
            case OPT_MAXEDGES: arg >> opt::maxEdges; break;
            case OPT_TR: opt::bPerformTR = true; break;
            case OPT_MAXINDEL: arg >> opt::maxIndelLength; break;
            case OPT_EXACT: opt::bExact = true; break;
            case OPT_EDGESTATS: opt::bEdgeStats = true; break;
            case OPT_VALIDATE: opt::bValidate = true; break;
            case OPT_HELP:
                std::cout << ASSEMBLE_USAGE_MESSAGE;
                exit(EXIT_SUCCESS);
            case OPT_VERSION:
                std::cout << ASSEMBLE_VERSION_MESSAGE;
                exit(EXIT_SUCCESS);

        }
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11907次
    • 积分:549
    • 等级:
    • 排名:千里之外
    • 原创:46篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类