#include "arg.ih"

bool Arg__::shortOption()
{
    size_t idx = d_optStr.find(d_nextChar);

    if (idx == string::npos)
    {
        if (d_unspecOK)
        {
            d_argv.emplace_back(string({ '-', d_nextChar }) );
            redoTail();
            return true;
        }
   
        d_optionType = NO_OPTION;
        return false;
    }

    if (d_optStr[idx + 1] != ':')       // no argument is required
    {
        shortOptionValue("");
        redoTail();
    }
                                        // an argument is (maybe) required
    else if (not d_tail.empty())        // the tail becomes the value
        shortOptionValue(d_tail);

    else if (d_arg + 1 != d_end)        // no tail, but there is another arg.
        shortOptionValue(*++d_arg);     // store the next arg

    else if (d_optStr[idx + 2] == ':')  // an arg is optional 
        shortOptionValue("");

    else
    {
        d_optionType = NO_ARGUMENT;
        return false;
    }

    return true;        
}
