Esempio n. 1
0
bool AnyOption::matchChar ( char c )
    {
        for ( int i = 0; i < optchar_counter; i ++ )
            {
                if ( optionchars[i] == c )
                    {     /* found match */
                        if ( optchartype[i] == COMMON_OPT || optchartype[i] == COMMAND_OPT )
                        { /* an option store and stop scanning */
                            return true;
                        }

                        else if ( optchartype[i] == COMMON_FLAG || optchartype[i] == COMMAND_FLAG )
                            { /* a flag store and keep scanning */
                                setFlagOn(c);
                                return false;
                            }
                    }
            }

        printVerbose("Unknown command argument option : ");
        printVerbose(c);
        printVerbose();
        printAutoUsage();
        return false;
    }
Esempio n. 2
0
int AnyOption::matchOpt ( char * opt )
    {
        for ( int i = 0; i < option_counter; i ++ )
            {
                if ( strcmp(options[i], opt) == 0 )
                    {
                        if ( optiontype[i] == COMMON_OPT || optiontype[i] == COMMAND_OPT )
                        { /* found option return index */
                            return i;
                        }

                        else if ( optiontype[i] == COMMON_FLAG || optiontype[i] == COMMAND_FLAG )
                            { /* found flag, set it */
                                setFlagOn(opt);
                                return - 1;
                            }
                    }
            }

        printVerbose("Unknown command argument option : ");
        printVerbose(opt);
        printVerbose();
        printAutoUsage();
        return - 1;
    }
Esempio n. 3
0
int AnyOption::parseGNU (char *arg) {
        int split_at = 0;
        /* if has a '=' sign get value */
        for (unsigned int i = 0; i < strlen (arg); i++) {
                if (arg[i] == equalsign) {
                        split_at = i;     /* store index */
                        i = strlen (arg); /* get out of loop */
                }
        }
        if (split_at > 0) { /* it is an option value pair */
                char *tmp = (char *)malloc ((split_at + 1) * sizeof (char));
                for (int i = 0; i < split_at; i++)
                        tmp[i] = arg[i];
                tmp[split_at] = '\0';

                if (matchOpt (tmp) >= 0) {
                        setValue (options[matchOpt (tmp)], arg + split_at + 1);
                        free (tmp);
                } else {
                        printVerbose ("Unknown command argument option : ");
                        printVerbose (arg);
                        printVerbose ();
                        printAutoUsage ();
                        free (tmp);
                        return -1;
                }
        } else { /* regular options with no '=' sign  */
                return matchOpt (arg);
        }
        return -1;
}
Esempio n. 4
0
char AnyOption::parsePOSIX ( char * arg )
    {
        for ( unsigned int i = 0; i < strlen(arg); i ++ )
            {
                char ch = arg[i];

                if ( matchChar(ch) )
                    { /* keep matching flags till an option */
                        /*if last char argv[++i] is the value */
                        if ( i == strlen(arg) - 1 )
                        {
                            return ch;
                        }
                        else
                            {         /* else the rest of arg is the value */
                                i ++; /* skip any '=' and ' ' */

                                while ( arg[i] == whitespace || arg[i] == equalsign )
                                    i ++;

                                setValue(ch, arg + i);
                                return '0';
                            }
                    }
            }

        printVerbose("Unknown command argument option : ");
        printVerbose(arg);
        printVerbose();
        printAutoUsage();
        return '0';
    }
Esempio n. 5
0
void
AnyOption::processCommandArgs()
{
    if(!(valueStoreOK() && CommandSet())) {
        return;
    }

    if(max_legal_args == 0) {
        max_legal_args = argc;
    }
    new_argv = (int*) malloc((max_legal_args+1) * sizeof(int));
    for(int i = 1 ; i < argc ; i++) { /* ignore first argv */
        if(argv[i][0] == long_opt_prefix[0] &&
           argv[i][1] == long_opt_prefix[1]) {  /* long GNU option */
            int match_at = parseGNU(argv[i]+2);   /* skip -- */
            if(match_at >= 0 && i < argc-1) { /* found match */
                setValue(options[match_at], argv[++i]);
            }
        } else if(argv[i][0] ==  opt_prefix_char) {   /* POSIX char */
            if(POSIX()) {
                char ch =  parsePOSIX(argv[i]+1);  /* skip - */
                if(ch != '0' && i < argc-1) { /* matching char */
                    setValue(ch,  argv[++i]);
                }
            } else { /* treat it as GNU option with a - */
                int match_at = parseGNU(argv[i]+1);   /* skip - */
                if(match_at >= 0 && i < argc-1) { /* found match */
                    setValue(options[match_at], argv[++i]);
                }
            }
        } else { /* not option but an argument keep index */
            if(new_argc < max_legal_args) {
                new_argv[ new_argc ] = i ;
                new_argc++;
            } else { /* ignore extra arguments */
                printVerbose("Ignoring extra argument: ");
                printVerbose(argv[i]);
                printVerbose();
                printAutoUsage();
            }
            printVerbose("Unknown command argument option : ");
            printVerbose(argv[i]);
            printVerbose();
            printAutoUsage();
        }
    }
}