MCPcopy Create free account
hub / github.com/ipython/ipython / parse_options

Method parse_options

IPython/core/magic.py:575–654  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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,

Callers 15

cpasteMethod · 0.80
pasteMethod · 0.80
test_magic_parse_optionsFunction · 0.80
test_parse_optionsFunction · 0.80
_run_edit_testFunction · 0.80
logstartMethod · 0.80
prunMethod · 0.80
runMethod · 0.80
timeitMethod · 0.80
macroMethod · 0.80
rerunMethod · 0.80

Calls 3

UsageErrorClass · 0.85
StructClass · 0.85
arg_splitFunction · 0.50

Tested by 4

test_magic_parse_optionsFunction · 0.64
test_parse_optionsFunction · 0.64
_run_edit_testFunction · 0.64