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: str, opt_str: str, *long_opts: str, **kw: Any
)
| 685 | return strng |
| 686 | |
| 687 | def parse_options( |
| 688 | self, arg_str: str, opt_str: str, *long_opts: str, **kw: Any |
| 689 | ) -> tuple[Any, Any]: |
| 690 | """Parse options passed to an argument string. |
| 691 | |
| 692 | The interface is similar to that of :func:`getopt.getopt`, but it |
| 693 | returns a :class:`~IPython.utils.struct.Struct` with the options as keys |
| 694 | and the stripped argument string still as a string. |
| 695 | |
| 696 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
| 697 | This allows us to easily expand variables, glob files, quote |
| 698 | arguments, etc. |
| 699 | |
| 700 | Parameters |
| 701 | ---------- |
| 702 | arg_str : str |
| 703 | The arguments to parse. |
| 704 | opt_str : str |
| 705 | The options specification. |
| 706 | mode : str, default 'string' |
| 707 | If given as 'list', the argument string is returned as a list (split |
| 708 | on whitespace) instead of a string. |
| 709 | list_all : bool, default False |
| 710 | Put all option values in lists. Normally only options |
| 711 | appearing more than once are put in a list. |
| 712 | posix : bool, default True |
| 713 | Whether to split the input line in POSIX mode or not, as per the |
| 714 | conventions outlined in the :mod:`shlex` module from the standard |
| 715 | library. |
| 716 | """ |
| 717 | |
| 718 | # inject default options at the beginning of the input line |
| 719 | caller = sys._getframe(1).f_code.co_name |
| 720 | arg_str = "%s %s" % (self.options_table.get(caller, ""), arg_str) |
| 721 | |
| 722 | mode = kw.get("mode", "string") |
| 723 | if mode not in ["string", "list"]: |
| 724 | raise ValueError("incorrect mode given: %s" % mode) |
| 725 | # Get options |
| 726 | list_all = kw.get("list_all", 0) |
| 727 | posix = kw.get("posix", os.name == "posix") |
| 728 | strict = kw.get("strict", True) |
| 729 | |
| 730 | preserve_non_opts = kw.get("preserve_non_opts", False) |
| 731 | remainder_arg_str = arg_str |
| 732 | |
| 733 | # Check if we have more than one argument to warrant extra processing: |
| 734 | odict: dict[str, t.Any] = {} # Dictionary with options |
| 735 | args = arg_str.split() |
| 736 | if len(args) >= 1: |
| 737 | # If the list of inputs only has 0 or 1 thing in it, there's no |
| 738 | # need to look for options |
| 739 | argv = arg_split(arg_str, posix, strict) |
| 740 | # Do regular option processing |
| 741 | try: |
| 742 | opts, args = getopt(argv, opt_str, long_opts) |
| 743 | except GetoptError as e: |
| 744 | raise UsageError( |