python-fire
python-fire copied to clipboard
Is it possible to return `argparser` for customization?
fire can complete ~90% of the CLI needs for my application. However, the help documentation is only available during runtime in my case. Is there some way of getting access to the argparse parser from Fire and modifying it as a user? It looks like the overload-able information is available up to this point:
https://github.com/google/python-fire/blob/ed44d8b801fc24e40729abef11b2dcbf6588d361/fire/core.py#L129
After which it disappears into the void. I would love to be able to modify this parser by adding runtime information. Bonus points if after doing so, I can give it back to fire to call the component trace, etc. 🙂
This isn't currently possible (nor is it on the roadmap).
Can you say a bit more about your use-case?
Sure, I wanted a custom HelpAction for my application:
usage: S3A [--loadLastState LOADLASTSTATE] [--version] [--help] [--appsettings {"App Settings","Default"}]
[--colorscheme {"Color Scheme","dark","dark2","Default"}] [--toolshortcuts {"Default","Tool Shortcuts"}]
[--quickloader {"Default","Quick Loader","test"}]
[--multi-predictionsprocessor {"Default","grid","linknet","Multi-Predictions Processor","refine","template","test"}]
[--verticesprocessor {"basic","Default","test","Vertices Processor"}]
[--componenttablefilter {"blank","Component Table Filter","Default","underwriter"}]
[--fpicfeaturestools {"Default","FPIC Features Tools"}] [--project PROJECT] [--image IMAGE]
[--autosave AUTOSAVE] [--layout LAYOUT]
optional arguments:
--loadLastState LOADLASTSTATE
When the app is closed, all settings are saved. If this is *True*, these settings are restored
on startup. If *False*, they aren't.
--version show program's version number and exit
--help
--appsettings {"App Settings","Default"}
--colorscheme {"Color Scheme","dark","dark2","Default"}
--toolshortcuts {"Default","Tool Shortcuts"}
--quickloader {"Default","Quick Loader","test"}
--multi-predictionsprocessor {"Default","grid","linknet","Multi-Predictions Processor","refine","template","test"}
--verticesprocessor {"basic","Default","test","Vertices Processor"}
--componenttablefilter {"blank","Component Table Filter","Default","underwriter"}
--fpicfeaturestools {"Default","FPIC Features Tools"}
--project PROJECT
--image IMAGE
--autosave AUTOSAVE
--layout LAYOUT
All option limits come from files. So i.e. the user created "Color Scheme", "dark", "dark2", and "Default" files for --colorscheme options.
when python -m s3a --help is called, I want to populate the available option limits, which is trivial if I have access to the parser object:
# Custom help action simply loops through each directory with loadable files
parser.register("action", "help", S3AHelp)
Otherwise, it is difficult to expose this information to Fire for printing during help.
(I also ended up using this to trivially append a --version flag to my parser: parser.add_argument("--version", action="version", version=__version__). Otherwise, it would have to be a function keyword to show up through Fire if I'm not mistaken)