summaryrefslogtreecommitdiff
path: root/src/options.c
blob: 61a74b613bfcc6aab038387657e0341d7963ba73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include "cargs/cargs.h"

#include "options.h"
#include "logging.h"

static struct cag_option options[] = {
	{
		.identifier = 'q',
		.access_letters = "q",
		.access_name = NULL,
		.value_name = NULL,
		.description = "Decrease output verbosity (-qqq suppresses even fatal errors)",
	},
	{
		.identifier = 'v',
		.access_letters = "v",
		.access_name = NULL,
		.value_name = NULL,
		.description = "Increase output verbosity (-vvv displays every log message)",
	},
	{
		.identifier = 'c',
		.access_letters = "c",
		.access_name = "config",
		.value_name = "CONF_FILE",
		.description = "Specify configuration file to read (default: config.lua)",
	},
	{
		.identifier = 's',
		.access_letters = "s",
		.access_name = "script",
		.value_name = "SCRIPT_FILE",
		.description = "Override the built-in build script",
	},
	{
		.identifier = 'h',
		.access_letters = "h",
		.access_name = "help",
		.value_name = NULL,
		.description = "Print this help message and exit",
	},
};

static void print_usage(const char *progname)
{
   printf("Usage: %s [OPTIONS]\n", progname);
   cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
}

int parse_options(struct argent_options *opts, int argc, char **argv)
{
   opts->log_level = WARN;
   opts->conf_filename = "config.lua";
   opts->script_filename = NULL;
   
   char opt;
   cag_option_context context;
   cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
   while (cag_option_fetch(&context)) {
      opt = cag_option_get(&context);
      switch (opt) {
      case 'q':
         opts->log_level -= 1;
         break;
    
      case 'v':
         opts->log_level += 1;
         break;
    
      case 'c':
         opts->conf_filename = cag_option_get_value(&context);
         break;

      case 's':
         opts->script_filename = cag_option_get_value(&context);
         break;

      case 'h':
         print_usage(argv[0]);
         return 2;

      default:
         print_usage(argv[0]);
         return 1;
      }
   }

   // ew, globals (sorry)
   argent_log_level = opts->log_level;

   return 0;
}