add 'int' command arg spec type
This commit is contained in:
parent
d438b6dbc9
commit
d7cc7781bd
3 changed files with 18 additions and 8 deletions
|
@ -1,8 +1,9 @@
|
|||
import decimal, io, typing
|
||||
from src.utils import datetime, errors
|
||||
|
||||
from .time import duration
|
||||
from .spec import *
|
||||
from .time import duration
|
||||
from .types import try_int
|
||||
|
||||
COMMENT_TYPES = ["#", "//"]
|
||||
def hashflags(filename: str
|
||||
|
@ -78,12 +79,6 @@ def keyvalue(s: str, delimiter: str=" "
|
|||
items[key] = None
|
||||
return items
|
||||
|
||||
def try_int(s: str) -> typing.Optional[int]:
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
def line_normalise(s: str) -> str:
|
||||
lines = list(filter(None, [line.strip() for line in s.split("\n")]))
|
||||
return " ".join(line.replace(" ", " ") for line in lines)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import enum, typing
|
||||
from .time import duration
|
||||
from . import try_int
|
||||
from .types import try_int
|
||||
|
||||
class SpecArgumentContext(enum.IntFlag):
|
||||
CHANNEL = 1
|
||||
|
@ -49,6 +49,12 @@ class SpecArgumentTypeTrimString(SpecArgumentTypeString):
|
|||
def simple(self, args: typing.List[str]):
|
||||
return SpecArgumentTypeString.simple(self, list(filter(None, args)))
|
||||
|
||||
class SpecArgumentTypeInt(SpecArgumentType):
|
||||
def simple(self, args):
|
||||
if args:
|
||||
return try_int(args[0]), 1
|
||||
return None, 1
|
||||
|
||||
class SpecArgumentTypeDuration(SpecArgumentType):
|
||||
def name(self):
|
||||
return "+%s" % (SpecArgumentType.name(self) or "duration")
|
||||
|
@ -68,6 +74,7 @@ SPEC_ARGUMENT_TYPES = {
|
|||
"wordlower": SpecArgumentTypeWordLower,
|
||||
"string": SpecArgumentTypeString,
|
||||
"tstring": SpecArgumentTypeTrimString,
|
||||
"int": SpecArgumentTypeInt,
|
||||
"duration": SpecArgumentTypeDuration
|
||||
}
|
||||
|
||||
|
|
8
src/utils/parse/types.py
Normal file
8
src/utils/parse/types.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
import typing
|
||||
|
||||
def try_int(s: str) -> typing.Optional[int]:
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
return None
|
||||
|
Loading…
Reference in a new issue