"""
This module contains the functions
(:func:`ourobori.apps.options.build_arguments`) to create the
basic-service-arguments and helper-functions to be used for your own
argument-definitions (:func:`ourobori.apps.options.add_param` and
:func:`ourobori.apps.options.add_flag`).
These functions are used in your ``app.py`` to build the options to be passed
to the :func:`ourobori.apps.http.run_service`.
"""
from argparse import ArgumentParser
from argparse import Namespace
from typing import List
from typing import Optional
from typing import NoReturn
from typing import Union
[docs]def add_flag(*,
parser: ArgumentParser,
flag: str,
help_str: Optional[str] = None) -> NoReturn:
"""
Function to be used to add additional flag to an
``ArgumentParser``-instance.
Note:
This function modifies the passed ``parser``.
Parameters:
parser: The ArgumentParser-instance to add the flag
flag: The name for the flag
help_str: Optional adding the help-string for the param
"""
parser.add_argument(f'--{flag}', help=help_str, action='store_true')
[docs]def add_param(*,
parser: ArgumentParser,
short: str,
name: str,
param_type: Union[str, int, float],
default: Optional[Union[str, int, float]] = None,
choices: Optional[List[Union[str, int, float]]] = None,
help_str: Optional[str] = None) -> NoReturn:
"""
Function to be used to add additional argument to an
``ArgumentParser``-instance.
Note:
This function modifies the passed ``parser``.
Parameters:
parser: The ArgumentParser to add the param
short: Short name for the param
name: Full name for the param
param_type: The type of the param
default: Optional setting the default-value to use for this param
choices: Optional setting the choices for the possible values of the
param
help_str: Optional adding the help-string for the param
"""
args = dict(type=param_type,
help=help_str)
if default:
args['default'] = default
if choices:
args['choices'] = choices
parser.add_argument(f'-{short}', f'--{name}', **args)
[docs]def build_arguments(parser: Optional[ArgumentParser] = None) -> Namespace:
"""
Builds the default arguments for a service.
To use additional arguments create a ArgumentParser add your arguments
using the functions :func:`add_param` or :func:`add_flag` and pass the
resulting parser-object to this function.
Parameters:
parser: Optional passed parser containing additional arguments
Returns:
args: The parsed arguments parsed as Namespace-object
"""
if not parser:
parser = ArgumentParser()
add_param(parser=parser,
short='m',
name='mode',
param_type=str,
choices=['devl', 'staging', 'prod'],
default='devl')
add_param(parser=parser, short='p', name='port', param_type=int)
add_param(parser=parser, short='l', name='logfile', param_type=str)
add_param(parser=parser,
short='lo',
name='logstash',
param_type=str)
add_param(parser=parser,
short='ho',
name='hostname',
param_type=str)
add_flag(parser=parser, flag='apidoc')
add_flag(parser=parser, flag='client_apidoc')
add_flag(parser=parser, flag='debugmode')
args = parser.parse_args()
return args
__all__ = ['add_flag',
'add_param',
'build_arguments']