You could use getopt.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int bflag = 0;
int sflag = 0;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "bs")) != -1)
switch (c)
{
case 'b':
bflag = 1;
break;
case 's':
sflag = 1;
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.
", optopt);
else
fprintf (stderr,
"Unknown option character `\x%x'.
",
optopt);
return 1;
default:
abort ();
}
printf ("bflag = %d, sflag = %d
", bflag, sflag);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s
", argv[index]);
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…