函数option动态解析

本文探讨如何在Shell中为函数实现类似ls命令那样支持多种选项的功能。通过示例代码options.def, options.c 和 main.c,阐述一种实现多选项支持的思路。" 103570512,8704900,Win10环境下配置PyTorch-GPU与CUDA、cuDNN详细指南,"['GPU驱动', 'CUDA', 'cuDNN', 'PyTorch', '深度学习环境']
摘要由CSDN通过智能技术生成

在shell中有些命令如ls 等会支持很多option。那么是如何实现的呢?下面提供了一种给函数实现类似多options支持的思路代码。

options.def

DEF_OPTION(height,int,0,"Height of the input,default = 0")
DEF_OPTION(width,int,0,"Width of the input, default = 0 ")
DEF_OPTION(depth,int,3,"Depth of the input, default = 3")

options.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static inline string handle_option_string(const char *arg) { return arg; }
static inline int handle_option_int(const char *arg) { return atoi(arg); }
static inline float handle_option_float(const char *arg) { return atof(arg); }

int do_option(struct options *options, int argc, const char **argv, int i) {
#define DEF_OPTION(NAME,TYPE,DEFAULT,...) \
	if (0==strcmp("--" #NAME,argv[i])) options->NAME = handle_option_##TYPE(argv[i+1]);
#include "options.def"
#unde
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值