@matheusbsbs / matheus-bagliano / commits / 4814dbe830
Fork 0

git-cat-file: Make option parsing a little more flexible

This will make it easier to add newer options later. Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Roben committed Apr 23, 2008 at 15:17 UTC 4814dbe830baf5a72d8abbbfcc60eeea478b47c2
1 file changed +30 -12
builtin-cat-file.c
+30 -12
@@ -143,23 +143,41 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
143 return 0;
144 }
145
146 +static const char cat_file_usage[] = "git-cat-file [-t|-s|-e|-p|<type>] <sha1>";
147 +
148 int cmd_cat_file(int argc, const char **argv, const char *prefix)
149 {
148 - int opt;
149 - const char *exp_type, *obj_name;
150 + int i, opt = 0;
151 + const char *exp_type = NULL, *obj_name = NULL;
152
153 git_config(git_default_config);
152 - if (argc != 3)
153 - usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
154 - exp_type = argv[1];
155 - obj_name = argv[2];
156 -
157 - opt = 0;
158 - if ( exp_type[0] == '-' ) {
159 - opt = exp_type[1];
160 - if ( !opt || exp_type[2] )
161 - opt = -1; /* Not a single character option */
154 +
155 + for (i = 1; i < argc; ++i) {
156 + const char *arg = argv[i];
157 +
158 + if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
159 + exp_type = arg;
160 + opt = exp_type[1];
161 + continue;
162 + }
163 +
164 + if (arg[0] == '-')
165 + usage(cat_file_usage);
166 +
167 + if (!exp_type) {
168 + exp_type = arg;
169 + continue;
170 + }
171 +
172 + if (obj_name)
173 + usage(cat_file_usage);
174 +
175 + obj_name = arg;
176 + break;
177 }
178
179 + if (!exp_type || !obj_name)
180 + usage(cat_file_usage);
181 +
182 return cat_one_file(opt, exp_type, obj_name);
183 }