From 5c6a88bfeb915adde994c531be4ff2b9ca28e588 Mon Sep 17 00:00:00 2001 From: noobOriented Date: Tue, 17 Mar 2020 12:52:33 +0800 Subject: [PATCH 1/2] enhance IdKwargs output format --- examples/id_kwargs.py | 2 ++ yoctol_argparse/actions.py | 30 +++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/examples/id_kwargs.py b/examples/id_kwargs.py index 5b724d1..28934b9 100644 --- a/examples/id_kwargs.py +++ b/examples/id_kwargs.py @@ -11,6 +11,8 @@ id_choices=['squat', 'bench', 'deadlift'], split_token=',', use_bool_abbreviation=True, + default=IdKwargs.IdKwargsPair('squat', weight=50, reps=10, sets=5, use_belt=True), + metavar='exercise_type', help='(exercise_type, kwargs_string)', ) diff --git a/yoctol_argparse/actions.py b/yoctol_argparse/actions.py index 5f2245d..80cf172 100644 --- a/yoctol_argparse/actions.py +++ b/yoctol_argparse/actions.py @@ -1,6 +1,5 @@ import argparse import ast -from collections import namedtuple from .formatters import format_choices @@ -52,7 +51,23 @@ def __call__(self, parser, namespace, values, option_string=None): class IdKwargs(argparse.Action): - IdKwargsPair = namedtuple('IdKwargsPair', ['id', 'kwargs']) + class IdKwargsPair: + + def __init__(self, id, **kwargs): # noqa: A002 + self.id = id # noqa: A002 + self.kwargs = kwargs + + def __iter__(self): + return iter((self.id, self.kwargs)) + + def __str__(self): + if not self.kwargs: + return self.id + kwarg_string = ",".join(f"{k}={v}" for k, v in self.kwargs.items()) + return f"{self.id} {kwarg_string}" + + def __eq__(self, other): + return (self.id, self.kwargs) == other def __init__( self, @@ -61,6 +76,7 @@ def __init__( use_bool_abbreviation=True, default_as_string=True, sub_action='store', + metavar=None, **kwargs, ): super().__init__(nargs='+', **kwargs) @@ -71,7 +87,11 @@ def __init__( if sub_action not in ('store', 'append'): raise ValueError(f'unknown sub_action {sub_action:r}') self.sub_action = sub_action - self.metavar = (format_choices(id_choices), f'KEY1=VALUE1{split_token}KEY2=VALUE2') + if metavar is None: + metavar = (format_choices(id_choices), f'k1=v1{split_token}k2=v2') + elif isinstance(metavar, str): + metavar = (metavar, f'k1=v1{split_token}k2=v2') + self.metavar = metavar def __call__(self, parser, namespace, values, option_string=None): if len(values) == 2: @@ -104,11 +124,11 @@ def __call__(self, parser, namespace, values, option_string=None): ) if self.sub_action == 'store': - setattr(namespace, self.dest, self.IdKwargsPair(id_, kwargs)) + setattr(namespace, self.dest, self.IdKwargsPair(id_, **kwargs)) elif self.sub_action == 'append': if not getattr(namespace, self.dest, None): setattr(namespace, self.dest, []) - getattr(namespace, self.dest).append(self.IdKwargsPair(id_, kwargs)) + getattr(namespace, self.dest).append(self.IdKwargsPair(id_, **kwargs)) else: raise AssertionError From 5e5d20e4865c5286057ec15c289ebedf70950ed8 Mon Sep 17 00:00:00 2001 From: noobOriented Date: Tue, 17 Mar 2020 13:01:50 +0800 Subject: [PATCH 2/2] version 0.5.0 --- yoctol_argparse/__version__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yoctol_argparse/__version__.py b/yoctol_argparse/__version__.py index 818c1dd..ccaf6cc 100644 --- a/yoctol_argparse/__version__.py +++ b/yoctol_argparse/__version__.py @@ -1,4 +1,4 @@ __title__ = 'yoctol-argparse' -__version__ = '0.4.0' +__version__ = '0.5.0' __description__ = 'Argument Parser created by Yoctol' __author__ = 'noobOriented'