-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.py
37 lines (29 loc) · 1 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import annotations
from typing import NamedTuple
from settings import DEFAULT_PREFIX
from utils import remove_prefix
class Command(NamedTuple):
name: str
raw_args: str
args: list[str]
@classmethod
def from_str(cls, s: str, prefix: str = DEFAULT_PREFIX) -> 'Command':
if not s.startswith(prefix):
raise ValueError(f'`{s}` does not start with `{prefix}`')
stripped = remove_prefix(text=s, prefix=prefix)
cmd_name, *raw_args = [part.strip() for part in stripped.split(' ', maxsplit=1)]
_, *args = [part.strip() for part in stripped.split()]
return Command(
name=cmd_name,
raw_args=''.join(raw_args),
args=args,
)
def to_str(self, prefix: str = DEFAULT_PREFIX) -> str:
return f'{prefix}{self.name} {self.raw_args}'
@classmethod
def dummy(cls) -> 'Command':
return Command(
name='привет',
raw_args='',
args=[],
)