]> git.sesse.net Git - nageru/blob - flags.cpp
Support other streams than the default file.
[nageru] / flags.cpp
1 #include "flags.h"
2
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <utility>
9
10 using namespace std;
11
12 Flags global_flags;
13
14 // Long options that have no corresponding short option.
15 enum LongOption {
16         OPTION_HELP = 1000,
17         OPTION_SLOW_DOWN_INPUT = 1001
18 };
19
20 void usage()
21 {
22         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
23         fprintf(stderr, "\n");
24         fprintf(stderr, "      --help                      print usage information\n");
25         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
26         fprintf(stderr, "                                    source URL given)");
27 }
28
29 void parse_flags(int argc, char * const argv[])
30 {
31         static const option long_options[] = {
32                 { "help", no_argument, 0, OPTION_HELP },
33                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
34                 { 0, 0, 0, 0 }
35         };
36         for ( ;; ) {
37                 int option_index = 0;
38                 int c = getopt_long(argc, argv, "q:", long_options, &option_index);
39
40                 if (c == -1) {
41                         break;
42                 }
43                 switch (c) {
44                 case OPTION_SLOW_DOWN_INPUT:
45                         global_flags.slow_down_input = true;
46                         break;
47                 case OPTION_HELP:
48                         usage();
49                         exit(0);
50                 default:
51                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
52                         fprintf(stderr, "\n");
53                         usage();
54                         exit(1);
55                 }
56         }
57 }