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+
4449struct tool_def {
4550 char name [16 ];
4651 void * ptr ;
@@ -81,7 +86,10 @@ static const struct tool_def tool_list[] = {
8186};
8287
8388static 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+
96109void 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
114127int 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
0 commit comments