Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Argparse actions (with a wrapper) as multiconfparse actions? #88

Open
jonathanhaigh opened this issue Jun 19, 2020 · 3 comments
Open

Comments

@jonathanhaigh
Copy link
Owner

No description provided.

@jonathanhaigh
Copy link
Owner Author

Step 1: make Multiconfparse actions more like Argparse actions by:

  • renaming accumulate_processed_value to __call__;
  • passing Namespace objects to __call__ rather than just the current value for the config item.

jonathanhaigh added a commit that referenced this issue Jun 19, 2020
For #88

This is to make implementing actions more familiar to argparse users and
to, perhaps, make it possible to convert existing argparse actions to
multiconfparse actions.

* Rename `Action.accumulate_processed_value` to `Action.__call__`.

* Pass `Action.__call__` a `Namespace` object rather than just the
  current value for the config item.

* Instead of populating a `Namespace` attribute with `NONE` to represent
  the lack of a default value, just don't set the attribute at all.
jonathanhaigh added a commit that referenced this issue Jun 19, 2020
For #88

This is to make implementing actions more familiar to argparse users and
to, perhaps, make it possible to convert existing argparse actions to
multiconfparse actions.

* Rename `Action.accumulate_processed_value` to `Action.__call__`.

* Pass `Action.__call__` a `Namespace` object rather than just the
  current value for the config item.

* Instead of populating a `Namespace` attribute with `NONE` to represent
  the lack of a default value, just don't set the attribute at all.
jonathanhaigh added a commit that referenced this issue Jun 19, 2020
For #88

This is to make implementing actions more familiar to argparse users and
to, perhaps, make it possible to convert existing argparse actions to
multiconfparse actions.

* Rename `Action.accumulate_processed_value` to `Action.__call__`.

* Pass `Action.__call__` a `Namespace` object rather than just the
  current value for the config item.

* Instead of populating a `Namespace` attribute with `NONE` to represent
  the lack of a default value, just don't set the attribute at all.
jonathanhaigh added a commit that referenced this issue Jun 19, 2020
For #88

This is to make implementing actions more familiar to argparse users and
to, perhaps, make it possible to convert existing argparse actions to
multiconfparse actions.

* Rename `Action.accumulate_processed_value` to `Action.__call__`.

* Pass `Action.__call__` a `Namespace` object rather than just the
  current value for the config item.

* Instead of populating a `Namespace` attribute with `NONE` to represent
  the lack of a default value, just don't set the attribute at all.
@jonathanhaigh
Copy link
Owner Author

Let's see what values look like when they get to an Argparse Action class:

>>> import argparse as ap
>>> class A(ap.Action):
...     def __init__(self, option_strings, dest, **kwargs):
...              super().__init__(option_strings, dest, **kwargs)
...     def __call__(self, parser, namespace, values, option_string):
...             print(f"__call__(parser, namespace, {values}, {option_string})")
... 
>>> p = ap.ArgumentParser()
>>> p.add_argument("--c0", action=A, nargs=0)
A(option_strings=['--c0'], dest='c0', nargs=0, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.add_argument("--cn", action=A)
A(option_strings=['--cn'], dest='cn', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.add_argument("--cq", action=A, nargs="?")
A(option_strings=['--cq'], dest='cq', nargs='?', const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.add_argument("--c1", action=A, nargs=1)
A(option_strings=['--c1'], dest='c1', nargs=1, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.add_argument("--c2", action=A, nargs=2)
A(option_strings=['--c2'], dest='c2', nargs=2, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.add_argument("--cp", action=A, nargs="+")
A(option_strings=['--cp'], dest='cp', nargs='+', const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.add_argument("--cs", action=A, nargs="*")
A(option_strings=['--cs'], dest='cs', nargs='*', const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.parse_args("--c0 --cn cnv --cq --cq cqv --c1 c1v --c2 c2v1 c2v2 --cp cpv1 --cp cpv1 cpv2 --cs --cs csv1 csv2".split())
__call__(parser, namespace, [], --c0)
__call__(parser, namespace, cnv, --cn)
__call__(parser, namespace, None, --cq)
__call__(parser, namespace, cqv, --cq)
__call__(parser, namespace, ['c1v'], --c1)
__call__(parser, namespace, ['c2v1', 'c2v2'], --c2)
__call__(parser, namespace, ['cpv1'], --cp)
__call__(parser, namespace, ['cpv1', 'cpv2'], --cp)
__call__(parser, namespace, [], --cs)
__call__(parser, namespace, ['csv1', 'csv2'], --cs)
Namespace(c0=None, c1=None, c2=None, cn=None, cp=None, cq=None, cs=None)
>>>

It's a bit strange that in the nargs == 0 case, [] is the value rather than None like in the nargs == "?" case when no argument is provided, but I guess it's just following the other cases where nargs is an integer.

@jonathanhaigh
Copy link
Owner Author

jonathanhaigh commented Jun 20, 2020

Using [] for nargs == 0 values does mean that something like multiconfparse's MENTIONED_WITHOUT_VALUE isn't necessary for nargs == 0, but it would still be required for the nargs == "+" case without a value if we want to distinguish that case from the case where the value is None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant