Skip to content

Commit e77ffb7

Browse files
jmarshallpd3
authored andcommitted
Add apply_verbosity() function to implement --verbosity
Prior to C23, declaring a variable after a case label is an error. Introduce a function encapsulating --verbosity parsing to avoid needing a local variable within this option parsing case. Similarly in vcfmerge.c, avoid declaring a variable after a goto label.
1 parent 283023b commit e77ffb7

35 files changed

+48
-107
lines changed

bcftools.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ void error(const char *format, ...) HTS_NORETURN HTS_FORMAT(HTS_PRINTF_FMT, 1, 2
5050
// newline will be added by the function.
5151
void error_errno(const char *format, ...) HTS_NORETURN HTS_FORMAT(HTS_PRINTF_FMT, 1, 2);
5252

53+
// Set hts_verbose and return 0, or return -1 if str is not a valid integer
54+
int apply_verbosity(const char *str);
55+
5356
// For on the fly index creation with --write-index
5457
int init_index2(htsFile *fh, bcf_hdr_t *hdr, const char *fname, char **idx_fname, int idx_fmt);
5558
int init_index(htsFile *fh, bcf_hdr_t *hdr, const char *fname, char **idx_fname);

consensus.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,15 +1259,12 @@ int main_consensus(int argc, char *argv[])
12591259
{0,0,0,0}
12601260
};
12611261
int c;
1262-
char *tmp;
12631262
while ((c = getopt_long(argc, argv, "h?s:S:1Ii:e:H:f:o:m:c:M:p:a:v:",loptions,NULL)) >= 0)
12641263
{
12651264
switch (c)
12661265
{
12671266
case 'v':
1268-
int verbose = strtol(optarg,&tmp,10);
1269-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
1270-
if ( verbose > 3 ) hts_verbose = verbose;
1267+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
12711268
break;
12721269
case 1 : args->mark_del = optarg[0]; break;
12731270
case 2 :

mpileup.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,13 +1469,10 @@ int main_mpileup(int argc, char *argv[])
14691469
{"verbosity",required_argument,NULL,'v'},
14701470
{NULL, 0, NULL, 0}
14711471
};
1472-
char *tmp;
14731472
while ((c = getopt_long(argc, argv, "Ag:f:r:R:q:Q:C:BDd:L:b:P:po:e:h:Im:F:EG:6O:xa:s:S:t:T:M:X:UW::v:",lopts,NULL)) >= 0) {
14741473
switch (c) {
14751474
case 'v':
1476-
int verbose = strtol(optarg,&tmp,10);
1477-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
1478-
if ( verbose > 3 ) hts_verbose = verbose;
1475+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
14791476
break;
14801477
case 'x': mplp.flag &= ~MPLP_SMART_OVERLAPS; break;
14811478
case 16 :

plugins/check-sparsity.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ int run(int argc, char **argv)
246246
switch (c)
247247
{
248248
case 'v':
249-
int verbose = strtol(optarg,&tmp,10);
250-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
251-
if ( verbose > 3 ) hts_verbose = verbose;
249+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
252250
break;
253251
case 'n':
254252
args->min_sites = strtol(optarg,&tmp,10);

plugins/contrast.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,7 @@ int run(int argc, char **argv)
499499
switch (c)
500500
{
501501
case 'v':
502-
int verbose = strtol(optarg,&tmp,10);
503-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
504-
if ( verbose > 3 ) hts_verbose = verbose;
502+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
505503
break;
506504
case 1 : args->force_samples = 1; break;
507505
case 'f': args->max_AC_str = optarg; break;

plugins/gvcfz.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,7 @@ int run(int argc, char **argv)
347347
switch (c)
348348
{
349349
case 'v':
350-
int verbose = strtol(optarg,&tmp,10);
351-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
352-
if ( verbose > 3 ) hts_verbose = verbose;
350+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
353351
break;
354352
case 'a': args->trim_alts = 1; break;
355353
case 'e':

plugins/indel-stats.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,7 @@ int run(int argc, char **argv)
706706
switch (c)
707707
{
708708
case 'v':
709-
int verbose = strtol(optarg,&tmp,10);
710-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
711-
if ( verbose > 3 ) hts_verbose = verbose;
709+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
712710
break;
713711
case 1 :
714712
MAX_LEN = strtod(optarg,&tmp);

plugins/isecGT.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ int run(int argc, char **argv)
9999
switch (c)
100100
{
101101
case 'v':
102-
int verbose = strtol(optarg,&tmp,10);
103-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
104-
if ( verbose > 3 ) hts_verbose = verbose;
102+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
105103
break;
106104
case 'o': args->output_fname = optarg; break;
107105
case 'O':

plugins/mendelian2.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,9 +832,7 @@ int run(int argc, char **argv)
832832
switch (c)
833833
{
834834
case 'v':
835-
int verbose = strtol(optarg,&tmp,10);
836-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
837-
if ( verbose > 3 ) hts_verbose = verbose;
835+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
838836
break;
839837
case 'e':
840838
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n");

plugins/parental-origin.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ int run(int argc, char **argv)
345345
switch (c)
346346
{
347347
case 'v':
348-
int verbose = strtol(optarg,&tmp,10);
349-
if ( *tmp || verbose<0 ) error("Could not parse argument: --verbosity %s\n", optarg);
350-
if ( verbose > 3 ) hts_verbose = verbose;
348+
if ( apply_verbosity(optarg) < 0 ) error("Could not parse argument: --verbosity %s\n", optarg);
351349
break;
352350
case 'e':
353351
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n");

0 commit comments

Comments
 (0)