Skip to content

Commit b29a08c

Browse files
authored
Merge pull request #238 from Paiusco/add-version-flag
Add version/help on ydotool client
2 parents 94fe670 + 9509ca7 commit b29a08c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Client/ydotool.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@
3838

3939
#include <errno.h>
4040
#include <stdio.h>
41+
#include <getopt.h>
4142

4243
#include <string.h>
4344

45+
#ifndef VERSION
46+
#define VERSION "unknown"
47+
#endif
48+
4449
struct tool_def {
4550
char name[16];
4651
void *ptr;
@@ -81,7 +86,10 @@ static const struct tool_def tool_list[] = {
8186
};
8287

8388
static void show_help() {
84-
puts("Usage: ydotool <cmd> <args>\n"
89+
puts("Usage: ydotool [OPTION] <cmd> <args>\n"
90+
"Options:\n"
91+
" -h, --help Display this help and exit\n"
92+
" -V, --version Show version information\n"
8593
"Available commands:");
8694

8795
int tool_count = sizeof(tool_list) / sizeof(struct tool_def);
@@ -93,6 +101,11 @@ static void show_help() {
93101
puts("Use environment variable YDOTOOL_SOCKET to specify daemon socket.");
94102
}
95103

104+
static void show_version() {
105+
puts("ydotool version(or hash): ");
106+
puts(VERSION);
107+
}
108+
96109
void uinput_emit(uint16_t type, uint16_t code, int32_t val, bool syn_report) {
97110
struct input_event ie = {
98111
.type = type,
@@ -112,9 +125,29 @@ void uinput_emit(uint16_t type, uint16_t code, int32_t val, bool syn_report) {
112125
}
113126

114127
int main(int argc, char **argv) {
115-
if (argc < 2 || strncmp(argv[1], "-h", 2) == 0 || strncmp(argv[1], "--h", 3) == 0 || strcmp(argv[1], "help") == 0) {
116-
show_help();
117-
return 0;
128+
129+
static struct option long_options[] = {
130+
{"help", no_argument, 0, 'h'},
131+
{"version", no_argument, 0, 'V'},
132+
};
133+
134+
int opt = getopt_long(argc, argv, "hV", long_options, NULL);
135+
if (opt != -1)
136+
{
137+
switch (opt) {
138+
case 'h':
139+
show_help();
140+
exit(0);
141+
142+
case 'V':
143+
show_version();
144+
exit(0);
145+
146+
default:
147+
puts("Not a valid option\n");
148+
show_help();
149+
exit(1);
150+
}
118151
}
119152

120153
int (*tool_main)(int argc, char **argv) = NULL;
@@ -129,7 +162,7 @@ int main(int argc, char **argv) {
129162

130163
if (!tool_main) {
131164
printf("ydotool: Unknown command: %s\n"
132-
"Run 'ydotool help' if you want a command list\n", argv[1]);
165+
"Run 'ydotool --help' if you want a command list\n", argv[1]);
133166
return 1;
134167
}
135168

Daemon/ydotoold.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static void show_help() {
9292
}
9393

9494
static void show_version() {
95+
puts("ydotoold version(or hash): ");
9596
puts(VERSION);
9697
}
9798

0 commit comments

Comments
 (0)