builtin-cat-file.c: use parse_options()
This simplifies the option parsing. Signed-off-by: Michele Ballabio <barra_cuda@katamail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michele Ballabio committed
May 23, 2008 at 16:19 UTC
15d8e5651948e3914f4b765d7d1dc1bc2f5c4073
1 file changed
+46
-73
builtin-cat-file.c
+46
-73
@@ -8,6 +8,10 @@
8
#include "tag.h"
9
#include "tree.h"
10
#include "builtin.h"
11
+#include "parse-options.h"
12
+
13
+#define BATCH 1
14
+#define BATCH_CHECK 2
15
16
static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
17
{
@@ -158,7 +162,7 @@ static int batch_one_object(const char *obj_name, int print_contents)
162
return 0;
163
}
164
161
- if (print_contents)
165
+ if (print_contents == BATCH)
166
contents = read_sha1_file(sha1, &type, &size);
167
else
168
type = sha1_object_info(sha1, &size);
@@ -169,7 +173,7 @@ static int batch_one_object(const char *obj_name, int print_contents)
173
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
174
fflush(stdout);
175
172
- if (print_contents) {
176
+ if (print_contents == BATCH) {
177
write_or_die(1, contents, size);
178
printf("\n");
179
fflush(stdout);
@@ -192,89 +196,58 @@ static int batch_objects(int print_contents)
196
return 0;
197
}
198
195
-static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | [--batch|--batch-check] < <list_of_sha1s> ]";
199
+static const char * const cat_file_usage[] = {
200
+ "git-cat-file [-t|-s|-e|-p|<type>] <sha1>",
201
+ "git-cat-file [--batch|--batch-check] < <list_of_sha1s>",
202
+ NULL
203
+};
204
205
int cmd_cat_file(int argc, const char **argv, const char *prefix)
206
{
199
- int i, opt = 0, batch = 0, batch_check = 0;
207
+ int opt = 0, batch = 0;
208
const char *exp_type = NULL, *obj_name = NULL;
209
202
- git_config(git_default_config);
203
-
204
- for (i = 1; i < argc; ++i) {
205
- const char *arg = argv[i];
206
- int is_batch = 0, is_batch_check = 0;
207
-
208
- is_batch = !strcmp(arg, "--batch");
209
- if (!is_batch)
210
- is_batch_check = !strcmp(arg, "--batch-check");
211
-
212
- if (is_batch || is_batch_check) {
213
- if (opt) {
214
- error("git-cat-file: Can't use %s with -%c", arg, opt);
215
- usage(cat_file_usage);
216
- } else if (exp_type) {
217
- error("git-cat-file: Can't use %s when a type (\"%s\") is specified", arg, exp_type);
218
- usage(cat_file_usage);
219
- } else if (obj_name) {
220
- error("git-cat-file: Can't use %s when an object (\"%s\") is specified", arg, obj_name);
221
- usage(cat_file_usage);
222
- }
223
-
224
- if ((is_batch && batch_check) || (is_batch_check && batch)) {
225
- error("git-cat-file: Can't use %s with %s", arg, is_batch ? "--batch-check" : "--batch");
226
- usage(cat_file_usage);
227
- }
228
-
229
- if (is_batch)
230
- batch = 1;
231
- else
232
- batch_check = 1;
210
+ const struct option options[] = {
211
+ OPT_GROUP("<type> can be one of: blob, tree, commit, tag"),
212
+ OPT_SET_INT('t', NULL, &opt, "show object type", 't'),
213
+ OPT_SET_INT('s', NULL, &opt, "show object size", 's'),
214
+ OPT_SET_INT('e', NULL, &opt,
215
+ "exit with zero when there's no error", 'e'),
216
+ OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
217
+ OPT_SET_INT(0, "batch", &batch,
218
+ "show info and content of objects feeded on stdin", BATCH),
219
+ OPT_SET_INT(0, "batch-check", &batch,
220
+ "show info about objects feeded on stdin",
221
+ BATCH_CHECK),
222
+ OPT_END()
223
+ };
224
234
- continue;
235
- }
236
-
237
- if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
238
- if (batch || batch_check) {
239
- error("git-cat-file: Can't use %s with %s", arg, batch ? "--batch" : "--batch-check");
240
- usage(cat_file_usage);
241
- }
242
-
243
- exp_type = arg;
244
- opt = exp_type[1];
245
- continue;
246
- }
247
-
248
- if (arg[0] == '-')
249
- usage(cat_file_usage);
250
-
251
- if (!exp_type) {
252
- if (batch || batch_check) {
253
- error("git-cat-file: Can't specify a type (\"%s\") with %s", arg, batch ? "--batch" : "--batch-check");
254
- usage(cat_file_usage);
255
- }
256
-
257
- exp_type = arg;
258
- continue;
259
- }
225
+ git_config(git_default_config);
226
261
- if (obj_name)
262
- usage(cat_file_usage);
227
+ if (argc != 3 && argc != 2)
228
+ usage_with_options(cat_file_usage, options);
229
264
- // We should have hit one of the earlier if (batch || batch_check) cases before
265
- // getting here.
266
- assert(!batch);
267
- assert(!batch_check);
230
+ argc = parse_options(argc, argv, options, cat_file_usage, 0);
231
269
- obj_name = arg;
270
- break;
232
+ if (opt) {
233
+ if (argc == 1)
234
+ obj_name = argv[0];
235
+ else
236
+ usage_with_options(cat_file_usage, options);
237
+ }
238
+ if (!opt && !batch) {
239
+ if (argc == 2) {
240
+ exp_type = argv[0];
241
+ obj_name = argv[1];
242
+ } else
243
+ usage_with_options(cat_file_usage, options);
244
+ }
245
+ if (batch && (opt || argc)) {
246
+ usage_with_options(cat_file_usage, options);
247
}
248
273
- if (batch || batch_check)
249
+ if (batch)
250
return batch_objects(batch);
251
276
- if (!exp_type || !obj_name)
277
- usage(cat_file_usage);
278
-
252
return cat_one_file(opt, exp_type, obj_name);
253
}