Commit fcf4e0ab authored by ∞'s avatar 💻

Merge branch 'jpic' into 'master'

Command group support

See merge request !1
parents 005f1be1 6cdbbc7b
Pipeline #1882 passed with stages
in 35 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, Group
from .console_script import ConsoleScript, BaseGroup, Group
from .test import autotest
from .cli import debug, docmod, help, run
......@@ -8,7 +8,7 @@ or to execute callables or help working with cli2 itself.
import textwrap
import types
from .console_script import ConsoleScript, Group
from .console_script import ConsoleScript, BaseGroup
from .command import command, option
from .exceptions import Cli2ArgsException
from .colors import GREEN, RED, RESET, YELLOW
......@@ -20,7 +20,7 @@ def docmod(module_name):
Example: cli2 docmod cli2
"""
return Group.factory(module_name).doc
return BaseGroup.factory(module_name).doc
@command(color=GREEN)
......@@ -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:
......@@ -53,7 +53,7 @@ def help(*args):
'Showing help for',
importable.module.__name__ + RESET
])
yield Group.factory(importable.module.__name__).doc
yield BaseGroup.factory(importable.module.__name__).doc
@command(color=GREEN)
......
......@@ -45,13 +45,14 @@ class GroupDocDescriptor:
self.value = value
class Group(collections.OrderedDict):
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 Group(collections.OrderedDict):
return cls(module_name, doc).add_module(module_name)
class ConsoleScript(Group):
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
Group.__init__(self, argv[0].split('/')[-1], doc)
super().__init__(argv[0].split('/')[-1], doc, default_command)
self.argv = argv
self.exit_code = 0
self.add_help()
......
......@@ -17,13 +17,13 @@ class Parser:
def parse(self):
from .introspection import Callable
from .console_script import Group
from .console_script import BaseGroup
for arg in self.argv_all:
if not self.command and arg in self.group:
item = self.group[arg]
if isinstance(item, Group):
if isinstance(item, BaseGroup):
self.group = item
elif isinstance(item, Callable):
self.command = item
......
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