#
# Copyright 2019-Present Sonatype Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# encoding: utf-8
import sys
from abc import ABC, abstractmethod
from argparse import ArgumentParser, Namespace
from typing import Optional
if sys.version_info >= (3, 8):
from importlib.metadata import version as meta_version
else:
from importlib_metadata import version as meta_version
jake_version: str = 'TBC'
try:
[docs]
jake_version = str(meta_version('jake')) # type: ignore[no-untyped-call]
except Exception:
jake_version = 'DEVELOPMENT'
[docs]
class BaseCommand(ABC):
def __init__(self) -> None:
super().__init__()
self._arguments: Optional[Namespace] = None
[docs]
def execute(self, arguments: Namespace) -> int:
self._arguments = arguments
return self.handle_args()
@abstractmethod
[docs]
def handle_args(self) -> int:
pass
@abstractmethod
[docs]
def get_argument_parser_name(self) -> str:
pass
@abstractmethod
[docs]
def get_argument_parser_help(self) -> str:
pass
@abstractmethod
[docs]
def setup_argument_parser(self, arg_parser: ArgumentParser) -> None:
pass
@property
[docs]
def arguments(self) -> Namespace:
if self._arguments:
return self._arguments
raise ValueError('Arguments have not been set yet - execute() has to be called first')