Skip to content

Commit

Permalink
Merge pull request #20 from Yoctol/enhance-id-kwargs
Browse files Browse the repository at this point in the history
Enhance id kwargs
  • Loading branch information
noobOriented authored Mar 17, 2020
2 parents e6f8c5a + 5e5d20e commit e24a2aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions examples/id_kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
)

Expand Down
2 changes: 1 addition & 1 deletion yoctol_argparse/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__title__ = 'yoctol-argparse'
__version__ = '0.4.0'
__version__ = '0.5.0'
__description__ = 'Argument Parser created by Yoctol'
__author__ = 'noobOriented'
30 changes: 25 additions & 5 deletions yoctol_argparse/actions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import argparse
import ast
from collections import namedtuple

from .formatters import format_choices

Expand Down Expand Up @@ -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,
Expand All @@ -61,6 +76,7 @@ def __init__(
use_bool_abbreviation=True,
default_as_string=True,
sub_action='store',
metavar=None,
**kwargs,
):
super().__init__(nargs='+', **kwargs)
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e24a2aa

Please sign in to comment.