Add initial CLI support
This commit is contained in:
parent
e16d67e2f8
commit
22a9c68611
14 changed files with 168 additions and 26 deletions
29
trovedb/cli/cli_common.py
Normal file
29
trovedb/cli/cli_common.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""Common code for CLI subcommands"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from trovedb import trove_factory, user_env, trove as tr
|
||||
|
||||
|
||||
def display_error(msg: str) -> None:
|
||||
print(f"Error: {msg}", file=sys.stderr)
|
||||
|
||||
|
||||
class CliEnv:
|
||||
"""Environment for CLI subcommands"""
|
||||
def __init__(self, verbose: bool = False):
|
||||
self.verbose = verbose
|
||||
self.local_trove = trove_factory.get_trove(user_env.TROVEBASE) if user_env.TROVEBASE else None
|
||||
|
||||
def checked_trove(self) -> tr.Trove:
|
||||
if self.local_trove is None:
|
||||
display_error("No local trove found")
|
||||
raise SystemExit()
|
||||
return self.local_trove
|
||||
|
||||
def common_args(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument('-v', '--verbose', action='store_true')
|
||||
|
||||
def get_env(args: argparse.Namespace) -> CliEnv:
|
||||
return CliEnv(args.verbose)
|
||||
Loading…
Add table
Add a link
Reference in a new issue