scriptconfig.modal module

The scriptconfig ModalCLI

This module defines a way to group several smaller scriptconfig CLIs into a single parent CLI that chooses between them “modally”. E.g. if we define two configs: do_foo and do_bar, we use ModalCLI to define a parent program that can run one or the other. Let’s make this more concrete.

CommandLine

xdoctest -m scriptconfig.modal __doc__:0

Example

>>> import scriptconfig as scfg
>>> #
>>> class DoFooCLI(scfg.DataConfig):
>>>     __command__ = 'do_foo'
>>>     option1 = scfg.Value(None, help='option1')
>>>     #
>>>     @classmethod
>>>     def main(cls, cmdline=1, **kwargs):
>>>         self = cls.cli(cmdline=cmdline, data=kwargs)
>>>         print('Called Foo with: ' + str(self))
>>> #
>>> class DoBarCLI(scfg.DataConfig):
>>>     __command__ = 'do_bar'
>>>     option1 = scfg.Value(None, help='option1')
>>>     #
>>>     @classmethod
>>>     def main(cls, cmdline=1, **kwargs):
>>>         self = cls.cli(cmdline=cmdline, data=kwargs)
>>>         print('Called Bar with: ' + str(self))
>>> #
>>> #
>>> class MyModalCLI(scfg.ModalCLI):
>>>     __version__ = '1.2.3'
>>>     foo = DoFooCLI
>>>     bar = DoBarCLI
>>> #
>>> modal = MyModalCLI()
>>> MyModalCLI.main(argv=['do_foo'])
>>> #MyModalCLI.main(argv=['do-foo'])
>>> MyModalCLI.main(argv=['--version'])
>>> try:
>>>     MyModalCLI.main(argv=['--help'])
>>> except SystemExit:
>>>     print('prevent system exit due to calling --help')
class scriptconfig.modal.MetaModalCLI(name, bases, namespace, *args, **kwargs)[source]

Bases: type

A metaclass to help minimize boilerplate when defining a ModalCLI

class scriptconfig.modal.ModalCLI(description='', sub_clis=None, version=None)[source]

Bases: object

Contains multiple scriptconfig.Config items with corresponding main functions.

CommandLine

xdoctest -m scriptconfig.modal ModalCLI

Example

>>> from scriptconfig.modal import *  # NOQA
>>> import scriptconfig as scfg
>>> self = ModalCLI(description='A modal CLI')
>>> #
>>> @self.register
>>> class Command1Config(scfg.Config):
>>>     __command__ = 'command1'
>>>     __default__ = {
>>>         'foo': 'spam'
>>>     }
>>>     @classmethod
>>>     def main(cls, cmdline=1, **kwargs):
>>>         config = cls(cmdline=cmdline, data=kwargs)
>>>         print('config1 = {}'.format(ub.urepr(dict(config), nl=1)))
>>> #
>>> @self.register
>>> class Command2Config(scfg.DataConfig):
>>>     __command__ = 'command2'
>>>     foo = 'eggs'
>>>     baz = 'biz'
>>>     @classmethod
>>>     def main(cls, cmdline=1, **kwargs):
>>>         config = cls.cli(cmdline=cmdline, data=kwargs)
>>>         print('config2 = {}'.format(ub.urepr(dict(config), nl=1)))
>>> #
>>> parser = self.argparse()
>>> parser.print_help()
...
A modal CLI
...
commands:
  {command1,command2}  specify a command to run
    command1           argparse CLI generated by scriptconfig...
    command2           argparse CLI generated by scriptconfig...
>>> self.run(argv=['command1'])
config1 = {
    'foo': 'spam',
}
>>> self.run(argv=['command2', '--baz=buz'])
config2 = {
    'foo': 'eggs',
    'baz': 'buz',
}

CommandLine

xdoctest -m scriptconfig.modal ModalCLI:1

Example

>>> # Declarative modal CLI (new in 0.7.9)
>>> import scriptconfig as scfg
>>> class MyModalCLI(scfg.ModalCLI):
>>>     #
>>>     class Command1(scfg.DataConfig):
>>>         __command__ = 'command1'
>>>         foo = scfg.Value('spam', help='spam spam spam spam')
>>>         @classmethod
>>>         def main(cls, cmdline=1, **kwargs):
>>>             config = cls.cli(cmdline=cmdline, data=kwargs)
>>>             print('config1 = {}'.format(ub.urepr(dict(config), nl=1)))
>>>     #
>>>     class Command2(scfg.DataConfig):
>>>         __command__ = 'command2'
>>>         foo = 'eggs'
>>>         baz = 'biz'
>>>         @classmethod
>>>         def main(cls, cmdline=1, **kwargs):
>>>             config = cls.cli(cmdline=cmdline, data=kwargs)
>>>             print('config2 = {}'.format(ub.urepr(dict(config), nl=1)))
>>> #
>>> MyModalCLI.main(argv=['command1'])
>>> MyModalCLI.main(argv=['command2', '--baz=buz'])

Example

>>> # Declarative modal CLI (new in 0.7.9)
>>> import scriptconfig as scfg
>>> class MyModalCLI(scfg.ModalCLI):
>>>     ...
>>> #
>>> @MyModalCLI.register
>>> class Command1(scfg.DataConfig):
>>>     __command__ = 'command1'
>>>     foo = scfg.Value('spam', help='spam spam spam spam')
>>>     @classmethod
>>>     def main(cls, cmdline=1, **kwargs):
>>>         config = cls.cli(cmdline=cmdline, data=kwargs)
>>>         print('config1 = {}'.format(ub.urepr(dict(config), nl=1)))
>>> #
>>> @MyModalCLI.register
>>> class Command2(scfg.DataConfig):
>>>     __command__ = 'command2'
>>>     foo = 'eggs'
>>>     baz = 'biz'
>>>     @classmethod
>>>     def main(cls, cmdline=1, **kwargs):
>>>         config = cls.cli(cmdline=cmdline, data=kwargs)
>>>         print('config2 = {}'.format(ub.urepr(dict(config), nl=1)))
>>> #
>>> MyModalCLI.main(argv=['command1'])
>>> MyModalCLI.main(argv=['command2', '--baz=buz'])
property sub_clis
classmethod register(cli_cls)[source]
Parameters:

cli_cli (scriptconfig.Config) – A CLI-aware config object to register as a sub CLI

_build_subcmd_infos()[source]
_parserkw()[source]

Generate the kwargs for making a new argparse.ArgumentParser

argparse(parser=None, special_options=Ellipsis)[source]

Builds a new argparse object for this ModalCLI or extends an existing one with it.

build_parser(parser=None, special_options=Ellipsis)

Builds a new argparse object for this ModalCLI or extends an existing one with it.

classmethod main(argv=None, strict=True, autocomplete='auto')[source]

Execute the modal CLI as the main script

classmethod run(argv=None, strict=True, autocomplete='auto')

Execute the modal CLI as the main script