]> git.sesse.net Git - nageru/blob - futatabi/flags.cpp
0de7b0261937f42b1133e82b95e42d742acbab28
[nageru] / futatabi / 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         OPTION_HTTP_PORT = 1002
19 };
20
21 void usage()
22 {
23         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
24         fprintf(stderr, "\n");
25         fprintf(stderr, "      --help                      print usage information\n");
26         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
27         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
28         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
29         fprintf(stderr, "                                    source URL given)\n");
30         fprintf(stderr, "  -q, --interpolation-quality N   1 = fastest\n");
31         fprintf(stderr, "                                  2 = default (realtime 720p on fast embedded GPUs)\n");
32         fprintf(stderr, "                                  3 = good (realtime 720p on GTX 970 or so)\n");
33         fprintf(stderr, "                                  4 = best (not realtime on any current GPU)\n");
34         fprintf(stderr, "  -d, --working-directory DIR     where to store frames and database\n");
35         fprintf(stderr, "      --http-port PORT            which port to listen on for output\n");
36 }
37
38 void parse_flags(int argc, char * const argv[])
39 {
40         static const option long_options[] = {
41                 { "help", no_argument, 0, OPTION_HELP },
42                 { "width", required_argument, 0, 'w' },
43                 { "height", required_argument, 0, 'h' },
44                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
45                 { "interpolation-quality", required_argument, 0, 'q' },
46                 { "working-directory", required_argument, 0, 'd' },
47                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
48                 { 0, 0, 0, 0 }
49         };
50         for ( ;; ) {
51                 int option_index = 0;
52                 int c = getopt_long(argc, argv, "w:h:q:d:", long_options, &option_index);
53
54                 if (c == -1) {
55                         break;
56                 }
57                 switch (c) {
58                 case 'w':
59                         global_flags.width = atoi(optarg);
60                         break;
61                 case 'h':
62                         global_flags.height = atoi(optarg);
63                         break;
64                 case OPTION_SLOW_DOWN_INPUT:
65                         global_flags.slow_down_input = true;
66                         break;
67                 case 'q':
68                         global_flags.interpolation_quality = atoi(optarg);
69                         break;
70                 case 'd':
71                         global_flags.working_directory = optarg;
72                         break;
73                 case OPTION_HTTP_PORT:
74                         global_flags.http_port = atoi(optarg);
75                         break;
76                 case OPTION_HELP:
77                         usage();
78                         exit(0);
79                 default:
80                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
81                         fprintf(stderr, "\n");
82                         usage();
83                         exit(1);
84                 }
85         }
86
87         if (global_flags.interpolation_quality < 1 || global_flags.interpolation_quality > 4) {
88                 fprintf(stderr, "Interpolation quality must be 1, 2, 3 or 4.\n");
89                 usage();
90                 exit(1);
91         }
92 }