Making argument required to be in quotation marks

  dictionary, python

I have this function, inputs are list of arguments and allowed arguments, it converts these args to dictionary for example

x = ["-a", "Avatar link", "-u", "user name"]
y = ["-u", "-a", "-id"]
output = {"-a": "Avatar link", "-u": "user name"}
def parseArgs(args:list, allowed_args:list):
    """Parse list of non-positional arguments and return a dictionary, inputs are args and allowed args"""
    dic = {}
    try:
        current_key = None
        for el in args:
            if el in allowed_args:
                current_key = el
                dic[current_key] = []
            else:
                dic[current_key].append(el)
            
        for key, val in dic.items():
            dic[key] = " ".join(val)
    except KeyError:
        raise KeyError
    finally:
        return dic

The problem is when user name is for example "-C o o k i e s-", program will see this as separate arg so -u will be empty and -C arg will be equal to "o o k i e s", c arg isnt allowed so it will throw an error. I want to make another variable "quoted_args" with list of allowed args where they must be specified in quotes so for example

quoted_args = ["-u"]
args = ["-u", '"-C o o k i e s-"']

And dict output will still be without quotation marks

dict = {"-u": "-C o o k i e s-"}
How can i achieve that?

Source: Python Questions

LEAVE A COMMENT