i2c-tools: Use getopt
PATCH 1/2 i2c-tools: Use getopt
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.
--- tools/i2cdetect.c | 23 ++++++++++------------- tools/i2cdump.c | 49 +++++++++++++++++++++++-------------------------- tools/i2cget.c | 31 ++++++++++++++----------------- tools/i2cset.c | 45 +++++++++++++++++++-------------------------- 4 files changed, 66 insertions(+), 82 deletions(-)
tools/i2cdetect.c +10 -13
@@ -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 ); }
tools/i2cdump.c +23 -26
113 lines not on this page · raw message
tools/i2cget.c +14 -17
@@ -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 ();
tools/i2cset.c +19 -26
109 lines not on this page · raw message
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 2/2 i2c-tools: Implement and document option -h
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.
--- tools/i2cdetect.8 | 6 ++++++ tools/i2cdetect.c | 3 ++- tools/i2cdump.8 | 6 ++++++ tools/i2cdump.c | 3 ++- tools/i2cget.8 | 6 ++++++ tools/i2cget.c | 3 ++- tools/i2cset.8 | 6 ++++++ tools/i2cset.c | 3 ++- 8 files changed, 32 insertions(+), 4 deletions(-)
tools/i2cdetect.8 +6 -0
@@ -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.
tools/i2cdetect.c +2 -1
@@ -242,9 +242,10 @@ int main(int argc, char *argv[]) first =0x00 ; last =0x7F ;break ; +case 'h' :case '?' :help (); -exit (1 ); +exit (opt =='?' ); } }
tools/i2cdump.8 +6 -0
@@ -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
tools/i2cdump.c +2 -1
@@ -123,9 +123,10 @@ int main(int argc, char *argv[])case 'r' : range = optarg;break ;case 'y' : yes =1 ;break ;case 'a' : all_addrs =1 ;break ; +case 'h' :case '?' :help (); -exit (1 ); +exit (opt =='?' ); } }
tools/i2cget.8 +6 -0
@@ -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
tools/i2cget.c +2 -1
@@ -192,9 +192,10 @@ int main(int argc, char *argv[])case 'f' : force =1 ;break ;case 'y' : yes =1 ;break ;case 'a' : all_addrs =1 ;break ; +case 'h' :case '?' :help (); -exit (1 ); +exit (opt =='?' ); } }
tools/i2cset.8 +6 -0
@@ -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
tools/i2cset.c +2 -1
@@ -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 =='?' ); } }
Signature · 2 lines
Jean Delvare SUSE L3 Support
Quoted 5 lines
tools/i2cdetect.c | 23 ++++++++++------------- tools/i2cdump.c | 49 +++++++++++++++++++++++-------------------------- tools/i2cget.c | 31 ++++++++++++++----------------- tools/i2cset.c | 45 +++++++++++++++++++--------------------------
Yeah, useful change for sure! Any chance to tackle i2ctransfer as well while you are at it?
Hi Wolfram,
Quoted 7 lines · Wolfram Sang
Quoted 4 lines
tools/i2cdetect.c | 23 ++++++++++------------- tools/i2cdump.c | 49 +++++++++++++++++++++++-------------------------- tools/i2cget.c | 31 ++++++++++++++----------------- tools/i2cset.c | 45 +++++++++++++++++++--------------------------Yeah, useful change for sure! Any chance to tackle i2ctransfer as well while you are at it?
I did not want to step on your toes ;-) I'll look into applying the same changes to i2ctransfer if possible, no problem.
Signature · 2 lines
Jean Delvare SUSE L3 Support
Hi Jean,
Quoted 2 lines
I did not want to step on your toes ;-) I'll look into applying the same changes to i2ctransfer if possible, no problem.
Thank you, much appreciated! My toes are far away tackling other problems currently ;)
All the best,
Wolfram