diff cp2102/read_partno.c @ 98:1cacc1ae56f0

cp2102 tools: convert to -d option for non-default device
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 04:45:13 +0000
parents 8d35346f1d46
children
line wrap: on
line diff
--- a/cp2102/read_partno.c	Thu Sep 28 03:26:11 2023 +0000
+++ b/cp2102/read_partno.c	Thu Sep 28 04:45:13 2023 +0000
@@ -15,6 +15,30 @@
 #include "cp210x_defs.h"
 
 extern struct usb_device *find_cp2102_device();
+char *device_selector;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "d:")) != EOF) {
+		switch (c) {
+		case 'd':
+			device_selector = optarg;
+			continue;
+		default:
+		usage:
+			fprintf(stderr, "usage: %s [-d device-selector]\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind)
+		goto usage;
+}
 
 main(argc, argv)
 	char **argv;
@@ -24,11 +48,8 @@
 	u_char partno;
 	int rc;
 
-	if (argc != 2) {
-		fprintf(stderr, "usage: %s device-selector\n", argv[0]);
-		exit(1);
-	}
-	dev = find_cp2102_device(argv[1]);
+	process_cmdline(argc, argv);
+	dev = find_cp2102_device(device_selector);
 	usbh = usb_open(dev);
 	if (!usbh) {
 		fprintf(stderr, "error: usb_open() failed\n");