Esempio n. 1
0
 def __init__(self, msg, argument=None, onion_txt=None):
     """
     Parameters
     ----------
     msg : str
         The error message to be displayed.
     argument : str, optional
         The argument responsible for the error. if `None` (default),
         determines the argument name from `msg`, which will be the
         error message from an `argparse.ArgumentError` instance.
     onion_txt : str, optional
         The text of the installer arguments from onion comment in
         which the error occurred (i.e., the full onion comment with
         `# <installer>:` removed). If `None` (default), the error
         will not be displayed in Python's `SyntaxError`-specific
         traceback format.
     """
     if (msg is not None and argument is None
             and msg.startswith('argument ')):
         split_msg = msg.split()
         argument = split_msg[1].rstrip(':')
         msg = ' '.join(split_msg[2:])
     if (onion_txt is not None) and (argument is not None):
         # default sorting is alphabetical where '--a' comes before
         # '-a', so long option name will always be checked first,
         # which is what we want
         for aname in sorted(argument.split('/')):
             if aname in onion_txt:
                 target_offset = onion_txt.index(aname)
                 break
         else:
             target_offset = 0
     else:
         target_offset = 0
     # both `argparse.ArgumentError` and `SyntaxError` are
     # non-cooperative, so need to initialize them separately rather
     # than just running through the MRO via a call to super()
     ArgumentError.__init__(self, argument=None, message=msg)
     OnionParserError.__init__(self,
                               msg=msg,
                               target_text=onion_txt,
                               target_offset=target_offset)
     self.argument_name = argument