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
How can I use default arguments in a method with python-fire? Possible bug. Feature request: Argument in fire.Fire
to force using keyword args
#207
Comments
Thanks for the questions.
Try instead:
By inserting a separator, you are telling Fire that you are done passing arguments to methodb.
I'll come back and look at this question at a later time. |
Yes, I agree with what you say about using the separator, but my confusion is in two cases:
Thanks for looking into this when you have a moment! |
@dbieber bump |
When you do If you want to force use of keyword arguments rather than allowing the argument to methodb to be passed positionally, you can try:
I haven't checked if this works and it's not guaranteed to be supported long term though. |
Thanks @dbieber , i will give this a shot. In your example, you are passing the method to the decorator, but i will try passing my class to it (there are too many methods). Hopefully it threats the defaults argument values correctly. Maybe a feature request will be to have an optional argument in btw, would this patch be applied before or after calling |
before (It is during the call to fire.Fire that your users command is executed.)
thanks for the request, I agree that configuring Fire should be a better experience |
fire.Fire
to force using keyword args
@dbieber thanks!, I just tried your solution, and unfortunately, that didnt work. edit:Didnt work for the class, but works for the individual method. for method in dir(SomeClass):
if not method.startswith('_'):
fire.decorators._SetMetadata(getattr(SomeClass, method), fire.decorators.ACCEPTS_POSITIONAL_ARGS, False) This works, but it seems super ugly... |
@dbieber this question is not related to this post, but I couldnt think of a better person to ask this. I know if how I can access and override the command being passed to the Fire instance. So for instance: while True:
commands = 'my --commands being --passed to the class'
fire_obj = fire.Fire(MyClass, command=commands) So now I can access an instance of MyClass using
I wrote a short post about how to combine using fire and prompt_toolkit for awesomeness. Thanks! |
Re the feature request: IIUC, you can just do this in Python 3: def methodb(self, *, x='B123'): This seems to work, though |
@bmccutchon Thanks for sharing that 👍. Yes, using that approach will make x a keyword only argument which will mean it can only be passed as a flag, not positionally. So methoda will not be consumed as a positional argument to x. @securisec I'm not sure I understand what you're trying to do in the loop. It sounds like you can just pass a different value to commands in each iteration of the loop, however, I don't know why you want to call Fire repeatedly in a loop to begin with. But if you want to continue this discussion, let's do it in a brand new issue so we keep each issue to a single topic. (And cool post :), thanks for sharing.) |
Thanks! although @bmccutchon is very elegant, and it works, it has two issues.
At the moment, the response on #207 (comment) seems to be a much more effective solution. Ideally, a force keywords would be a much better solution. @dbieber I figured out the solution to my other question. I will create a new issue with what I cant figure out. |
I am having an issue with
python-fire
when a method has arguments with default values. Consider the following code:As seen in the comments, in the
print(s.b), it is printing
B123` correctly. But when i change the order of the methods being called in fire from the command line, I am getting the odd behavior.Example:
As you can see with the last example, if i call
methodb
(which has an argument with a default value), it printsmethoda
instead ofB123
as expected.My question is, How can I use a method that has a default argument value first in this type of scenario without passing in
--x=something
?In short, how can i make
> python x.py methodb methoda
properly printB123
?Second question is, how can I show the method args in the help? Currently it shows nothing when there is a default value assigned. When I do
methoda methodb
, I do not have to pass--x=B123
, but if I move the method with the default argument value up, it doesnt work as seen in the example.The text was updated successfully, but these errors were encountered: