Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parser): delete doc-based command configuration parser #239

Merged
merged 6 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 0 additions & 31 deletions cleo/commands/command.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from __future__ import annotations

import inspect
import re

from typing import TYPE_CHECKING
from typing import Any
from typing import ContextManager
Expand All @@ -13,7 +10,6 @@
from cleo.io.inputs.string_input import StringInput
from cleo.io.null_io import NullIO
from cleo.io.outputs.output import Verbosity
from cleo.parser import Parser
from cleo.ui.table_separator import TableSeparator


Expand Down Expand Up @@ -47,40 +43,13 @@ def io(self) -> IO:
return self._io

def configure(self) -> None:
if not self.name:
doc = self.__doc__

if not doc:
for base in inspect.getmro(self.__class__):
if base.__doc__ is not None:
doc = base.__doc__
break

if doc:
self._parse_doc(doc)

for argument in self.arguments:
self._definition.add_argument(argument)

for option in self.options:
self._definition.add_option(option)

def _parse_doc(self, doc: str) -> None:
lines = doc.strip().split("\n", 1)
if len(lines) > 1:
self.description = lines[0].strip()
signature = re.sub(r"\s{2,}", " ", lines[1].strip())
definition = Parser.parse(signature)
self.name = definition["name"]

for argument in definition["arguments"]:
self._definition.add_argument(argument)

for option in definition["options"]:
self._definition.add_option(option)
else:
self.description = lines[0].strip()

def execute(self, io: IO) -> int:
self._io = io

Expand Down
185 changes: 0 additions & 185 deletions cleo/parser.py

This file was deleted.

10 changes: 4 additions & 6 deletions tests/commands/completion/fixtures/command_with_colons.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import annotations

from cleo.commands.command import Command
from cleo.helpers import option


class CommandWithColons(Command):
"""
Test.

command:with:colons
{ --goodbye }
"""
name = "command:with:colons"
options = [option("goodbye", flag=False)]
Secrus marked this conversation as resolved.
Show resolved Hide resolved
description = "Test."
19 changes: 12 additions & 7 deletions tests/commands/completion/fixtures/hello_command.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from __future__ import annotations

from cleo.commands.command import Command
from cleo.helpers import option


class HelloCommand(Command):
"""
Complete me please.

hello
{ --dangerous-option= : This $hould be `escaped`. }
{ --option-without-description }
"""
name = "hello"
options = [
option(
"dangerous-option",
flag=False,
default=" ",
Secrus marked this conversation as resolved.
Show resolved Hide resolved
description="This $hould be `escaped`.",
),
option("option-without-description", flag=False),
Secrus marked this conversation as resolved.
Show resolved Hide resolved
]
description = "Complete me please."
8 changes: 2 additions & 6 deletions tests/commands/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@


class MyCommand(Command):
"""
Command testing.

test
{action : The action to execute.}
"""
name = "test"
arguments = [argument("action", description="The action to execute.")]

def handle(self) -> int:
action = self.argument("action")
Expand Down
7 changes: 2 additions & 5 deletions tests/fixtures/inherited_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@


class ParentCommand(Command):
"""
Parent Command.

parent
"""
name = "parent"
description = "Parent Command."


class ChildCommand(ParentCommand):
Expand Down
18 changes: 12 additions & 6 deletions tests/fixtures/signature_command.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
from __future__ import annotations

from cleo.commands.command import Command
from cleo.helpers import argument
from cleo.helpers import option


class SignatureCommand(Command):
"""
description

signature:command {foo : Foo} {bar? : Bar} {--z|baz : Baz} {--Z|bazz : Bazz}
"""

name = "signature:command"
options = [
option("baz", "z", description="Baz"),
option("bazz", "Z", description="Bazz"),
]
arguments = [
argument("foo", description="Foo"),
argument("bar", description="Bar", optional=True),
]
help = "help"
description = "description"

def handle(self) -> int:
self.line("handle called")
Expand Down