Skip to content

Commit

Permalink
command: add.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Sep 6, 2024
1 parent 2cbed9d commit 5623460
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
12 changes: 5 additions & 7 deletions niar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ def cli(np: Project):
subparsers = parser.add_subparsers(required=True)

build.add_arguments(
np,
subparsers.add_parser(
"build", help="build the design, and optionally program it"
),
)
np, subparsers.add_parser("build", help="build the design, and optionally program it"))
if np.cxxrtl_targets:
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.func(args)
13 changes: 13 additions & 0 deletions niar/command.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions niar/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from amaranth import Elaboratable
from amaranth.build import Platform

from .command import Command
from .cxxrtl_platform import CxxrtlPlatform

__all__ = ["Project"]
Expand Down Expand Up @@ -84,6 +85,7 @@ class Project:
targets: list[type[Platform]]
cxxrtl_targets: list[type[CxxrtlPlatform]] = []
externals: list[str] = []
commands: list[Command] = []

origin: Path

Expand Down Expand Up @@ -118,6 +120,12 @@ class Project:
required=False,
isinstance_list=str,
),
Prop(
"commands",
description="a list of Command objects which extend the CLI",
required=False,
isinstance_list=Command,
),
]

def __init_subclass__(cls):
Expand Down Expand Up @@ -164,6 +172,15 @@ def main(self):
def path(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:
def __init__(self, np: Project):
Expand Down

0 comments on commit 5623460

Please sign in to comment.