Review nft_options_check()
Phil Sutter
reviewercover Review nft_options_check()
The hard-coded list of nft options which take an argument is a maintenance burden and also needless since the information is already present in struct nft_options array.
After some indenting cleanup in patch 1, turn the list of options to compare against into a loop over nft_options' elements.
Phil Sutter (2):
main: Reduce indenting in nft_options_check()
main: Refer to nft_options in nft_options_check()
src/main.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 1/2 main: Reduce indenting in nft_options_check()
No functional change intended.
--- src/main.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
src/main.c +16 -15
@@ -333,23 +333,24 @@ static bool nft_options_check(int argc, char * const argv[]) }else if (skip) { skip = false;continue ; - }else if (argv[i][0 ] =='-' ) { -if (nonoption) { -nft_options_error (argc, argv, pos); -return false; - }else if (argv[i][1 ] =='d' || - argv[i][1 ] =='I' || - argv[i][1 ] =='f' || - argv[i][1 ] =='D' || - !strcmp (argv[i],"--debug" ) || - !strcmp (argv[i],"--includepath" ) || - !strcmp (argv[i],"--define" ) || - !strcmp (argv[i],"--file" )) { - skip = true; -continue ; - } }else if (argv[i][0 ] !='-' ) { nonoption = true; +continue ; + } +if (nonoption) { +nft_options_error (argc, argv, pos); +return false; + } +if (argv[i][1 ] =='d' || + argv[i][1 ] =='I' || + argv[i][1 ] =='f' || + argv[i][1 ] =='D' || + !strcmp (argv[i],"--debug" ) || + !strcmp (argv[i],"--includepath" ) || + !strcmp (argv[i],"--define" ) || + !strcmp (argv[i],"--file" )) { + skip = true; +continue ; } }
Signature · 1 line
2.41.0
Phil Sutter
reviewerPATCH 2/2 main: Refer to nft_options in nft_options_check()
In reply to Phil SutterConsult the array when determining whether a given option is followed by an argument or not instead of hard-coding those that do. The array holds both short and long option name, so one extra pitfall removed.
--- src/main.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
src/main.c +9 -10
@@ -325,6 +325,7 @@ static bool nft_options_check(int argc, char * const argv[]) {bool skip = false, nonoption = false;int pos =0 , i; +size_t j;for (i =1 ; i < argc; i++) { pos +=strlen (argv[i -1 ]) +1 ;
@@ -341,16 +342,14 @@ static bool nft_options_check(int argc, char * const argv[])nft_options_error (argc, argv, pos);return false; } -if (argv[i][1 ] =='d' || - argv[i][1 ] =='I' || - argv[i][1 ] =='f' || - argv[i][1 ] =='D' || - !strcmp (argv[i],"--debug" ) || - !strcmp (argv[i],"--includepath" ) || - !strcmp (argv[i],"--define" ) || - !strcmp (argv[i],"--file" )) { - skip = true; -continue ; +for (j =0 ; j <NR_NFT_OPTIONS ; j++) { +if (nft_options[j].arg && + (argv[i][1 ] == (char )nft_options[j].val || + (argv[i][1 ] =='-' && + !strcmp (argv[i] +2 , nft_options[j].name)))) { + skip = true; +break ; + } } }
Signature · 1 line
2.41.0