tools: Use getopt for option parsing in all tools
diff --git a/tools/ideviceenterrecovery.c b/tools/ideviceenterrecovery.c
index 0cc9936..29cc5c9 100644
--- a/tools/ideviceenterrecovery.c
+++ b/tools/ideviceenterrecovery.c
@@ -27,8 +27,9 @@
 
 #include <stdio.h>
 #include <string.h>
-#include <errno.h>
 #include <stdlib.h>
+#include <getopt.h>
+#include <errno.h>
 #ifndef WIN32
 #include <signal.h>
 #endif
@@ -36,22 +37,22 @@
 #include <libimobiledevice/libimobiledevice.h>
 #include <libimobiledevice/lockdown.h>
 
-static void print_usage(int argc, char **argv)
+static void print_usage(int argc, char **argv, int is_error)
 {
-	char *name = NULL;
-
-	name = strrchr(argv[0], '/');
-	printf("Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
-	printf("\n");
-	printf("Makes a device with the supplied UDID enter recovery mode immediately.\n");
-	printf("\n");
-	printf("OPTIONS:\n");
-	printf("  -d, --debug\t\tenable communication debugging\n");
-	printf("  -h, --help\t\tprints usage information\n");
-	printf("  -v, --version\t\tprints version information\n");
-	printf("\n");
-	printf("Homepage:    <" PACKAGE_URL ">\n");
-	printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n");
+	char *name = strrchr(argv[0], '/');
+	fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] UDID\n", (name ? name + 1: argv[0]));
+	fprintf(is_error ? stderr : stdout,
+		"\n"
+		"Makes a device with the supplied UDID enter recovery mode immediately.\n"
+		"\n"
+		"OPTIONS:\n"
+		"  -d, --debug           enable communication debugging\n"
+		"  -h, --help            prints usage information\n"
+		"  -v, --version         prints version information\n"
+		"\n"
+		"Homepage:    <" PACKAGE_URL ">\n"
+		"Bug Reports: <" PACKAGE_BUGREPORT ">\n"
+	);
 }
 
 int main(int argc, char *argv[])
@@ -60,34 +61,44 @@
 	lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
 	idevice_t device = NULL;
 	idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
-	int i;
 	const char* udid = NULL;
+	int c = 0;
+	const struct option longopts[] = {
+		{ "debug", no_argument, NULL, 'd' },
+		{ "help", no_argument, NULL, 'h' },
+		{ "version", no_argument, NULL, 'v' },
+		{ NULL, 0, NULL, 0}
+	};
 
 #ifndef WIN32
 	signal(SIGPIPE, SIG_IGN);
 #endif
 	/* parse cmdline args */
-	for (i = 1; i < argc; i++) {
-		if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
+	while ((c = getopt_long(argc, argv, "dhv", longopts, NULL)) != -1) {
+		switch (c) {
+		case 'd':
 			idevice_set_debug_level(1);
-			continue;
-		}
-		else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
-			print_usage(argc, argv);
+			break;
+		case 'h':
+			print_usage(argc, argv, 0);
 			return 0;
-		}
-		else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
+		case 'v':
 			printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
 			return 0;
+		default:
+			print_usage(argc, argv, 1);
+			return 2;
 		}
 	}
+	argc -= optind;
+	argv += optind;
 
-	i--;
-	if (argc < 2 || !argv[i] || !*argv[i]) {
-		print_usage(argc, argv);
-		return 0;
+	if (!argv[0]) {
+		fprintf(stderr, "ERROR: No UDID specified\n");
+		print_usage(argc+optind, argv-optind, 1);
+		return 2;
 	}
-	udid = argv[i];
+	udid = argv[0];
 
 	ret = idevice_new(&device, udid);
 	if (ret != IDEVICE_E_SUCCESS) {