Skip to content

Commit

Permalink
consistency with samtools for version and usage reporting
Browse files Browse the repository at this point in the history
- bcftools version/--version/--version-only as in samtools
- "Version" line in main usage message consitent with samtools
- bcftools help/--help (to stdout and return 0)
  • Loading branch information
mcshane committed Feb 21, 2014
1 parent 5933bf3 commit 8d0afcf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
58 changes: 32 additions & 26 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static cmd_t cmds[] =
},
{ .func = main_vcfnorm,
.alias = "norm",
.help = "normalize indels"
.help = "left-align normalize indels"
},
{ .func = main_vcfquery,
.alias = "query",
Expand Down Expand Up @@ -118,28 +118,20 @@ static cmd_t cmds[] =
}
};

char *bcftools_version_string = NULL;

char *bcftools_version(void)
{
if ( !bcftools_version_string )
{
int len = strlen(hts_version()) + strlen(BCFTOOLS_VERSION) + 9;
bcftools_version_string = (char*) malloc(len);
snprintf(bcftools_version_string,len,"%s+htslib-%s", BCFTOOLS_VERSION,hts_version());
}
return bcftools_version_string;
return BCFTOOLS_VERSION;
}

static int usage(void)
static void usage(FILE *fp)
{
fprintf(stderr, "\n");
fprintf(stderr, "Program: bcftools (Tools for variant calling and manipulating VCFs and BCFs)\n");
fprintf(stderr, "Version: %s\n", bcftools_version());
fprintf(stderr, "\n");
fprintf(stderr, "Usage: bcftools <command> <argument>\n");
fprintf(stderr, "\n");
fprintf(stderr, "Commands:\n");
fprintf(fp, "\n");
fprintf(fp, "Program: bcftools (Tools for variant calling and manipulating VCFs and BCFs)\n");
fprintf(fp, "Version: %s (using htslib %s)\n", bcftools_version(), hts_version());
fprintf(fp, "\n");
fprintf(fp, "Usage: bcftools <command> <argument>\n");
fprintf(fp, "\n");
fprintf(fp, "Commands:\n");

int i = 0;
const char *sep = NULL;
Expand All @@ -148,29 +140,43 @@ static int usage(void)
if ( !cmds[i].func ) sep = cmds[i].alias;
if ( sep )
{
printf("\n -- %s\n", sep);
fprintf(fp, "\n -- %s\n", sep);
sep = NULL;
}
if ( cmds[i].func && cmds[i].help[0]!='-' ) printf("\t%-15s %s\n", cmds[i].alias, cmds[i].help);
if ( cmds[i].func && cmds[i].help[0]!='-' ) fprintf(fp, "\t%-15s %s\n", cmds[i].alias, cmds[i].help);
i++;
}

fprintf(stderr,"\n");
return 1;
fprintf(fp,"\n");
}

int main(int argc, char *argv[])
{
if (argc < 2) return usage();
if (argc < 2) { usage(stderr); return 1; }

if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
printf("bcftools %s\nUsing htslib %s\nCopyright (C) 2014 Genome Research Ltd.\n", bcftools_version(), hts_version());
return 0;
}
else if (strcmp(argv[1], "--version-only") == 0) {
printf("%s+htslib-%s\n", bcftools_version(), hts_version());
return 0;
}
else if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
if (argc == 2) { usage(stdout); return 0; }
// Otherwise change "bcftools help COMMAND [...]" to "bcftools COMMAND";
// main_xyz() functions by convention display the subcommand's usage
// when invoked without any arguments.
argv++;
argc = 2;
}

int i = 0;
while (cmds[i].alias)
{
if ( !strcmp(argv[1],cmds[i].alias) )
{
int ret = cmds[i].func(argc-1,argv+1);
free(bcftools_version_string);
return ret;
return cmds[i].func(argc-1,argv+1);
}
i++;
}
Expand Down
2 changes: 1 addition & 1 deletion vcfmerge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ void merge_buffer(args_t *args)
void bcf_hdr_append_version(bcf_hdr_t *hdr, int argc, char **argv, const char *cmd)
{
kstring_t str = {0,0,0};
ksprintf(&str,"##%sVersion=%s\n", cmd, bcftools_version());
ksprintf(&str,"##%sVersion=%s+htslib-%s\n", cmd, bcftools_version(), hts_version());
bcf_hdr_append(hdr,str.s);

str.l = 0;
Expand Down

0 comments on commit 8d0afcf

Please sign in to comment.