Skip to content

Commit

Permalink
Argparse options (#56)
Browse files Browse the repository at this point in the history
* help handling
* added parser opts and help
* remove object inheritance for base classes
* bump version

Co-authored-by: Felix Soubelet <[email protected]>
  • Loading branch information
JoschD and fsoubelet authored Sep 19, 2022
1 parent 961e7a4 commit 600564b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion generic_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__title__ = "generic_parser"
__description__ = "A parser for arguments and config-files that also allows direct python input."
__url__ = "https://github.com/pylhc/generic_parser"
__version__ = "1.0.9"
__version__ = "1.1.0"
__author__ = "pylhc"
__author_email__ = "[email protected]"
__license__ = "MIT"
4 changes: 2 additions & 2 deletions generic_parser/dict_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Parser #######################################################################


class DictParser(object):
class DictParser:
"""
Provides functions to parse a dictionary.
Expand Down Expand Up @@ -393,7 +393,7 @@ class ArgumentError(Exception):
pass


class Parameter(object):
class Parameter:
"""Helper Class for DictParser."""
def __init__(self, name, **kwargs):
self.name = name
Expand Down
41 changes: 34 additions & 7 deletions generic_parser/entrypoint_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,16 @@ def entry_function(opt):
import copy
import json
import argparse
import sys
from argparse import ArgumentParser
from configparser import ConfigParser
from inspect import getfullargspec
from functools import wraps
from pathlib import Path
from textwrap import wrap
from typing import Callable, Mapping

from generic_parser.tools import DotDict, silence, unformatted_console_logging
from generic_parser.tools import DotDict, silence, unformatted_console_logging, StringIO, log_out
from generic_parser.dict_parser import ParameterError, ArgumentError, DictParser

import logging
Expand All @@ -186,8 +188,8 @@ def entry_function(opt):
# EntryPoint Class #############################################################


class EntryPoint(object):
def __init__(self, parameter, strict=False):
class EntryPoint:
def __init__(self, parameter, strict: bool = False, argument_parser_args: Mapping = None, help_printer: Callable = None):
"""Initialize decoration: Handle the desired input parameter."""
self.strict = strict

Expand All @@ -203,8 +205,9 @@ def __init__(self, parameter, strict=False):
# this also ensures that the parameters are correctly defined,
# by tests in argparser and in Parameter(),
# which is used in dict_parser -> add parameter
self.argparse = self._create_argument_parser()
self.argparse = self._create_argument_parser(argument_parser_args)
self.dictparse = self._create_dict_parser() # also used for configfiles
self._help_printer = help_printer

def parse(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -252,9 +255,14 @@ def _create_config_argument(self):
parser.add_argument('--{}'.format(ID_SECTION), type=str, dest=ID_SECTION,)
return parser

def _create_argument_parser(self):
def _create_argument_parser(self, args_dict: Mapping):
"""Creates the ArgumentParser from parameter."""
parser = ArgumentParser()

if args_dict:
parser = ArgumentParser(**args_dict)
else:
parser = ArgumentParser()

parser = _add_params_to_argument_parser(parser, self.parameter)
return parser

Expand All @@ -281,7 +289,26 @@ def _handle_commandline(self, args=None):
options = self.configarg.parse_args(args)
except SystemExit:
# parse regular options
options, unknown_opts = self.argparse.parse_known_args(args)
errors_io = StringIO()
try:
with log_out(stderr=errors_io): # errors go into errors_io
options, unknown_opts = self.argparse.parse_known_args(args)
except SystemExit as e:
errors_str = errors_io.getvalue()
# print help on wrong input
if self._help_printer and e.code == 2: # code 0 means help has been printed
self._help_printer(self.argparse.format_help())
# remove duplicated "usage" line
errors_str = "\n".join(errors_str.split("\n")[-2:])

# print errors now (if any)
sys.stderr.write(errors_str)
raise e

# print help, even if passed on to other parser
if self._help_printer and "--help" in unknown_opts:
self._help_printer(self.argparse.format_help())

options = DotDict(vars(options))
if self.strict:
if unknown_opts:
Expand Down

0 comments on commit 600564b

Please sign in to comment.