Parse options passed to an argument string. The interface is similar to that of :func:`getopt.getopt`, but it returns a :class:`~IPython.utils.struct.Struct` with the options as keys and the stripped argument string still as a string. arg_str is quoted as a true sys
(self, arg_str, opt_str, *long_opts, **kw)
| 573 | return strng |
| 574 | |
| 575 | def parse_options(self, arg_str, opt_str, *long_opts, **kw): |
| 576 | """Parse options passed to an argument string. |
| 577 | |
| 578 | The interface is similar to that of :func:`getopt.getopt`, but it |
| 579 | returns a :class:`~IPython.utils.struct.Struct` with the options as keys |
| 580 | and the stripped argument string still as a string. |
| 581 | |
| 582 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
| 583 | This allows us to easily expand variables, glob files, quote |
| 584 | arguments, etc. |
| 585 | |
| 586 | Parameters |
| 587 | ---------- |
| 588 | |
| 589 | arg_str : str |
| 590 | The arguments to parse. |
| 591 | |
| 592 | opt_str : str |
| 593 | The options specification. |
| 594 | |
| 595 | mode : str, default 'string' |
| 596 | If given as 'list', the argument string is returned as a list (split |
| 597 | on whitespace) instead of a string. |
| 598 | |
| 599 | list_all : bool, default False |
| 600 | Put all option values in lists. Normally only options |
| 601 | appearing more than once are put in a list. |
| 602 | |
| 603 | posix : bool, default True |
| 604 | Whether to split the input line in POSIX mode or not, as per the |
| 605 | conventions outlined in the :mod:`shlex` module from the standard |
| 606 | library. |
| 607 | """ |
| 608 | |
| 609 | # inject default options at the beginning of the input line |
| 610 | caller = sys._getframe(1).f_code.co_name |
| 611 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
| 612 | |
| 613 | mode = kw.get('mode','string') |
| 614 | if mode not in ['string','list']: |
| 615 | raise ValueError('incorrect mode given: %s' % mode) |
| 616 | # Get options |
| 617 | list_all = kw.get('list_all',0) |
| 618 | posix = kw.get('posix', os.name == 'posix') |
| 619 | strict = kw.get('strict', True) |
| 620 | |
| 621 | # Check if we have more than one argument to warrant extra processing: |
| 622 | odict = {} # Dictionary with options |
| 623 | args = arg_str.split() |
| 624 | if len(args) >= 1: |
| 625 | # If the list of inputs only has 0 or 1 thing in it, there's no |
| 626 | # need to look for options |
| 627 | argv = arg_split(arg_str, posix, strict) |
| 628 | # Do regular option processing |
| 629 | try: |
| 630 | opts,args = getopt(argv, opt_str, long_opts) |
| 631 | except GetoptError as e: |
| 632 | raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, |