Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
$ gcc opt.c -o opt $ sudo chmod +x opt $ ./opt -a 2 -b 5 -o '*' 2 * 5 = 10
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
void usage(char *name)
{
printf("usage: %s\n \
\t-h this message\n \
\t-c [config file]\n \
\t--help this message\n \
\t--config=config_file\n", name);
return;
}
int main (int argc, char *argv[])
{
int c;
while (1){
static struct option long_opt[] = {
{"help", 0, 0, 'h'},
{"config", 1, 0, 'c'},
{0,0,0,0}
};
int optIdx;
if((c = getopt_long(argc, argv, "c:h", long_opt, &optIdx)) == -1)
break;
switch( c ){
case 'h':
usage(argv[0]);
return(-1);
case 'c':
printf("option 'c' selected, filename: %s\n", optarg);
return(1);
default:
usage(argv[0]);
return(-1);
}
}
return(0);
}
* This source code was highlighted with Source Code Highlighter.
Разбор опций командной строки в UNIX-подобных системах