Inital commit with basic commands.
This commit is contained in:
83
src/bsv/main.py
Normal file
83
src/bsv/main.py
Normal file
@@ -0,0 +1,83 @@
|
||||
# bsv - Backup, Synchronization, Versioning
|
||||
# Copyright (C) 2023 Simon Boyé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import os.path
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from textwrap import dedent
|
||||
|
||||
from bsv import __version__
|
||||
from bsv.command import init_commands
|
||||
|
||||
|
||||
def make_parser(
|
||||
prog: str = "",
|
||||
exit_on_error: bool = True,
|
||||
) -> ArgumentParser:
|
||||
parent_parser = ArgumentParser(add_help=False)
|
||||
parent_parser.add_argument(
|
||||
"--repository",
|
||||
type = Path,
|
||||
help = dedent("""
|
||||
Bsv repository path. Overides default paths and BSV_REPOSITORY environment variable.
|
||||
""").strip(),
|
||||
)
|
||||
|
||||
parser = ArgumentParser(
|
||||
prog = prog or os.path.basename(sys.argv[0]),
|
||||
description = dedent("""
|
||||
bsv - Backup, Synchronization, Versioning.
|
||||
""").strip(),
|
||||
parents = [parent_parser],
|
||||
exit_on_error = exit_on_error,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--version",
|
||||
action = "version",
|
||||
version = f"bsv version {__version__}",
|
||||
)
|
||||
|
||||
init_commands(parser, [parent_parser])
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
def main(
|
||||
args: list[str] | None = None,
|
||||
prog: str = "",
|
||||
exit_on_error: bool = True,
|
||||
) -> int:
|
||||
parser = make_parser(
|
||||
prog = prog or os.path.basename(sys.argv[0]),
|
||||
exit_on_error = exit_on_error,
|
||||
)
|
||||
arg_dict = vars(parser.parse_args(args or sys.argv[1:]))
|
||||
|
||||
repository_path: Path | None = arg_dict.pop("repository")
|
||||
if repository_path is None and "BSV_REPOSITORY" in os.environ:
|
||||
repository_path = Path(os.environ["BSV_REPOSITORY"])
|
||||
# else:
|
||||
# for path in get_config_dirs():
|
||||
# maybe_config_path = path / "config.toml"
|
||||
# if maybe_config_path.is_file():
|
||||
# config_path = maybe_config_path
|
||||
# break
|
||||
|
||||
command = arg_dict.pop("command")
|
||||
|
||||
return command(repository_path=repository_path, **arg_dict)
|
||||
Reference in New Issue
Block a user