Add babeltrace option parsing
[babeltrace.git] / converter / babeltrace.c
CommitLineData
34ac0e6c
MD
1/*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
4c8bfb7e 18
34ac0e6c
MD
19#include <babeltrace/babeltrace.h>
20#include <popt.h>
21#include <errno.h>
22#include <stdlib.h>
4c8bfb7e 23
34ac0e6c
MD
24static const char *opt_input_format;
25static const char *opt_output_format;
26
27static const char *opt_input_path;
28static const char *opt_output_path;
29
30int babeltrace_verbose, babeltrace_debug;
31
32enum {
33 OPT_NONE = 0,
34 OPT_HELP,
35 OPT_VERBOSE,
36 OPT_DEBUG,
37};
38
39static struct poptOption long_options[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
42 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
43 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
44 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
45 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
46 { NULL, 0, 0, NULL, 0, NULL, NULL },
47};
48
49static void usage(FILE *fp)
4c8bfb7e 50{
34ac0e6c
MD
51 fprintf(fp, "Babeltrace %u.%u\n\n", BABELTRACE_VERSION_MAJOR,
52 BABELTRACE_VERSION_MINOR);
53 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT OUTPUT\n");
54 fprintf(fp, "\n");
55 fprintf(fp, " INPUT Input trace path\n");
56 fprintf(fp, " OUTPUT Output trace path\n");
57 fprintf(fp, "\n");
58 fprintf(fp, " -i, --input-format Input trace path\n");
59 fprintf(fp, " -o, --output-format Input trace path\n");
60 fprintf(fp, "\n");
61 fprintf(fp, " -h, --help This help message\n");
62 fprintf(fp, " -v, --verbose Verbose mode\n");
63 fprintf(fp, " -d, --debug Debug mode\n");
64 fprintf(fp, "\n");
65}
66
67/*
68 * Return 0 if caller should continue, < 0 if caller should return
69 * error, > 0 if caller should exit without reporting error.
70 */
71static int parse_options(int argc, const char **argv)
72{
73 poptContext pc;
74 int opt, ret = 0;
75
76 pc = poptGetContext(NULL, argc, argv, long_options, 0);
77 poptReadDefaultConfig(pc, 0);
78
79 while ((opt = poptGetNextOpt(pc)) != -1) {
80 switch (opt) {
81 case OPT_HELP:
82 usage(stderr);
83 ret = 1; /* exit cleanly */
84 goto end;
85 case OPT_VERBOSE:
86 babeltrace_verbose = 1;
87 break;
88 case OPT_DEBUG:
89 babeltrace_debug = 1;
90 break;
91 default:
92 ret = -EINVAL;
93 goto end;
94 }
95 }
96
97 opt_input_path = poptGetArg(pc);
98 if (!opt_input_path) {
99 ret = -EINVAL;
100 goto end;
101 }
102 opt_output_path = poptGetArg(pc);
103 if (!opt_output_path) {
104 ret = -EINVAL;
105 goto end;
106 }
107end:
108 if (pc) {
109 poptFreeContext(pc);
110 }
111 return ret;
112}
113
114int main(int argc, const char **argv)
115{
116 int ret;
117
118 ret = parse_options(argc, argv);
119 if (ret < 0) {
120 fprintf(stdout, "Error parsing options.\n");
121 usage(stdout);
122 exit(EXIT_FAILURE);
123 } else if (ret > 0) {
124 exit(EXIT_SUCCESS);
125 }
126 printf_verbose("Verbose mode active.\n");
127 printf_debug("Debug mode active.\n");
128
129 printf_verbose("Converting from file: %s\n", opt_input_path);
130 printf_verbose("Converting from format: %s\n",
131 opt_input_format ? : "<autodetect>");
132 printf_verbose("Converting to file: %s\n", opt_output_path);
133 printf_verbose("Converting to format: %s\n",
134 opt_output_format ? : "CTF");
135
4c8bfb7e
MD
136 return 0;
137}
This page took 0.04032 seconds and 4 git commands to generate.