コード例 #1
0
def _parse_record(parser, record_token):

    global SUPPORTED_RECORDS

    # match parser to record type
    record_type = None
    for i in range(3):
        try:
            if record_token[i] in SUPPORTED_RECORDS:
                record_type = record_token[i]
                break
        except KeyError:
            break

    if not record_type:
        from azure.cli.core.util import CLIError
        raise CLIError('Unable to determine record type: {}'.format(
            ' '.join(record_token)))

    # move the record type to the front of the token list so it will conform to argparse
    if record_type != parser.prog:
        raise IncorrectParserException

    try:
        rr, unmatched = parser.parse_known_args(record_token)
        assert len(unmatched) == 0, "Unmatched fields: %s" % unmatched
    except (SystemExit, AssertionError, InvalidLineException) as ex:
        # invalid argument
        raise InvalidLineException(' '.join(record_token))

    record = rr.__dict__
    record['type'] = record_type

    # remove emtpy fields
    for field in list(record.keys()):
        if record[field] is None:
            del record[field]

    for key in record:
        try:
            if len(record[key]) == 1:
                record[key] = record[key][0]
        except TypeError:
            continue

    return record
コード例 #2
0
 def error(self, message):
     """
     Silent error message
     """
     raise InvalidLineException(message)