Code:
#include <stdio.h>
#include <unistd.h>
void process_options(int argc, char *argv[])
{
int oc;
char *tmp;
while((oc = getopt(argc, argv, "ngl:")) != -1)
{
switch(oc)
{
case 'n':
printf("My name is Lyong.\n");
break;
case 'g':
printf("Her name is Xxiong.\n");
break;
case 'l':
tmp = optarg;
printf("Our love is %s\n", tmp);
break;
default:
printf("arguments error!\n");
break;
}
}
}
int main(int argc, char *argv[])
{
process_options(argc, argv);
return 0;
}
Result:
main_parameter_sample -n -l 99
My name is Lyong.
Our love is 99
沒有留言:
張貼留言