Я хотел бы написать собственную магию, которая получает параметр --first или --second, но вызывает ошибку, когда параметров вообще нет.В простом argparse я могу сделать это с mutually_exclusive_group, однако я не знаю, как применить это в случае Магии.Пример в жестком коде:
from IPython.core import magic_arguments
from IPython.core.magic import cell_magic, Magics, magics_class
@magics_class
class TestMagics(Magics):
@cell_magic
@magic_arguments.magic_arguments()
@magic_arguments.argument('--first', '-f',
action='store_true',
help='Whether to print the results'
)
@magic_arguments.argument('--second', '-s',
action='store_true',
help='Whether to print the results'
)
def hello(self, line='', cell=None):
args = magic_arguments.parse_argstring(self.hello, line)
if args.first or args.second:
print('hello ' + cell)
else:
print('error should occur!')
ip = get_ipython()
ip.register_magics(TestMagics)
В чанке:
%%hello
world
должно вызвать ошибку