From a895fcd61a1873060e0f301788b8ed6a0f6e08f9 Mon Sep 17 00:00:00 2001 From: jukoo Date: Sun, 1 Dec 2024 23:19:34 +0000 Subject: [PATCH] make flag option more dynamic instead the default one --- .gitignore | 1 + arghlp.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- arghlp.h | 17 +++++------------ main.c | 3 ++- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 75e72d9..8302b2f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build/* lib/* example/bin/* example/build/* +TODO.md diff --git a/arghlp.c b/arghlp.c index 0f72825..4e9a916 100644 --- a/arghlp.c +++ b/arghlp.c @@ -16,7 +16,7 @@ #include #include #include - +#include #include "arghlp.h" char __usage[USAGE_BUFF] = {0} ; @@ -169,3 +169,52 @@ static struct option * extract_getopt_option(struct option * g_opt) return g_opt ; } +char *optentry(char *const *av , int ac ,int optindex) +{ + + if(ac == optindex) + return optarg ; + + char *optfname= (char *) *(av + (optindex -1)) ; + + if(0x2d == (*optfname & 0xff) && 0x2d != (*(optfname +1) & 0xff )) + return optarg ; + + //! looking for equal symbol "=" + + char *equ_symb= strchr(optfname , 0x3d & 0xff ) ; + if (equ_symb) + { + size_t symb_offset= equ_symb - optfname ; + char *next= equ_symb+1; + if (*next == '\0') + { + goto __maybe_next; + } + + //! --option=argument + return strdup(next) ; + } + goto __maybe_next; + +__maybe_next: + char *optfname_value = (char *) *(av+optindex); + + if(0x2d == (*optfname_value & 0xff)) return optarg ; + + //! --option =args + if(0x3d == (*optfname_value & 0xff)) + { + optfname_value++ ; + if (0 == (*optfname_value & 0xff)) + { + optindex++ ; + goto __maybe_next ; + } + return strdup(optfname_value) ; + } + + //! --option= args + return strdup(optfname_value) ; + +} diff --git a/arghlp.h b/arghlp.h index 2481618..2dacbcf 100644 --- a/arghlp.h +++ b/arghlp.h @@ -126,19 +126,12 @@ struct arghlp { ARGHLP static char * get_program_basename(char *const * __argument_vector) __wur __nonnull((1)); ARGHLP static void * set_option( char * __shortopt , int __has_arg ) ; ARGHLP static char * build_short_option(const struct optionx * __raw_optionx) __wur __nonnull((1)); - -ARGHLP void static build_usage_helper(char* flags_t , - const char * __flag_description) __nonnull((1 ,2)); - -ARGHLP static char * make_sysnopsys(char *__basename , - struct arghlp * __synopsys, - char * __usage_body) __nonnull((1,2,3)) ; - +ARGHLP void static build_usage_helper(char* flags_t, const char * __flag_description) __nonnull((1,2)); +ARGHLP static char * make_sysnopsys(char *__basename, struct arghlp * __synopsys, char * __usage_body) __nonnull((1,2,3)) ; ARGHLP static struct option * extract_getopt_option(struct option * __g_option) __nonnull(()) ; +ARGHLP void *arghlp_context ( int __argcounter, char * const *__argvector , + const struct arghlp *__arghlp , void * __wur __argxtra) __nonnull((2,3)); -ARGHLP void *arghlp_context ( int __argcounter , - char * const *__argvector , - const struct arghlp *__arghlp , void * __wur __argxtra) - __nonnull((2,3)); +ARGHLP char *optentry(char * const * __av ,int __ac , int __optindex) ; #endif //!__argument_helper diff --git a/main.c b/main.c index f3618b3..dbfbd96 100644 --- a/main.c +++ b/main.c @@ -44,7 +44,8 @@ void * argparse(int ac ,char * const * av , const char *shortopt , struct opt fprintf(stdout, "input -> %s\n" , input) ; break; case 'o': - char *output = optarg ; + + char *output = optentry( av , ac , optind) ; fprintf(stdout, "output -> %s\n" , output) ; break;