int main_addreplacerg(int argc, char** argv) { parsed_opts_t* opts = NULL; state_t* state = NULL; if (!parse_args(argc, argv, &opts)) goto error; if (opts == NULL) return EXIT_SUCCESS; // Not an error but user doesn't want us to proceed if (!opts || !init(opts, &state)) goto error; if (!readgroupise(state)) goto error; cleanup_opts(opts); cleanup_state(state); return EXIT_SUCCESS; error: cleanup_opts(opts); cleanup_state(state); return EXIT_FAILURE; }
int main_split(int argc, char** argv) { int ret = 1; parsed_opts_t* opts = parse_args(argc, argv); if (!opts ) goto cleanup_opts; state_t* status = init(opts); if (!status) goto cleanup_opts; if (split(status)) ret = 0; cleanup_state(status); cleanup_opts: cleanup_opts(opts); return ret; }
static bool parse_args(int argc, char** argv, parsed_opts_t** opts) { *opts = NULL; int n; if (argc == 1) { usage(stdout); return true; } parsed_opts_t* retval = calloc(1, sizeof(parsed_opts_t)); if (! retval ) { fprintf(stderr, "[%s] Out of memory allocating parsed_opts_t\n", __func__); return false; } // Set defaults retval->mode = overwrite_all; sam_global_args_init(&retval->ga); static const struct option lopts[] = { SAM_OPT_GLOBAL_OPTIONS(0, 0, 'O', 0, 0), { NULL, 0, NULL, 0 } }; kstring_t rg_line = {0,0,NULL}; while ((n = getopt_long(argc, argv, "r:R:m:o:O:l:h", lopts, NULL)) >= 0) { switch (n) { case 'r': // Are we adding to existing rg line? if (ks_len(&rg_line) == 0) { if (strlen(optarg)<3 || (optarg[0] != '@' && optarg[1] != 'R' && optarg[2] != 'G')) { kputs("@RG\t", &rg_line); } } else { kputs("\t", &rg_line); } kputs(optarg, &rg_line); break; case 'R': retval->rg_id = strdup(optarg); break; case 'm': { if (strcmp(optarg, "overwrite_all") == 0) { retval->mode = overwrite_all; } else if (strcmp(optarg, "orphan_only") == 0) { retval->mode = orphan_only; } else { usage(stderr); return false; } break; } case 'o': retval->output_name = strdup(optarg); break; case 'h': usage(stdout); free(retval); return true; case '?': usage(stderr); free(retval); return false; case 'O': default: if (parse_sam_global_opt(n, optarg, lopts, &retval->ga) == 0) break; usage(stderr); free(retval); return false; } } retval->rg_line = ks_release(&rg_line); if (argc-optind < 1) { fprintf(stderr, "You must specify an input file.\n"); usage(stderr); cleanup_opts(retval); return false; } if (retval->rg_id && retval->rg_line) { fprintf(stderr, "The options -r and -R are mutually exclusive.\n"); cleanup_opts(retval); return false; } if (retval->rg_line) { char* tmp = basic_unescape(retval->rg_line); if ((retval->rg_id = get_rg_id(tmp)) == NULL) { fprintf(stderr, "[%s] The supplied RG line lacks an ID tag.\n", __func__); free(tmp); cleanup_opts(retval); return false; } retval->rg_line = tmp; } retval->input_name = strdup(argv[optind+0]); *opts = retval; return true; }