{"thread":{"id":"7773","subject":"[PATCH 2/3] Improve argument parsing in cat-file","startedAt":"2007-04-22T01:14:43Z","lastAt":"2007-04-22T01:14:43Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"40079","messageId":"20070422011443.GB2910@spearce.org","threadId":"7773","inReplyTo":null,"subject":"[PATCH 2/3] Improve argument parsing in cat-file","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-04-22T01:14:43Z","receivedAt":"2007-04-22T01:14:43Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Small refactoring of the command line parsing so we can\nmore easily add new long options to the mix.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n builtin-cat-file.c |   27 ++++++++++++++-------------\n 1 files changed, 14 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin-cat-file.c b/builtin-cat-file.c\nindex f132d58..b2437fe 100644\n--- a/builtin-cat-file.c\n+++ b/builtin-cat-file.c\n@@ -82,25 +82,26 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \tenum object_type type;\n \tvoid *buf;\n \tunsigned long size;\n-\tint opt;\n-\tconst char *exp_type, *obj_name;\n+\tint opt = 0, i;\n+\tconst char *exp_type = NULL, *obj_name = NULL;\n \n \tgit_config(git_default_config);\n-\tif (argc != 3)\n+\tfor (i = 1; i < argc; i++) {\n+\t\tconst char *a = argv[i];\n+\t\tif (!opt && a[0] == '-' && a[1])\n+\t\t\topt = a[1];\n+\t\telse if (!opt && !exp_type)\n+\t\t\texp_type = a;\n+\t\telse if (!obj_name)\n+\t\t\tobj_name = a;\n+\t\telse\n+\t\t\tbreak;\n+\t}\n+\tif (!obj_name)\n \t\tusage(\"git-cat-file [-t|-s|-e|-p|<type>] <sha1>\");\n-\texp_type = argv[1];\n-\tobj_name = argv[2];\n-\n \tif (get_sha1(obj_name, sha1))\n \t\tdie(\"Not a valid object name %s\", obj_name);\n \n-\topt = 0;\n-\tif ( exp_type[0] == '-' ) {\n-\t\topt = exp_type[1];\n-\t\tif ( !opt || exp_type[2] )\n-\t\t\topt = -1; /* Not a single character option */\n-\t}\n-\n \tbuf = NULL;\n \tswitch (opt) {\n \tcase 't':\n-- \n1.5.1.1.135.gf948\n"}]}