-
Notifications
You must be signed in to change notification settings - Fork 11
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
Support **kwargs #3
Comments
Perhaps this should only be supported if the function signature is overridden in the docstring as per this example. As a side note, I should update |
Everything currently maps to the function signature so it's a bit of work to start adding extra parameters. Can look into this again if someone has a use case for it. |
I just left huge comment here, but here is the part that may be relevant to this enhancement:
In order to attempt casting kwargs to reasonable types, I also use a helper function:
|
|
I think I agree with you on The simplest solution might be to pack up the kwargs into a
|
I've been thinking about |
I think this is a reasonable feature request, essentially requiring the use of parse_known_args and then "manually" parsing the remainder args into The yamlization part seems independent and could be done by annotating I'll leave the implementation to anyone who has a real use for this, though. |
How about something like this? No filtering needed. from argparse import ArgumentParser
def func(**kwargs: float):
return kwargs
parser = ArgumentParser()
parser.add_argument("--kwargs", nargs="*", default=[])
args = parser.parse_args()
def convert_dict(m: list[str]) -> dict[str, float]:
assert len(m) % 2 == 0
return dict(zip(m[::2], map(float, m[1::2])))
kwargs = convert_dict(args.kwargs)
print(kwargs, func(**kwargs)) The command would be entered as # ex: --kwargs foo 1 2 bar 3 4
def func(**kwargs: tuple[int, int]:
...
# this should be generated
def convert_dict(m: list[str]) -> dict[str, tuple[int, int]]:
assert len(m) % 3 == 0
... |
This looks like a different request? What I had in mind was I think your request can be more generally thought of as a "custom parser that takes more than one parameter", i.e. something like
which is not supported now (currently custom parsers all take a single argument) but seems reasonable to have (well, as usual someone needs to write the implementation :-)) -- we could just introspect the signature of the custom parser... I guess supporting variadic custom parsers is also linked to dataclass support (#82 (comment)), as these basically could behave like variadic parsers (except for the tricky question of whether they should introduce flags or be positional...) |
I have a use-case where I am embedding the Airflow CLI (which uses argparse) inside my defopt CLI. I would like to be able to pass the command line through to the Airflow CLI. My command is defined as follows. As you can see, positional arguments are passed through. Is there a way to pass through keyword arguments as well? In effect, I would like to instruct defopt to stop parsing arguments after the sub-command. def airflow(*args: str):
sys.argv.pop(1)
from airflow import __main__
sys.exit(__main__.main()) |
The classical way to do this (if I understand your request correctly) would be to use |
Awesome thank you! In case this is useful to anyone, this is what I ended up using. def airflow(*args: str):
sys.argv.pop(1)
if "--" in sys.argv:
sys.argv.remove("--")
from airflow import __main__
sys.exit(__main__.main()) |
While I can't see a good way of passing arbitrary keyword arguments, users should absolutely be able to pass documented keyword arguments.
defopt.run
is itself an example of a function that accepts**kwargs
for Python 2 compatibility but expects at mostargv
to be specified.The text was updated successfully, but these errors were encountered: