[getopt]: support for long options
Long options are quite useful, too
committer: Markus Bröker <mbroeker@largo.homelinux.org>
--- a/lotto.c
+++ b/lotto.c
@@ -159,7 +159,16 @@
num = 6;
max = ONESECOND;
- while ((i = getopt (argc, argv, "m:e:n:vwh")) >= 0) {
+ struct option options[] = {
+ {"max", 1, 0, 'm'},
+ {"end", 1, 0, 'e'},
+ {"numbers", 1, 0, 'n'},
+ {"verbose", 0, 0, 'v'},
+ {"wide", 0, 0, 'w'},
+ {"help", 0, 0, 'h'},
+ };
+
+ while ((i = getopt_long_only (argc, argv, "m:e:n:vwh", options, NULL)) != -1) {
switch (i) {
case 'm':
max = atol (optarg);
--- a/numerierung.c
+++ b/numerierung.c
@@ -10,13 +10,13 @@
void usage (char *cmd)
{
- printf ("Usage: %s [-h] [-t] [-v] [file]\n", cmd);
+ printf ("Usage: %s [-h|-p] [<file>]\n", cmd);
exit (0);
}
int main (int argc, char **argv)
{
- char buffer[81];
+ char buffer[240];
int counter = 0;
@@ -24,8 +24,12 @@
int c, pure = 0;
- while ((c = getopt (argc, argv, "hp")) != -1) {
- printf ("Zeichen: %c\n", c);
+ struct option options[] = {
+ {"help", 0, 0, 'h'},
+ {"pure", 0, 0, 'p'},
+ };
+
+ while ((c = getopt_long_only (argc, argv, "hp", options, NULL)) != -1) {
switch (c) {
case 'h':
usage (argv[0]);
@@ -36,7 +40,6 @@
default:
printf ("Unknown Parameter: %s\n", argv[optind]);
}
- printf ("GETOPT: %d : %s\n", optind, argv[optind]);
}
if (optind == argc)
@@ -51,7 +54,7 @@
*buffer = 0;
- while (fgets (buffer, 80, f) != NULL) {
+ while (fgets (buffer, sizeof (buffer), f) != NULL) {
if (!pure)
printf ("%4.4d: %s", counter++, buffer);
else
--- a/sudoku.c
+++ b/sudoku.c
@@ -188,7 +188,12 @@
int ret;
int colored = 0;
- while ((i = getopt (argc, argv, "ch")) != -1) {
+ struct option options[] = {
+ {"colored", 0, 0, 'c'},
+ {"help", 0, 0, 'h'},
+ };
+
+ while ((i = getopt_long_only (argc, argv, "ch", options, NULL)) != -1) {
switch (i) {
case 'h':
usage (argv[0]);