# Path: utils/cmdparse/cmdspec.py
# class OptionError(Exception):
#   """ An error occured while parsing the arguments or interpreting the cmdspec"""
#   def __init__(self,msg,*args):
#     """Initialise the Exception"""
#     self.msg=msg
#     self.args=args
#   def __str__(self):
#     """Convert the Exception to a string"""
#     if len(self.args)>0:
#       return self.msg+": "+", ".join(str(a) for a in self.args)
#     else:
#       return self.msg


from itertools import chain
from .cmdspec import OptionError
import textwrap
import sys
                        except ValueError as ex:
                            raise OptionError("Argument isn't of the right format for option '-"+c+"'",ex)
                    else:
                        outopts[opt.keyword]=opt.value
        else: # not an option so add it to the args (will check we are allowed an arg here later)
            outargs.append(arg)
    # parsed inargs so check if the outargs for number and then convert them to relevent types
    if len(outargs)<cmdspec.minarguments():
        raise OptionError("Not enough arguments provided")
    if (not cmdspec.repeatable) and len(outargs)>cmdspec.maxarguments():
        raise OptionError("Too many arguments provided")
    # can always use replast here as we have already checked for the required length
    outargs=[_convertarg(arg,spec) for arg,spec in zip(outargs,_replast(cmdspec.allarguments))]
    # return the parsed arguments and options
    return outargs,outopts

def _replast(a):
    """Return an iterator that returns the elements of a then repeates the last
    one forever."""
    last=None
    for el in a:
        last=el # store in case it is the last argument
        yield el
    while True:
        yield last

def _convertarg(arg,spec):
    try:
        return spec.conversion(arg)
    except ValueError as ex:

# Path: utils/cmdparse/cmdspec.py
# class OptionError(Exception):
#   """ An error occured while parsing the arguments or interpreting the cmdspec"""
#   def __init__(self,msg,*args):
#     """Initialise the Exception"""
#     self.msg=msg
#     self.args=args
#   def __str__(self):
#     """Convert the Exception to a string"""
#     if len(self.args)>0:
#       return self.msg+": "+", ".join(str(a) for a in self.args)
#     else:
#       return self.msg


# Path: utils/cmdparse/cmdparse.py
from itertools import chain
from .cmdspec import OptionError
import textwrap
import sys

                        except ValueError as ex:
                            raise OptionError("Argument isn't of the right format for option '-"+c+"'",ex)
                    else:
                        outopts[opt.keyword]=opt.value
        else: # not an option so add it to the args (will check we are allowed an arg here later)
            outargs.append(arg)
    # parsed inargs so check if the outargs for number and then convert them to relevent types
    if len(outargs)<cmdspec.minarguments():
        raise OptionError("Not enough arguments provided")
    if (not cmdspec.repeatable) and len(outargs)>cmdspec.maxarguments():
        raise OptionError("Too many arguments provided")
    # can always use replast here as we have already checked for the required length
    outargs=[_convertarg(arg,spec) for arg,spec in zip(outargs,_replast(cmdspec.allarguments))]
    # return the parsed arguments and options
    return outargs,outopts

def _replast(a):
    """Return an iterator that returns the elements of a then repeates the last
    one forever."""
    last=None
    for el in a:
        last=el # store in case it is the last argument
        yield el
    while True:
        yield last

def _convertarg(arg,spec):
    try:
        return spec.conversion(arg)
    except ValueError as ex:

        raise OptionError("Argument isn't of the right format for '"+spec.name+"'",ex)