The Wayback Machine - https://web.archive.org/web/20201020072037/https://github.com/google/python-fire/issues/291
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

Allegro trains integration. #291

Open
ramonemiliani93 opened this issue Oct 15, 2020 · 3 comments
Open

Allegro trains integration. #291

ramonemiliani93 opened this issue Oct 15, 2020 · 3 comments

Comments

@ramonemiliani93
Copy link

@ramonemiliani93 ramonemiliani93 commented Oct 15, 2020

Hi, I am currently trying to integrate python-fire and trains. Trains, at the moment, is capable of automatically detecting the arguments when using argparse. I tried with fire but unfortunately the same does not happen. Could you point me to where the argument parsing happens in fire?

@dbieber
Copy link
Member

@dbieber dbieber commented Oct 15, 2020

The arguments are initially parsed here:

args, flag_args = parser.SeparateFlagArgs(args)

You can see how they're used in the _Fire function's large loop, described here:

python-fire/fire/core.py

Lines 366 to 388 in 878b8d8

The steps performed by this method are:
1. Parse any Flag args (the args after the final --)
2. Start with component as the current component.
2a. If the current component is a class, instantiate it using args from args.
2b. If the component is a routine, call it using args from args.
2c. If the component is a sequence, index into it using an arg from
args.
2d. If possible, access a member from the component using an arg from args.
2e. If the component is a callable object, call it using args from args.
2f. Repeat 2a-2e until no args remain.
Note: Only the first applicable rule from 2a-2e is applied in each iteration.
After each iteration of step 2a-2e, the current component is updated to be the
result of the applied rule.
3a. Embed into ipython REPL if interactive mode is selected.
3b. Generate a completion script if that flag is provided.
In step 2, arguments will only ever be consumed up to a separator; a single
step will never consume arguments from both sides of a separator.
The separator defaults to a hyphen (-), and can be overwritten with the
--separator Fire argument.

Let me know if you have further questions.

@bmartinn
Copy link

@bmartinn bmartinn commented Oct 15, 2020

Thanks @dbieber !
It seems the _ParseArgs is where the magic happens, defining the names of the arguments, default values etc. Is this correct?

@dbieber
Copy link
Member

@dbieber dbieber commented Oct 16, 2020

Yes that's right.
You'll also want to take a look at

python-fire/fire/core.py

Lines 670 to 671 in 878b8d8

parse = _MakeParseFn(fn, metadata)
(varargs, kwargs), consumed_args, remaining_args, capacity = parse(args)

^^That's the path for parsing arguments to a function. Fire also supports using command line arguments for e.g. accessing a member of an object too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.