#include "arg.ih"

bool Arg__::optionArg()
{
    string_view arg(*d_arg);

    if (arg.length() == 1 or arg[0] != '-')
    {
        d_argv.emplace_back(arg);
        return false;                   // a mere argument, maybe only '-'
    }

    if (arg[1] != '-')                  // a short argument
    {
        d_short = true;
        d_nextChar = arg[1];            // store the short arg. character
        d_option.clear();
        d_option.push_back(d_nextChar);
        d_tail = arg.substr(2);
        return true;
    }
    
    // option starts with two dashes

    d_short = false;
    d_assignment = false;
    d_tail.clear();

    if (arg.length() == 2)              // only two dashes
    {
        d_option = arg;
        return true;
    }

    // a long option

    size_t idx = arg.find('=');         // find the '=' char
    if (idx == string::npos)            // no '=' char
        d_option = arg;                 // arg contains the option
    else
    {
        d_assignment = true;
        d_option = arg.substr(0, idx);
        d_tail = arg.substr(idx + 1);   // what's beyond the '='
    }

    return true;
}
