Add command line options
This commit is contained in:
parent
e36750f8cd
commit
c2b5fceaf5
|
@ -18,15 +18,100 @@
|
|||
|
||||
#define evdev_name "logid"
|
||||
|
||||
LogLevel global_verbosity = DEBUG;
|
||||
LogLevel global_verbosity = INFO;
|
||||
Configuration* global_config;
|
||||
EvdevDevice* global_evdev;
|
||||
DeviceFinder* finder;
|
||||
|
||||
enum class Option
|
||||
{
|
||||
None,
|
||||
Verbose,
|
||||
Config,
|
||||
Help
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char* config_file = "/etc/logid.cfg";
|
||||
for(int i = 1; i < argc; i++)
|
||||
{
|
||||
Option option = Option::None;
|
||||
if(argv[i][0] == '-') // Option
|
||||
{
|
||||
switch(argv[i][1])
|
||||
{
|
||||
case '-': // Full option name
|
||||
{
|
||||
std::string op_str = argv[i];
|
||||
if (op_str == "--verbose") option = Option::Verbose;
|
||||
if (op_str == "--config") option = Option::Config;
|
||||
if (op_str == "--help") option = Option::Help;
|
||||
break;
|
||||
}
|
||||
case 'v': // Verbosity
|
||||
option = Option::Verbose;
|
||||
break;
|
||||
case 'c': // Config file path
|
||||
option = Option::Config;
|
||||
break;
|
||||
case 'h': // Help
|
||||
option = Option::Help;
|
||||
break;
|
||||
default:
|
||||
log_printf(WARN, "%s is not a valid option, ignoring.", argv[1]);
|
||||
}
|
||||
switch(option)
|
||||
{
|
||||
case Option::Verbose:
|
||||
{
|
||||
if (++i >= argc)
|
||||
{
|
||||
global_verbosity = DEBUG; // Assume debug verbosity
|
||||
break;
|
||||
}
|
||||
std::string loglevel = argv[i];
|
||||
try { global_verbosity = string_to_loglevel(argv[i]); }
|
||||
catch (std::invalid_argument &e)
|
||||
{
|
||||
if (argv[i][0] == '-')
|
||||
global_verbosity = DEBUG; // Assume debug verbosity
|
||||
else
|
||||
{
|
||||
log_printf(WARN, e.what());
|
||||
printf("Valid verbosity levels are: Debug, Info, Warn/Warning, or Error.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Option::Config:
|
||||
{
|
||||
if (++i >= argc)
|
||||
{
|
||||
log_printf(ERROR, "Config file is not specified.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
config_file = argv[i];
|
||||
break;
|
||||
}
|
||||
case Option::Help:
|
||||
printf(R"(Usage: %s [options]
|
||||
Possible options are:
|
||||
-v,--verbose [level] Set log level to debug/info/warn/error (leave blank for debug)
|
||||
-c,--config [file path] Change config file from default at %s
|
||||
-h,--help Print this message.
|
||||
)", argv[0], config_file);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
case Option::None:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read config
|
||||
try { global_config = new Configuration("logid.cfg"); }
|
||||
try { global_config = new Configuration(config_file); }
|
||||
catch (std::exception &e) { return EXIT_FAILURE; }
|
||||
|
||||
//Create an evdev device called 'logid'
|
||||
|
|
|
@ -107,4 +107,17 @@ Action string_to_action(std::string s)
|
|||
if(s == "changedpi") return Action::ChangeDPI;
|
||||
|
||||
throw std::invalid_argument(original_str + " is an invalid action.");
|
||||
}
|
||||
|
||||
LogLevel string_to_loglevel(std::string s)
|
||||
{
|
||||
std::string original_str = s;
|
||||
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
|
||||
|
||||
if(s == "debug") return DEBUG;
|
||||
if(s == "info") return INFO;
|
||||
if(s == "warn" || s == "warning") return WARN;
|
||||
if(s == "error") return ERROR;
|
||||
|
||||
throw std::invalid_argument(original_str + " is an invalid log level.");
|
||||
}
|
|
@ -21,5 +21,6 @@ Direction get_direction(int x, int y);
|
|||
Direction string_to_direction(std::string s);
|
||||
GestureMode string_to_gesturemode(std::string s);
|
||||
Action string_to_action(std::string s);
|
||||
LogLevel string_to_loglevel(std::string s);
|
||||
|
||||
#endif //UTIL_H
|
Loading…
Reference in New Issue
Block a user