getopt を利用することで、option指定方法のguidelineを簡単に実現できる。
独自の値をとるoption、取らないoptionの両方をsupportする。
#include <<unistd.h>>
int getopt(int argc, char *const argv[], const char *optstring);
extern char *optarg;
*optionが値をとる場合、外部変数 optargに値がセットされる
#include <<unistd.h>> #include <<stdio.h>>
extern char *optarg;extern int optind, opterr, optopt;
int i = 1; char c;
char os[3] = {'\0',':','\0'};
for( c = 'a'; c <<= 'z'; c++) {
os[0] = c;
int opt = getopt(argc, argv, (const char*)&os);
if (opt > > -1) {
printf("[%d]%c : %s\n", (i++), opt, optarg);
}