getopt_long(argc, argv, "", OPTIONS, NULL)分析

来源:互联网 发布:qt snmp 网络管理软件 编辑:程序博客网 时间:2024/06/03 07:46

static const struct option OPTIONS[] = {

{ "factory_test", no_argument, NULL, 'f'+'t'},

{ "send_intent", required_argument, NULL, 's' },

{ "update_image", required_argument, NULL, 'u' },

{ "recover_image", required_argument, NULL, 'r' }, 

{ "wipe_all", no_argument, NULL, 'w'+'a' },

{ "wipe_data", no_argument, NULL, 'w' },

}


while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1)
{

switch (arg) {

case 'p': previous_runs = atoi(optarg); break;

case 's': send_intent = optarg; break;

case 'u': update_image = optarg; break;

case 'r': recover_image = optarg; break;

case 'w': wipe_flags |= default_factory_reset;break;

case 'w'+'a': wipe_flags |= WIPE_ALL; break;

case '?':

LOGE("Invalid command argument\n");

continue;

}

}


上面贴的是android在recovery.c中的代码,当接收到参数时,在OPTION中寻找和参数相同的一项,之后getopt_long返回短选项,如"send_intent",返回“s”。

argc, argv
第一个参数是总共参数的数目,包括可执行文件本身
第二个参数是具体的参数 

getopt_long是用来解析参数的使用getopt_long(),需要引入头文件#include <getopt.h>

/gingerbread/bootable/recovery下有Android.mk,里面LOCAL_MODULE := recovery编译后生成recovery应用,

这个可执行文件会被最后编译成固件,如recovery.img烧写进板子

原创粉丝点击