Skip to content

Commit

Permalink
Simplify the CLI code and make help prettier
Browse files Browse the repository at this point in the history
Fixes #34

Signed-off-by: Fabrice Normandin <[email protected]>
  • Loading branch information
lebrice committed Mar 14, 2023
1 parent a363710 commit a3c9639
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 55 deletions.
66 changes: 41 additions & 25 deletions sarc/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,65 @@
from __future__ import annotations

import logging
from dataclasses import dataclass
from typing import Any, Union
from typing import Protocol

from simple_parsing import ArgumentParser, field, subparsers
from simple_parsing import ArgumentParser

from sarc.cli.acquire import Acquire
from sarc.cli.db import Db
from sarc.cli.acquire import add_acquire_commands
from sarc.cli.db import add_db_commands

logger = logging.getLogger(__name__)


@dataclass
class CLI:
command: Union[Acquire, Db] = subparsers({"acquire": Acquire, "db": Db})
class Command(Protocol):
def execute(self) -> int:
...


def main(argv: list[str] | None = None) -> int:
parser = ArgumentParser()
# parser.add_arguments(GlobalArgs, dest="global_args")

verbose: int = field(
alias=["-v"],
parser.add_argument(
"-v",
"--verbose",
default=0,
help="logging levels of information about the process (-v: INFO. -vv: DEBUG)",
action="count",
help="logging levels of information about the process (-v: INFO. -vv: DEBUG)",
)

def execute(self) -> int:
levels = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}
command_subparsers = parser.add_subparsers(
dest="command_name",
title="Command title",
description="Description",
required=True,
)

logging.basicConfig(
format="%(asctime)-15s::%(levelname)s::%(name)s::%(message)s",
level=levels.get(self.verbose, logging.DEBUG),
)
db_parser = command_subparsers.add_parser("db", help="database-related commands")

# logger.debug("SARC version : %s", sarc.__version__)
add_db_commands(db_parser)

return self.command.execute()
acquire_parser = command_subparsers.add_parser(
"acquire", help="commands used acquire different kinds of data"
)

add_acquire_commands(acquire_parser)

def main(argv: list[Any] | None = None) -> int:
"""Main commandline for SARC"""
parser = ArgumentParser()
parser.add_arguments(CLI, dest="command")
args = parser.parse_args(argv)
command: CLI = args.command

return command.execute()
verbose: int = args.verbose
# NOTE: unused, but available in case it's needed:
# command_name: str = args.command_name
# subcommand_name: str = args.subcommand_name
subcommand: Command = args.subcommand

levels = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}
logging.basicConfig(
format="%(asctime)-15s::%(levelname)s::%(name)s::%(message)s",
level=levels.get(verbose, logging.DEBUG),
)

return subcommand.execute()


if __name__ == "__main__":
Expand Down
30 changes: 16 additions & 14 deletions sarc/cli/acquire/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from dataclasses import dataclass
from typing import Union

from simple_parsing import subparsers
from simple_parsing import ArgumentParser

from sarc.cli.acquire.allocations import AcquireAllocations
from sarc.cli.acquire.jobs import AcquireJobs
from sarc.cli.acquire.storages import AcquireStorages


@dataclass
class Acquire:
command: Union[AcquireAllocations, AcquireJobs, AcquireStorages] = subparsers(
{
"allocations": AcquireAllocations,
"jobs": AcquireJobs,
"storages": AcquireStorages,
}
def add_acquire_commands(parser: ArgumentParser):
subparsers = parser.add_subparsers(
title="subcommand",
description="Acquire subcommand",
dest="subcommand_name",
required=True,
)
allocations_parser = subparsers.add_parser(
"allocations", help="Acquire allocations help"
)
allocations_parser.add_arguments(AcquireAllocations, dest="subcommand")

jobs_parser = subparsers.add_parser("jobs", help="Acquire jobs help")
jobs_parser.add_arguments(AcquireJobs, dest="subcommand")

def execute(self) -> int:
return self.command.execute()
storages_parser = subparsers.add_parser("storages", help="Acquire storages help")
storages_parser.add_arguments(AcquireStorages, dest="subcommand")
2 changes: 2 additions & 0 deletions sarc/cli/acquire/allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def convert_csv_row_to_allocation(

@dataclass
class AcquireAllocations:
"""Command to acquire data about the allocations."""

file: Path

def execute(self) -> int:
Expand Down
3 changes: 3 additions & 0 deletions sarc/cli/acquire/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ def _daterange(

@dataclass
class AcquireJobs:
"""Command used to acquire job data."""

cluster_names: list[str] = field(
alias=["-c"], default_factory=list, choices=clusters
)
"""List of cluster names to acquire job data for."""

dates: list[str] = field(alias=["-d"], default_factory=list)

Expand Down
5 changes: 4 additions & 1 deletion sarc/cli/acquire/storages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Optional

from simple_parsing import field

Expand All @@ -15,7 +16,9 @@

@dataclass
class AcquireStorages:
file: Path = field(default=None)
"""Acquire information about the storage."""

file: Optional[Path] = None
cluster_names: list[str] = field(
alias=["-c"], default_factory=list, choices=clusters
)
Expand Down
24 changes: 9 additions & 15 deletions sarc/cli/db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
from dataclasses import dataclass
from typing import Union

from simple_parsing import subparsers
from simple_parsing import ArgumentParser

from sarc.cli.db.init import DbInit


@dataclass
class Db:
"""this is help"""

command: Union[DbInit] = subparsers(
{
"init": DbInit,
}
def add_db_commands(parser: ArgumentParser):
subparsers = parser.add_subparsers(
title="subcommand",
description="subcommand description",
dest="subcommand_name",
required=True,
)

def execute(self) -> int:
return self.command.execute()
db_init_subparser = subparsers.add_parser("init", help="Initialize the DB")
db_init_subparser.add_arguments(DbInit, dest="subcommand")
2 changes: 2 additions & 0 deletions sarc/cli/db/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@dataclass
class DbInit:
"""Initializes the database."""

url: Optional[str]
database: Optional[str]

Expand Down

0 comments on commit a3c9639

Please sign in to comment.