google / python-fire

Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
Other
27.06k stars 1.44k forks source link

Is it possible to return `argparser` for customization? #346

Open ntjess opened 3 years ago

ntjess commented 3 years ago

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. 🙂

dbieber commented 2 years ago

This isn't currently possible (nor is it on the roadmap).

Can you say a bit more about your use-case?

ntjess commented 2 years ago

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)

twardoch commented 4 months ago

I would love to be able to "capture" the argparse instance— then I could pass it to Gooey ( like I do in https://github.com/twardoch/ezgooey ) and have a minimal-effort GUI in addition to CLI.

I love the power & simplicity of 🔥!