1
0
Fork 0
mirror of https://git.sr.ht/~kivikakk/niar synced 2024-12-22 23:42:23 +00:00

command: add.

This commit is contained in:
Asherah Connor 2024-09-06 19:14:26 +03:00
parent 2cbed9def8
commit 56234604d0
3 changed files with 35 additions and 7 deletions

View file

@ -12,15 +12,13 @@ def cli(np: Project):
subparsers = parser.add_subparsers(required=True) subparsers = parser.add_subparsers(required=True)
build.add_arguments( build.add_arguments(
np, np, subparsers.add_parser("build", help="build the design, and optionally program it"))
subparsers.add_parser(
"build", help="build the design, and optionally program it"
),
)
if np.cxxrtl_targets: if np.cxxrtl_targets:
cxxrtl.add_arguments( cxxrtl.add_arguments(
np, subparsers.add_parser("cxxrtl", help="run the C++ simulator tests") np, subparsers.add_parser("cxxrtl", help="run the C++ simulator tests"))
)
for command in np.commands:
command.add_arguments(np, subparsers.add_parser(command.name, help=command.help))
args = parser.parse_args() args = parser.parse_args()
args.func(args) args.func(args)

13
niar/command.py Normal file
View file

@ -0,0 +1,13 @@
from typing import Callable
__all__ = ["Command"]
class Command:
add_arguments: Callable
name: str
help: str
def __init__(self, *, add_arguments, help):
self.add_arguments = add_arguments
self.name = add_arguments.__name__
self.help = help

View file

@ -6,6 +6,7 @@ from typing import Optional
from amaranth import Elaboratable from amaranth import Elaboratable
from amaranth.build import Platform from amaranth.build import Platform
from .command import Command
from .cxxrtl_platform import CxxrtlPlatform from .cxxrtl_platform import CxxrtlPlatform
__all__ = ["Project"] __all__ = ["Project"]
@ -84,6 +85,7 @@ class Project:
targets: list[type[Platform]] targets: list[type[Platform]]
cxxrtl_targets: list[type[CxxrtlPlatform]] = [] cxxrtl_targets: list[type[CxxrtlPlatform]] = []
externals: list[str] = [] externals: list[str] = []
commands: list[Command] = []
origin: Path origin: Path
@ -118,6 +120,12 @@ class Project:
required=False, required=False,
isinstance_list=str, isinstance_list=str,
), ),
Prop(
"commands",
description="a list of Command objects which extend the CLI",
required=False,
isinstance_list=Command,
),
] ]
def __init_subclass__(cls): def __init_subclass__(cls):
@ -164,6 +172,15 @@ class Project:
def path(self): def path(self):
return ProjectPath(self) return ProjectPath(self)
@classmethod
def command(cls, *, help):
def inner(add_arguments):
cls.commands.append(Command(
add_arguments=add_arguments,
help=help,
))
return inner
class ProjectPath: class ProjectPath:
def __init__(self, np: Project): def __init__(self, np: Project):