Improve argument parsing in cat-file
PATCH 2/3 Improve argument parsing in cat-file
Small refactoring of the command line parsing so we can more easily add new long options to the mix.
Signed-off-by Shawn O. Pearce <[email protected]>
--- builtin-cat-file.c | 27 ++++++++++++++------------- 1 files changed, 14 insertions(+), 13 deletions(-)
builtin-cat-file.c +14 -13
@@ -82,25 +82,26 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)enum object_type type;void *buf;unsigned long size; -int opt; -const char *exp_type, *obj_name; +int opt =0 , i; +const char *exp_type =NULL , *obj_name =NULL ;git_config (git_default_config); -if (argc !=3 ) +for (i =1 ; i < argc; i++) { +const char *a = argv[i]; +if (!opt && a[0 ] =='-' && a[1 ]) + opt = a[1 ]; +else if (!opt && !exp_type) + exp_type = a; +else if (!obj_name) + obj_name = a; +else +break ; + } +if (!obj_name)usage ("git-cat-file [-t|-s|-e|-p|<type>] <sha1>" ); - exp_type = argv[1 ]; - obj_name = argv[2 ]; -if (get_sha1 (obj_name, sha1))die ("Not a valid object name %s" , obj_name); - opt =0 ; -if ( exp_type[0 ] =='-' ) { - opt = exp_type[1 ]; -if ( !opt || exp_type[2 ] ) - opt =-1 ;/* Not a single character option */ - } - buf =NULL ;switch (opt) {case 't' :
Signature · 1 line
1.5.1.1.135.gf948