From 9d17aeaddfbe7ddfc0615b85283a72224ee4f7b0 Mon Sep 17 00:00:00 2001 From: dokutan <54861821+dokutan@users.noreply.github.com> Date: Sat, 2 May 2020 16:50:55 +0200 Subject: [PATCH] Add --version option --- include/print_help.cpp | 1 + makefile | 6 +++++- mouse_m908.1 | 3 +++ mouse_m908.cpp | 26 +++++++++++++++++++++++--- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/include/print_help.cpp b/include/print_help.cpp index dc083fc..4955f60 100644 --- a/include/print_help.cpp +++ b/include/print_help.cpp @@ -33,5 +33,6 @@ void print_help(){ std::cout << "-b --bus\n\tUSB bus id, requires -d.\n"; std::cout << "-d --device\n\tUSB device number, requires -b.\n"; std::cout << "-k --kernel-driver\n\tDo not attempt to detach kernel driver.\n"; + std::cout << "-v --version\n\tPrint version.\n"; //std::cout << "-r --repeat\n\tSets number of times the macro will be repeated (1-255).\n"; } diff --git a/makefile b/makefile index 12f0fc8..dc06beb 100644 --- a/makefile +++ b/makefile @@ -9,6 +9,9 @@ CC = g++ CC_OPTIONS := -Wall -Wextra -O2 `pkg-config --cflags libusb-1.0` LIBS != pkg-config --libs libusb-1.0 +# version string +VERSION_STRING = "1.7" + # compile build: constructor.o data.o getters.o helpers.o load_config.o setters.o writers.o mouse_m908.o $(CC) *.o -o mouse_m908 $(LIBS) $(CC_OPTIONS) @@ -22,6 +25,7 @@ install: cp ./example.macro $(DOC_DIR)/mouse_m908/ && \ cp ./README.md $(DOC_DIR)/mouse_m908/ && \ cp ./keymap.md $(DOC_DIR)/mouse_m908/ && \ + cp ./LICENSE $(DOC_DIR)/mouse_m908/ && \ cp ./mouse_m908.1 $(MAN_DIR)/ # remove binary @@ -40,7 +44,7 @@ upgrade: install # individual files mouse_m908.o: - $(CC) -c mouse_m908.cpp $(CC_OPTIONS) + $(CC) -c mouse_m908.cpp $(CC_OPTIONS) -D VERSION_STRING=$(VERSION_STRING) constructor.o: $(CC) -c include/constructor.cpp $(CC_OPTIONS) diff --git a/mouse_m908.1 b/mouse_m908.1 index 17b5524..3d3fde7 100644 --- a/mouse_m908.1 +++ b/mouse_m908.1 @@ -40,6 +40,9 @@ USB device address, with this option the mouse gets opened by its bus id and dev .TP \fB\-k\fR, \fB\-\-kernel\-driver\fR Do not attempt to detach the kernel drivers. Useful for compatibility on some systems. +.TP +\fB\-v\fR, \fB\-\-version\fR +Print version. .SH EXAMPLES To send the configuration from example.ini .PP diff --git a/mouse_m908.cpp b/mouse_m908.cpp index 4e913e7..c261c83 100644 --- a/mouse_m908.cpp +++ b/mouse_m908.cpp @@ -30,6 +30,11 @@ #include "include/load_config.h" #include "include/print_help.cpp" +// this is the default version string +// the version string gets overwritten by the makefile +#ifndef VERSION_STRING +#define VERSION_STRING "no version defined during compilation" +#endif // this function checks its arguments and opens the mouse accordingly // (with vid and pid or with bus and device) @@ -75,6 +80,12 @@ int main( int argc, char **argv ){ mouse_m908 m; + // if no arguments: print help + if( argc == 1 ){ + print_help(); + return 0; + } + //command line options static struct option long_options[] = { {"help", no_argument, 0, 'h'}, @@ -86,6 +97,7 @@ int main( int argc, char **argv ){ {"bus", required_argument, 0, 'b'}, {"device", required_argument, 0, 'd'}, {"kernel-driver", no_argument, 0, 'k'}, + {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; @@ -93,6 +105,7 @@ int main( int argc, char **argv ){ bool flag_macro = false, flag_number = false; bool flag_bus = false, flag_device = false; bool flag_kernel_driver = false; + bool flag_version = false; //bool flag_repeat; std::string string_config, string_profile; std::string string_macro, string_number; @@ -101,8 +114,8 @@ int main( int argc, char **argv ){ //parse command line options int c, option_index = 0; - //while( (c = getopt_long( argc, argv, "hc:p:m:n:r:", - while( (c = getopt_long( argc, argv, "hc:p:m:n:b:d:k", + //while( (c = getopt_long( argc, argv, "hc:p:m:n:b:d:kvr:", + while( (c = getopt_long( argc, argv, "hc:p:m:n:b:d:kv", long_options, &option_index ) ) != -1 ){ switch( c ){ @@ -141,6 +154,9 @@ int main( int argc, char **argv ){ case 'k': flag_kernel_driver = true; break; + case 'v': + flag_version = true; + break; case '?': break; default: @@ -151,7 +167,11 @@ int main( int argc, char **argv ){ // set wether to detach kernel driver m.set_detach_kernel_driver( !flag_kernel_driver ); - //load and write config + // print version if requested + if( flag_version ) + std::cout << "Version: " << VERSION_STRING << "\n"; + + // load and write config if( flag_config ){ try{