getopt 用法
-
a:: 说明不能带参数, b: 说明 -b 后面必须带参数:
while((opt = getopt(argc, argv, "p::s::f:a::b::c::d::e::g::h::i")) != -1) {
switch (opt) {
case 'p':
clsoe_watchdog();
args_correctly = 1;
break;
case 's':
open_watchdog();
args_correctly = 1;
break;
case 'f':
time = strtol(optarg, NULL, 0);
feed_watchdog(time);
args_correctly = 1;
break;
case 'a':
set_led_status_1(1);
args_correctly = 1;
break;
case 'b':
set_led_status_1(0);
args_correctly = 1;
break;
case 'c':
set_led_status_2(1);
args_correctly = 1;
break;
case 'd':
set_led_status_2(0);
args_correctly = 1;
break;
case 'e':
set_led_status_3(1);
args_correctly = 1;
break;
case 'g':
set_led_status_3(0);
args_correctly = 1;
break;
case 'h':
on_all_led();
args_correctly = 1;
break;
case 'i':
off_all_led();
args_correctly = 1;
break;
default:
args_correctly = 0;
}
}
Read The Fucking Source Code