Add --version argument

This commit is contained in:
Konstantin Pereiaslov
2023-05-28 18:54:06 -05:00
parent c62dc47f68
commit 9344aa4e4e
3 changed files with 15 additions and 4 deletions

View File

@@ -6,7 +6,6 @@ ifeq ($(PREFIX),)
endif
SOURCES = time_utils.c sleep_utils.c main.c
OBJECTS = $(SOURCES:.c=.o)
all: executable
release: CCFLAGS += -O3
@@ -15,6 +14,8 @@ release: executable
debug: CCFLAGS += -DDEBUG -ggdb
debug: executable
executable: CCFLAGS += -DVERSION=\"$(shell git describe --tags 2>/dev/null || (echo -n "0.0-dev-" && git rev-parse HEAD))\"
%.o: %.c
$(CC) $(CCFLAGS) -c $< -o $@ $(LDFLAGS) $(LDLIBS)

View File

@@ -1,6 +1,5 @@
Here are the planned changes:
* Add support for --version|-V argument.
* AUR package.
* Look more into Wayland support.
* ?Test coverage?

15
main.c
View File

@@ -13,6 +13,10 @@
#include "sleep_utils.h"
#include "time_utils.h"
#ifndef VERSION
#define VERSION 'unkown'
#endif
int verbose;
int quiet;
@@ -50,9 +54,12 @@ void resume_command(pid_t pid) {
}
void print_usage(char *binary_name) {
printf("Usage: %s [--timeout|-t timeout_value_in_seconds] [--verbose|-v] [--quiet|-q] shell_command_to_run\n",
printf("Usage: %s [--timeout|-t timeout_value_in_seconds] [--verbose|-v] [--quiet|-q] [--version|-V] shell_command_to_run [shell_command_arguments]\n",
binary_name);
}
void print_version() {
printf("runwhenidle %s\n", VERSION);
}
pid_t run_shell_command(const char *shell_command_to_run, pid_t pid) {
if (verbose) {
@@ -138,12 +145,13 @@ int main(int argc, char *argv[]) {
{"verbose", no_argument, NULL, 'v'},
{"quiet", no_argument, NULL, 'q'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
// Parse command line options
int option;
while ((option = getopt_long(argc, argv, "+hvqt:", long_options, NULL)) != -1) {
while ((option = getopt_long(argc, argv, "+hvqt:V", long_options, NULL)) != -1) {
switch (option) {
case 't':
const long TIMEOUT_MAX_SUPPORTED_VALUE = 100000000; //~3 years
@@ -156,6 +164,9 @@ int main(int argc, char *argv[]) {
}
user_idle_timeout_ms = timeout_arg_value * 1000;
break;
case 'V':
print_version();
return 0;
case 'v':
verbose = 1;
break;