[PATCH evemu 2/3] tools: play: use getopt
Benjamin Tissoires
benjamin.tissoires at gmail.com
Thu Jan 9 11:34:08 PST 2014
Instead of relying on argc and hard coded values, use getopt framework.
Signed-off-by: Benjamin Tissoires <benjamin.tissoires at gmail.com>
---
tools/evemu-play.c | 37 +++++++++++++++++++++++++++++++++----
1 file changed, 33 insertions(+), 4 deletions(-)
diff --git a/tools/evemu-play.c b/tools/evemu-play.c
index 8d14618..030e142 100644
--- a/tools/evemu-play.c
+++ b/tools/evemu-play.c
@@ -45,10 +45,18 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
+#include <getopt.h>
+
+static struct option opts[] = {
+ { "help", no_argument, 0, 'h'},
+};
static void usage(void)
{
- fprintf(stderr, "Usage: %s <device>\n", program_invocation_short_name);
+ fprintf(stderr, "Usage: %s [OPTION] <device>\n", program_invocation_short_name);
+ fprintf(stderr, "\n");
+ fprintf(stderr, "Where OPTION is:\n");
+ fprintf(stderr, " -h or --help: print this message\n");
fprintf(stderr, "\n");
fprintf(stderr, "Event data is read from standard input.\n");
}
@@ -56,11 +64,30 @@ static void usage(void)
int main(int argc, char *argv[])
{
int fd;
- if (argc != 2) {
+
+ while(1) {
+ int option_index = 0;
+ int c;
+
+ c = getopt_long(argc, argv, "h", opts, &option_index);
+ if (c == -1) /* Detect the end of the options. */
+ break;
+
+ switch(c) {
+ case 'h': /* help */
+ default:
+ usage();
+ goto out;
+ }
+ }
+
+ /* if the device was not specified, fail */
+ if (optind >= argc) {
usage();
- return -1;
+ goto out;
}
- fd = open(argv[1], O_WRONLY);
+
+ fd = open(argv[optind], O_WRONLY);
if (fd < 0) {
fprintf(stderr, "error: could not open device\n");
return -1;
@@ -70,4 +97,6 @@ int main(int argc, char *argv[])
}
close(fd);
return 0;
+out:
+ return -1;
}
--
1.8.3.1
More information about the Input-tools
mailing list