Commit 6cdbbc7b authored by ∞'s avatar 💻

Add command group support

parent 73d9cf82
Pipeline #1881 passed with stages
in 40 seconds
......@@ -5,6 +5,6 @@ from .exceptions import Cli2Exception, Cli2ArgsException
from .parser import Parser
from .introspection import docfile, Callable, Importable, DocDescriptor
from .command import command, option, Option
from .console_script import ConsoleScript, BaseGroup
from .console_script import ConsoleScript, BaseGroup, Group
from .test import autotest
from .cli import debug, docmod, help, run
......@@ -35,8 +35,8 @@ def help(*args):
console_script = ConsoleScript.singleton
if not args:
# show console script documentation
yield console_script.doc
# show documentation for parsed group
yield console_script.parser.group.doc
else:
# show command documentation if possible
if args[0] in console_script:
......
......@@ -48,10 +48,11 @@ class GroupDocDescriptor:
class BaseGroup(collections.OrderedDict):
doc = GroupDocDescriptor()
def __init__(self, name, doc=None):
def __init__(self, name, doc=None, default_command='help'):
self.name = name
if doc:
self.doc = doc
self.default_command = default_command
def add_help(self):
from .cli import help
......@@ -80,12 +81,25 @@ class BaseGroup(collections.OrderedDict):
return cls(module_name, doc).add_module(module_name)
class Group(Callable, BaseGroup):
doc = GroupDocDescriptor()
def __init__(self, name, doc=None, default_command='help', color=None,
options=None):
from .cli import help
BaseGroup.__init__(self, name, doc, default_command=default_command)
Callable.__init__(self, name, help, color=color, options=options)
self.add_help()
if doc:
self.doc = doc
class ConsoleScript(BaseGroup):
def __init__(self, doc=None, argv=None, default_command='help'):
ConsoleScript.singleton = self
self.default_command = default_command
argv = argv if argv is not None else sys.argv
super().__init__(argv[0].split('/')[-1], doc)
super().__init__(argv[0].split('/')[-1], doc, default_command)
self.argv = argv
self.exit_code = 0
self.add_help()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment