All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] i2c-tools: Use getopt
@ 2022-05-09  9:03 Jean Delvare
  2022-05-09  9:08 ` [PATCH v2 2/2] i2c-tools: Implement and document option -h Jean Delvare
  2022-05-14 13:53 ` [PATCH v2 1/2] i2c-tools: Use getopt Wolfram Sang
  0 siblings, 2 replies; 4+ messages in thread
From: Jean Delvare @ 2022-05-09  9:03 UTC (permalink / raw)
  To: Linux I2C; +Cc: Wolfram Sang, david kerns

Use the standard C getopt() function instead of parsing the options
manually. The benefit of this move is that getopt() supports merged
flags (for example -fy instead of -f -y) and does not require options
to come first on the command line. As this is pretty standard by now,
not supporting such possibilities can confuse and disappoint the user.

Based on a preliminary patch from David Kerns.

Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
Note: For i2ctransfer, I chose to keep arg_idx although it has the same
meaning as getopt's optind variable, as I'm not sure if it is considered
an acceptable practice for the caller to tinkle with it. If it is then
we could get rid of arg_idx and use optind everywhere instead.

Changes since v1:
 * Update i2ctransfer too (suggested by Wolfram Sang)
 * Update the copyright years of the tools I wrote

 tools/i2cdetect.c   |   25 +++++++++++--------------
 tools/i2cdump.c     |   51 ++++++++++++++++++++++++---------------------------
 tools/i2cget.c      |   33 +++++++++++++++------------------
 tools/i2cset.c      |   47 ++++++++++++++++++++---------------------------
 tools/i2ctransfer.c |   14 ++++++--------
 5 files changed, 76 insertions(+), 94 deletions(-)

--- i2c-tools.orig/tools/i2cdetect.c	2022-05-06 16:05:51.203591122 +0200
+++ i2c-tools/tools/i2cdetect.c	2022-05-06 16:15:11.132122653 +0200
@@ -2,7 +2,7 @@
     i2cdetect.c - a user-space program to scan for I2C devices
     Copyright (C) 1999-2004  Frodo Looijaard <frodol@dds.nl>, and
                              Mark D. Studebaker <mdsxyz123@yahoo.com>
-    Copyright (C) 2004-2012  Jean Delvare <jdelvare@suse.de>
+    Copyright (C) 2004-2022  Jean Delvare <jdelvare@suse.de>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -205,12 +205,12 @@ int main(int argc, char *argv[])
 	unsigned long funcs;
 	int mode = MODE_AUTO;
 	int first = 0x08, last = 0x77;
-	int flags = 0;
+	int opt;
 	int yes = 0, version = 0, list = 0;
 
 	/* handle (optional) flags first */
-	while (1+flags < argc && argv[1+flags][0] == '-') {
-		switch (argv[1+flags][1]) {
+	while ((opt = getopt(argc, argv, "FValqry")) != -1) {
+		switch (opt) {
 		case 'V': version = 1; break;
 		case 'y': yes = 1; break;
 		case 'l': list = 1; break;
@@ -242,13 +242,10 @@ int main(int argc, char *argv[])
 			first = 0x00;
 			last = 0x7F;
 			break;
-		default:
-			fprintf(stderr, "Error: Unsupported option "
-				"\"%s\"!\n", argv[1+flags]);
+		case '?':
 			help();
 			exit(1);
 		}
-		flags++;
 	}
 
 	if (version) {
@@ -261,22 +258,22 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
-	if (argc < flags + 2) {
+	if (argc < optind + 1) {
 		fprintf(stderr, "Error: No i2c-bus specified!\n");
 		help();
 		exit(1);
 	}
-	i2cbus = lookup_i2c_bus(argv[flags+1]);
+	i2cbus = lookup_i2c_bus(argv[optind]);
 	if (i2cbus < 0) {
 		help();
 		exit(1);
 	}
 
 	/* read address range if present */
-	if (argc == flags + 4 && mode != MODE_FUNC) {
+	if (argc == optind + 3 && mode != MODE_FUNC) {
 		int tmp;
 
-		tmp = strtol(argv[flags+2], &end, 0);
+		tmp = strtol(argv[optind+1], &end, 0);
 		if (*end) {
 			fprintf(stderr, "Error: FIRST argument not a "
 				"number!\n");
@@ -291,7 +288,7 @@ int main(int argc, char *argv[])
 		}
 		first = tmp;
 
-		tmp = strtol(argv[flags+3], &end, 0);
+		tmp = strtol(argv[optind+2], &end, 0);
 		if (*end) {
 			fprintf(stderr, "Error: LAST argument not a "
 				"number!\n");
@@ -305,7 +302,7 @@ int main(int argc, char *argv[])
 			exit(1);
 		}
 		last = tmp;
-	} else if (argc != flags + 2) {
+	} else if (argc != optind + 1) {
 		help();
 		exit(1);
 	}
--- i2c-tools.orig/tools/i2cdump.c	2022-05-06 16:05:51.203591122 +0200
+++ i2c-tools/tools/i2cdump.c	2022-05-06 16:15:17.051191339 +0200
@@ -2,7 +2,7 @@
     i2cdump.c - a user-space program to dump I2C registers
     Copyright (C) 2002-2003  Frodo Looijaard <frodol@dds.nl>, and
                              Mark D. Studebaker <mdsxyz123@yahoo.com>
-    Copyright (C) 2004-2021  Jean Delvare <jdelvare@suse.de>
+    Copyright (C) 2004-2022  Jean Delvare <jdelvare@suse.de>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -110,26 +110,23 @@ int main(int argc, char *argv[])
 	char filename[20];
 	int block[256];
 	int pec = 0, even = 0;
-	int flags = 0;
+	int opt;
 	int force = 0, yes = 0, version = 0, all_addrs = 0;
 	const char *range = NULL;
 	int first = 0x00, last = 0xff;
 
 	/* handle (optional) flags first */
-	while (1+flags < argc && argv[1+flags][0] == '-') {
-		switch (argv[1+flags][1]) {
+	while ((opt = getopt(argc, argv, "Vafr:y")) != -1) {
+		switch (opt) {
 		case 'V': version = 1; break;
 		case 'f': force = 1; break;
-		case 'r': range = argv[1+(++flags)]; break;
+		case 'r': range = optarg; break;
 		case 'y': yes = 1; break;
 		case 'a': all_addrs = 1; break;
-		default:
-			fprintf(stderr, "Error: Unsupported option "
-				"\"%s\"!\n", argv[1+flags]);
+		case '?':
 			help();
 			exit(1);
 		}
-		flags++;
 	}
 
 	if (version) {
@@ -137,48 +134,48 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
-	if (argc < flags + 2) {
+	if (argc < optind + 1) {
 		fprintf(stderr, "Error: No i2c-bus specified!\n");
 		help();
 		exit(1);
 	}
-	i2cbus = lookup_i2c_bus(argv[flags+1]);
+	i2cbus = lookup_i2c_bus(argv[optind]);
 	if (i2cbus < 0) {
 		help();
 		exit(1);
 	}
 
-	if (argc < flags + 3) {
+	if (argc < optind + 2) {
 		fprintf(stderr, "Error: No address specified!\n");
 		help();
 		exit(1);
 	}
-	address = parse_i2c_address(argv[flags+2], all_addrs);
+	address = parse_i2c_address(argv[optind+1], all_addrs);
 	if (address < 0) {
 		help();
 		exit(1);
 	}
 
-	if (argc < flags + 4) {
+	if (argc < optind + 3) {
 		fprintf(stderr, "No size specified (using byte-data access)\n");
 		size = I2C_SMBUS_BYTE_DATA;
-	} else if (!strncmp(argv[flags+3], "b", 1)) {
+	} else if (!strncmp(argv[optind+2], "b", 1)) {
 		size = I2C_SMBUS_BYTE_DATA;
-		pec = argv[flags+3][1] == 'p';
-	} else if (!strncmp(argv[flags+3], "w", 1)) {
+		pec = argv[optind+2][1] == 'p';
+	} else if (!strncmp(argv[optind+2], "w", 1)) {
 		size = I2C_SMBUS_WORD_DATA;
-		pec = argv[flags+3][1] == 'p';
-	} else if (!strncmp(argv[flags+3], "W", 1)) {
+		pec = argv[optind+2][1] == 'p';
+	} else if (!strncmp(argv[optind+2], "W", 1)) {
 		size = I2C_SMBUS_WORD_DATA;
 		even = 1;
-	} else if (!strncmp(argv[flags+3], "s", 1)) {
+	} else if (!strncmp(argv[optind+2], "s", 1)) {
 		fprintf(stderr,
 			"SMBus block mode is no longer supported, please use i2cget instead\n");
 		exit(1);
-	} else if (!strncmp(argv[flags+3], "c", 1)) {
+	} else if (!strncmp(argv[optind+2], "c", 1)) {
 		size = I2C_SMBUS_BYTE;
-		pec = argv[flags+3][1] == 'p';
-	} else if (!strcmp(argv[flags+3], "i"))
+		pec = argv[optind+2][1] == 'p';
+	} else if (!strcmp(argv[optind+2], "i"))
 		size = I2C_SMBUS_I2C_BLOCK_DATA;
 	else {
 		fprintf(stderr, "Error: Invalid mode!\n");
@@ -186,8 +183,8 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	if (argc > flags + 4) {
-		bank = strtol(argv[flags+4], &end, 0);
+	if (argc > optind + 3) {
+		bank = strtol(argv[optind+3], &end, 0);
 		if (*end || size == I2C_SMBUS_I2C_BLOCK_DATA) {
 			fprintf(stderr, "Error: Invalid bank number!\n");
 			help();
@@ -200,8 +197,8 @@ int main(int argc, char *argv[])
 			exit(1);
 		}
 
-		if (argc > flags + 5) {
-			bankreg = strtol(argv[flags+5], &end, 0);
+		if (argc > optind + 4) {
+			bankreg = strtol(argv[optind+4], &end, 0);
 			if (*end) {
 				fprintf(stderr, "Error: Invalid bank register "
 					"number!\n");
--- i2c-tools.orig/tools/i2cget.c	2022-05-06 16:05:51.203591122 +0200
+++ i2c-tools/tools/i2cget.c	2022-05-06 16:14:58.335974165 +0200
@@ -1,6 +1,6 @@
 /*
     i2cget.c - A user-space program to read an I2C register.
-    Copyright (C) 2005-2021  Jean Delvare <jdelvare@suse.de>
+    Copyright (C) 2005-2022  Jean Delvare <jdelvare@suse.de>
 
     Based on i2cset.c:
     Copyright (C) 2001-2003  Frodo Looijaard <frodol@dds.nl>, and
@@ -180,25 +180,22 @@ int main(int argc, char *argv[])
 	int daddress;
 	char filename[20];
 	int pec = 0;
-	int flags = 0;
+	int opt;
 	int force = 0, yes = 0, version = 0, all_addrs = 0;
 	int length;
 	unsigned char block_data[I2C_SMBUS_BLOCK_MAX];
 
 	/* handle (optional) flags first */
-	while (1+flags < argc && argv[1+flags][0] == '-') {
-		switch (argv[1+flags][1]) {
+	while ((opt = getopt(argc, argv, "Vafy")) != -1) {
+		switch (opt) {
 		case 'V': version = 1; break;
 		case 'f': force = 1; break;
 		case 'y': yes = 1; break;
 		case 'a': all_addrs = 1; break;
-		default:
-			fprintf(stderr, "Error: Unsupported option "
-				"\"%s\"!\n", argv[1+flags]);
+		case '?':
 			help();
 			exit(1);
 		}
-		flags++;
 	}
 
 	if (version) {
@@ -206,20 +203,20 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
-	if (argc < flags + 3)
+	if (argc < optind + 2)
 		help();
 
-	i2cbus = lookup_i2c_bus(argv[flags+1]);
+	i2cbus = lookup_i2c_bus(argv[optind]);
 	if (i2cbus < 0)
 		help();
 
-	address = parse_i2c_address(argv[flags+2], all_addrs);
+	address = parse_i2c_address(argv[optind+1], all_addrs);
 	if (address < 0)
 		help();
 
-	if (argc > flags + 3) {
+	if (argc > optind + 2) {
 		size = I2C_SMBUS_BYTE_DATA;
-		daddress = strtol(argv[flags+3], &end, 0);
+		daddress = strtol(argv[optind+2], &end, 0);
 		if (*end || daddress < 0 || daddress > 0xff) {
 			fprintf(stderr, "Error: Data address invalid!\n");
 			help();
@@ -229,8 +226,8 @@ int main(int argc, char *argv[])
 		daddress = -1;
 	}
 
-	if (argc > flags + 4) {
-		switch (argv[flags+4][0]) {
+	if (argc > optind + 3) {
+		switch (argv[optind+3][0]) {
 		case 'b': size = I2C_SMBUS_BYTE_DATA; break;
 		case 'w': size = I2C_SMBUS_WORD_DATA; break;
 		case 'c': size = I2C_SMBUS_BYTE; break;
@@ -240,19 +237,19 @@ int main(int argc, char *argv[])
 			fprintf(stderr, "Error: Invalid mode!\n");
 			help();
 		}
-		pec = argv[flags+4][1] == 'p';
+		pec = argv[optind+3][1] == 'p';
 		if (size == I2C_SMBUS_I2C_BLOCK_DATA && pec) {
 			fprintf(stderr, "Error: PEC not supported for I2C block data!\n");
 			help();
 		}
 	}
 
-	if (argc > flags + 5) {
+	if (argc > optind + 4) {
 		if (size != I2C_SMBUS_I2C_BLOCK_DATA) {
 			fprintf(stderr, "Error: Length only valid for I2C block data!\n");
 			help();
 		}
-		length = strtol(argv[flags+5], &end, 0);
+		length = strtol(argv[optind+4], &end, 0);
 		if (*end || length < 1 || length > I2C_SMBUS_BLOCK_MAX) {
 			fprintf(stderr, "Error: Length invalid!\n");
 			help();
--- i2c-tools.orig/tools/i2cset.c	2022-05-06 16:05:51.203591122 +0200
+++ i2c-tools/tools/i2cset.c	2022-05-06 16:15:05.005051554 +0200
@@ -2,7 +2,7 @@
     i2cset.c - A user-space program to write an I2C register.
     Copyright (C) 2001-2003  Frodo Looijaard <frodol@dds.nl>, and
                              Mark D. Studebaker <mdsxyz123@yahoo.com>
-    Copyright (C) 2004-2012  Jean Delvare <jdelvare@suse.de>
+    Copyright (C) 2004-2022  Jean Delvare <jdelvare@suse.de>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -162,31 +162,24 @@ int main(int argc, char *argv[])
 	int value, daddress, vmask = 0;
 	char filename[20];
 	int pec = 0;
-	int flags = 0;
+	int opt;
 	int force = 0, yes = 0, version = 0, readback = 0, all_addrs = 0;
 	unsigned char block[I2C_SMBUS_BLOCK_MAX];
 	int len;
 
 	/* handle (optional) flags first */
-	while (1+flags < argc && argv[1+flags][0] == '-') {
-		switch (argv[1+flags][1]) {
+	while ((opt = getopt(argc, argv, "Vafm:ry")) != -1) {
+		switch (opt) {
 		case 'V': version = 1; break;
 		case 'f': force = 1; break;
 		case 'y': yes = 1; break;
-		case 'm':
-			if (2+flags < argc)
-				maskp = argv[2+flags];
-			flags++;
-			break;
+		case 'm': maskp = optarg; break;
 		case 'r': readback = 1; break;
 		case 'a': all_addrs = 1; break;
-		default:
-			fprintf(stderr, "Error: Unsupported option "
-				"\"%s\"!\n", argv[1+flags]);
+		case '?':
 			help();
 			exit(1);
 		}
-		flags++;
 	}
 
 	if (version) {
@@ -194,33 +187,33 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
-	if (argc < flags + 4)
+	if (argc < optind + 3)
 		help();
 
-	i2cbus = lookup_i2c_bus(argv[flags+1]);
+	i2cbus = lookup_i2c_bus(argv[optind]);
 	if (i2cbus < 0)
 		help();
 
-	address = parse_i2c_address(argv[flags+2], all_addrs);
+	address = parse_i2c_address(argv[optind+1], all_addrs);
 	if (address < 0)
 		help();
 
-	daddress = strtol(argv[flags+3], &end, 0);
+	daddress = strtol(argv[optind+2], &end, 0);
 	if (*end || daddress < 0 || daddress > 0xff) {
 		fprintf(stderr, "Error: Data address invalid!\n");
 		help();
 	}
 
 	/* check for command/mode */
-	if (argc == flags + 4) {
+	if (argc == optind + 3) {
 		/* Implicit "c" */
 		size = I2C_SMBUS_BYTE;
-	} else if (argc == flags + 5) {
+	} else if (argc == optind + 4) {
 		/* "c", "cp",  or implicit "b" */
-		if (!strcmp(argv[flags+4], "c")
-		 || !strcmp(argv[flags+4], "cp")) {
+		if (!strcmp(argv[optind+3], "c")
+		 || !strcmp(argv[optind+3], "cp")) {
 			size = I2C_SMBUS_BYTE;
-			pec = argv[flags+4][1] == 'p';
+			pec = argv[optind+3][1] == 'p';
 		} else {
 			size = I2C_SMBUS_BYTE_DATA;
 		}
@@ -250,11 +243,11 @@ int main(int argc, char *argv[])
 				fprintf(stderr, "Error: Mask not supported for block writes!\n");
 				help();
 			}
-			if (argc > (int)sizeof(block) + flags + 5) {
+			if (argc > (int)sizeof(block) + optind + 4) {
 				fprintf(stderr, "Error: Too many arguments!\n");
 				help();
 			}
-		} else if (argc != flags + 6) {
+		} else if (argc != optind + 5) {
 			fprintf(stderr, "Error: Too many arguments!\n");
 			help();
 		}
@@ -270,7 +263,7 @@ int main(int argc, char *argv[])
 		break;
 	case I2C_SMBUS_BYTE_DATA:
 	case I2C_SMBUS_WORD_DATA:
-		value = strtol(argv[flags+4], &end, 0);
+		value = strtol(argv[optind+3], &end, 0);
 		if (*end || value < 0) {
 			fprintf(stderr, "Error: Data value invalid!\n");
 			help();
@@ -283,8 +276,8 @@ int main(int argc, char *argv[])
 		break;
 	case I2C_SMBUS_BLOCK_DATA:
 	case I2C_SMBUS_I2C_BLOCK_DATA:
-		for (len = 0; len + flags + 5 < argc; len++) {
-			value = strtol(argv[flags + len + 4], &end, 0);
+		for (len = 0; len + optind + 4 < argc; len++) {
+			value = strtol(argv[optind + len + 3], &end, 0);
 			if (*end || value < 0) {
 				fprintf(stderr, "Error: Data value invalid!\n");
 				help();
--- i2c-tools.orig/tools/i2ctransfer.c	2021-05-21 13:12:37.911513943 +0200
+++ i2c-tools/tools/i2ctransfer.c	2022-05-09 10:51:59.890334698 +0200
@@ -131,7 +131,7 @@ static int confirm(const char *filename,
 int main(int argc, char *argv[])
 {
 	char filename[20];
-	int i2cbus, address = -1, file, arg_idx = 1, nmsgs = 0, nmsgs_sent, i;
+	int i2cbus, address = -1, file, opt, arg_idx, nmsgs = 0, nmsgs_sent, i;
 	int force = 0, yes = 0, version = 0, verbose = 0, all_addrs = 0;
 	struct i2c_msg msgs[I2C_RDRW_IOCTL_MAX_MSGS];
 	enum parse_state state = PARSE_GET_DESC;
@@ -140,21 +140,18 @@ int main(int argc, char *argv[])
 	for (i = 0; i < I2C_RDRW_IOCTL_MAX_MSGS; i++)
 		msgs[i].buf = NULL;
 
-	/* handle (optional) arg_idx first */
-	while (arg_idx < argc && argv[arg_idx][0] == '-') {
-		switch (argv[arg_idx][1]) {
+	/* handle (optional) flags first */
+	while ((opt = getopt(argc, argv, "Vafvy")) != -1) {
+		switch (opt) {
 		case 'V': version = 1; break;
 		case 'v': verbose = 1; break;
 		case 'f': force = 1; break;
 		case 'y': yes = 1; break;
 		case 'a': all_addrs = 1; break;
-		default:
-			fprintf(stderr, "Error: Unsupported option \"%s\"!\n",
-				argv[arg_idx]);
+		case '?':
 			help();
 			exit(1);
 		}
-		arg_idx++;
 	}
 
 	if (version) {
@@ -162,6 +159,7 @@ int main(int argc, char *argv[])
 		exit(0);
 	}
 
+	arg_idx = optind;
 	if (arg_idx == argc) {
 		help();
 		exit(1);


-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] i2c-tools: Implement and document option -h
  2022-05-09  9:03 [PATCH v2 1/2] i2c-tools: Use getopt Jean Delvare
@ 2022-05-09  9:08 ` Jean Delvare
  2022-05-14 13:53   ` Wolfram Sang
  2022-05-14 13:53 ` [PATCH v2 1/2] i2c-tools: Use getopt Wolfram Sang
  1 sibling, 1 reply; 4+ messages in thread
From: Jean Delvare @ 2022-05-09  9:08 UTC (permalink / raw)
  To: Linux I2C; +Cc: Wolfram Sang, david kerns

Add an option -h to display the help of the tools. This is a common
practice so users may expect it to work.

Based on a preliminary patch from David Kerns.

Signed-off-by: Jean Delvare <jdelvare@suse.de>
---
Changes since v1:
 * Update the getopt strings so that option -h is actually accepted
 * Update i2ctransfer too
 * Update the date of all modified man pages

 tools/i2cdetect.8   |    8 +++++++-
 tools/i2cdetect.c   |    5 +++--
 tools/i2cdump.8     |    8 +++++++-
 tools/i2cdump.c     |    5 +++--
 tools/i2cget.8      |    8 +++++++-
 tools/i2cget.c      |    5 +++--
 tools/i2cset.8      |    8 +++++++-
 tools/i2cset.c      |    5 +++--
 tools/i2ctransfer.8 |    8 +++++++-
 tools/i2ctransfer.c |    5 +++--
 10 files changed, 50 insertions(+), 15 deletions(-)

--- i2c-tools.orig/tools/i2cdetect.8	2022-05-09 10:57:59.426739954 +0200
+++ i2c-tools/tools/i2cdetect.8	2022-05-09 11:05:28.840435141 +0200
@@ -1,4 +1,4 @@
-.TH I2CDETECT 8 "October 2017"
+.TH I2CDETECT 8 "May 2022"
 .SH NAME
 i2cdetect \- detect I2C chips
 
@@ -18,6 +18,9 @@ i2cdetect \- detect I2C chips
 .I -V
 .br
 .B i2cdetect
+.I -h
+.br
+.B i2cdetect
 .I -l
 
 .SH DESCRIPTION
@@ -78,6 +81,9 @@ Display the list of functionalities impl
 .B "\-V"
 Display the version and exit.
 .TP
+.B "\-h"
+Display the help and exit.
+.TP
 .B "\-l"
 Output a list of installed busses.
 
--- i2c-tools.orig/tools/i2cdetect.c	2022-05-09 11:05:28.832435039 +0200
+++ i2c-tools/tools/i2cdetect.c	2022-05-09 11:06:15.086024749 +0200
@@ -209,7 +209,7 @@ int main(int argc, char *argv[])
 	int yes = 0, version = 0, list = 0;
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "FValqry")) != -1) {
+	while ((opt = getopt(argc, argv, "FVahlqry")) != -1) {
 		switch (opt) {
 		case 'V': version = 1; break;
 		case 'y': yes = 1; break;
@@ -242,9 +242,10 @@ int main(int argc, char *argv[])
 			first = 0x00;
 			last = 0x7F;
 			break;
+		case 'h':
 		case '?':
 			help();
-			exit(1);
+			exit(opt == '?');
 		}
 	}
 
--- i2c-tools.orig/tools/i2cdump.8	2022-05-09 10:57:59.426739954 +0200
+++ i2c-tools/tools/i2cdump.8	2022-05-09 11:05:28.840435141 +0200
@@ -1,4 +1,4 @@
-.TH I2CDUMP 8 "June 2021"
+.TH I2CDUMP 8 "May 2022"
 .SH NAME
 i2cdump \- examine I2C registers
 
@@ -14,6 +14,9 @@ i2cdump \- examine I2C registers
 .br
 .B i2cdump
 .B -V
+.br
+.B i2cdump
+.B -h
 
 .SH DESCRIPTION
 i2cdump is a small helper program to examine registers
@@ -24,6 +27,9 @@ visible through the I2C bus.
 .B -V
 Display the version and exit.
 .TP
+.B -h
+Display the help and exit.
+.TP
 .B -f
 Force access to the device even if it is already busy. By default, i2cdump
 will refuse to access a device which is already under the control of a
--- i2c-tools.orig/tools/i2cdump.c	2022-05-09 11:05:28.832435039 +0200
+++ i2c-tools/tools/i2cdump.c	2022-05-09 11:06:22.017113117 +0200
@@ -116,16 +116,17 @@ int main(int argc, char *argv[])
 	int first = 0x00, last = 0xff;
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "Vafr:y")) != -1) {
+	while ((opt = getopt(argc, argv, "Vafhr:y")) != -1) {
 		switch (opt) {
 		case 'V': version = 1; break;
 		case 'f': force = 1; break;
 		case 'r': range = optarg; break;
 		case 'y': yes = 1; break;
 		case 'a': all_addrs = 1; break;
+		case 'h':
 		case '?':
 			help();
-			exit(1);
+			exit(opt == '?');
 		}
 	}
 
--- i2c-tools.orig/tools/i2cget.8	2022-05-09 10:57:59.426739954 +0200
+++ i2c-tools/tools/i2cget.8	2022-05-09 11:05:28.840435141 +0200
@@ -1,4 +1,4 @@
-.TH I2CGET 8 "July 2021"
+.TH I2CGET 8 "May 2022"
 .SH "NAME"
 i2cget \- read from I2C/SMBus chip registers
 
@@ -13,6 +13,9 @@ i2cget \- read from I2C/SMBus chip regis
 .br
 .B i2cget
 .B -V
+.br
+.B i2cget
+.B -h
 
 .SH DESCRIPTION
 i2cget is a small helper program to read registers visible through the I2C
@@ -23,6 +26,9 @@ bus (or SMBus).
 .B -V
 Display the version and exit.
 .TP
+.B -h
+Display the help and exit.
+.TP
 .B -f
 Force access to the device even if it is already busy. By default, i2cget
 will refuse to access a device which is already under the control of a
--- i2c-tools.orig/tools/i2cget.c	2022-05-09 11:05:28.832435039 +0200
+++ i2c-tools/tools/i2cget.c	2022-05-09 11:06:33.221255964 +0200
@@ -186,15 +186,16 @@ int main(int argc, char *argv[])
 	unsigned char block_data[I2C_SMBUS_BLOCK_MAX];
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "Vafy")) != -1) {
+	while ((opt = getopt(argc, argv, "Vafhy")) != -1) {
 		switch (opt) {
 		case 'V': version = 1; break;
 		case 'f': force = 1; break;
 		case 'y': yes = 1; break;
 		case 'a': all_addrs = 1; break;
+		case 'h':
 		case '?':
 			help();
-			exit(1);
+			exit(opt == '?');
 		}
 	}
 
--- i2c-tools.orig/tools/i2cset.8	2022-05-09 10:57:59.426739954 +0200
+++ i2c-tools/tools/i2cset.8	2022-05-09 11:05:28.840435141 +0200
@@ -1,4 +1,4 @@
-.TH I2CSET 8 "October 2017"
+.TH I2CSET 8 "May 2022"
 .SH "NAME"
 i2cset \- set I2C registers
 
@@ -18,6 +18,9 @@ i2cset \- set I2C registers
 .br
 .B i2cset
 .B -V
+.br
+.B i2cset
+.B -h
 
 .SH DESCRIPTION
 i2cset is a small helper program to set registers visible through the I2C
@@ -28,6 +31,9 @@ bus.
 .B -V
 Display the version and exit.
 .TP
+.B -h
+Display the help and exit.
+.TP
 .B -f
 Force access to the device even if it is already busy. By default, i2cset
 will refuse to access a device which is already under the control of a
--- i2c-tools.orig/tools/i2cset.c	2022-05-09 11:05:28.832435039 +0200
+++ i2c-tools/tools/i2cset.c	2022-05-09 11:06:40.280345962 +0200
@@ -168,7 +168,7 @@ int main(int argc, char *argv[])
 	int len;
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "Vafm:ry")) != -1) {
+	while ((opt = getopt(argc, argv, "Vafhm:ry")) != -1) {
 		switch (opt) {
 		case 'V': version = 1; break;
 		case 'f': force = 1; break;
@@ -176,9 +176,10 @@ int main(int argc, char *argv[])
 		case 'm': maskp = optarg; break;
 		case 'r': readback = 1; break;
 		case 'a': all_addrs = 1; break;
+		case 'h':
 		case '?':
 			help();
-			exit(1);
+			exit(opt == '?');
 		}
 	}
 
--- i2c-tools.orig/tools/i2ctransfer.8	2022-05-09 10:57:59.426739954 +0200
+++ i2c-tools/tools/i2ctransfer.8	2022-05-09 11:05:28.841435154 +0200
@@ -1,4 +1,4 @@
-.TH i2ctransfer 8 "February 2017"
+.TH i2ctransfer 8 "May 2022"
 .SH "NAME"
 i2ctransfer \- send user-defined I2C messages in one transfer
 
@@ -16,6 +16,9 @@ i2ctransfer \- send user-defined I2C mes
 .br
 .B i2ctransfer
 .B -V
+.br
+.B i2ctransfer
+.B -h
 
 .SH DESCRIPTION
 .B i2ctransfer
@@ -63,6 +66,9 @@ It will print infos about all messages s
 .B -V
 Display the version and exit.
 .TP
+.B -h
+Display the help and exit.
+.TP
 .B -a
 Allow using addresses between 0x00 - 0x07 and 0x78 - 0x7f. Not recommended.
 
--- i2c-tools.orig/tools/i2ctransfer.c	2022-05-09 11:05:28.832435039 +0200
+++ i2c-tools/tools/i2ctransfer.c	2022-05-09 11:06:47.812441993 +0200
@@ -141,16 +141,17 @@ int main(int argc, char *argv[])
 		msgs[i].buf = NULL;
 
 	/* handle (optional) flags first */
-	while ((opt = getopt(argc, argv, "Vafvy")) != -1) {
+	while ((opt = getopt(argc, argv, "Vafhvy")) != -1) {
 		switch (opt) {
 		case 'V': version = 1; break;
 		case 'v': verbose = 1; break;
 		case 'f': force = 1; break;
 		case 'y': yes = 1; break;
 		case 'a': all_addrs = 1; break;
+		case 'h':
 		case '?':
 			help();
-			exit(1);
+			exit(opt == '?');
 		}
 	}
 

-- 
Jean Delvare
SUSE L3 Support

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] i2c-tools: Use getopt
  2022-05-09  9:03 [PATCH v2 1/2] i2c-tools: Use getopt Jean Delvare
  2022-05-09  9:08 ` [PATCH v2 2/2] i2c-tools: Implement and document option -h Jean Delvare
@ 2022-05-14 13:53 ` Wolfram Sang
  1 sibling, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2022-05-14 13:53 UTC (permalink / raw)
  To: Jean Delvare; +Cc: Linux I2C, david kerns

[-- Attachment #1: Type: text/plain, Size: 1057 bytes --]

On Mon, May 09, 2022 at 11:03:26AM +0200, Jean Delvare wrote:
> Use the standard C getopt() function instead of parsing the options
> manually. The benefit of this move is that getopt() supports merged
> flags (for example -fy instead of -f -y) and does not require options
> to come first on the command line. As this is pretty standard by now,
> not supporting such possibilities can confuse and disappoint the user.
> 
> Based on a preliminary patch from David Kerns.
> 
> Signed-off-by: Jean Delvare <jdelvare@suse.de>
> ---
> Note: For i2ctransfer, I chose to keep arg_idx although it has the same
> meaning as getopt's optind variable, as I'm not sure if it is considered
> an acceptable practice for the caller to tinkle with it. If it is then
> we could get rid of arg_idx and use optind everywhere instead.

I think we can improve that incrementally once we find out. A quick
glimpse from my side also did not reveal any conclusion. So:

Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Thank you for doing it!


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] i2c-tools: Implement and document option -h
  2022-05-09  9:08 ` [PATCH v2 2/2] i2c-tools: Implement and document option -h Jean Delvare
@ 2022-05-14 13:53   ` Wolfram Sang
  0 siblings, 0 replies; 4+ messages in thread
From: Wolfram Sang @ 2022-05-14 13:53 UTC (permalink / raw)
  To: Jean Delvare; +Cc: Linux I2C, david kerns

[-- Attachment #1: Type: text/plain, Size: 352 bytes --]

On Mon, May 09, 2022 at 11:08:44AM +0200, Jean Delvare wrote:
> Add an option -h to display the help of the tools. This is a common
> practice so users may expect it to work.
> 
> Based on a preliminary patch from David Kerns.
> 
> Signed-off-by: Jean Delvare <jdelvare@suse.de>

Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-05-14 13:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-09  9:03 [PATCH v2 1/2] i2c-tools: Use getopt Jean Delvare
2022-05-09  9:08 ` [PATCH v2 2/2] i2c-tools: Implement and document option -h Jean Delvare
2022-05-14 13:53   ` Wolfram Sang
2022-05-14 13:53 ` [PATCH v2 1/2] i2c-tools: Use getopt Wolfram Sang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.