Amending stdlib ArgumentParser
See original GitHub issueHi all, Is there a possibility to amend arguments configuration previously set to standard lib ArgumentParser with functionality avaialble in GooeyParser, to keep code DRY?
ie I have previosly created command line app using Argument Parser. For argument named “directory” it used argparse.FileType(‘w’). Now I want to hook up this script to Gooey, but I want “directory” arg to use neat widget=‘FileChooser’ from Gooey, but don’t want to rewrite all cli vesion just because this one thing.
Looking at GooeyParser class I tried achieving the above by amending gooey_parser.parser with cli_parser instance created previously in cli_main (as this attribute is actually ArgumentParser() instance looking at the code - but this didn’t work. Any ideas?
cli_module.py:
import argparse
cli_parser = argparse.ArgumentParser()
cli_parser.add_argument('directory', help='path to search files in', type=argparse.FileType('w'))
cli_parser.add_argument('--subdirectories', help='search inside subdirectories', action='store_true')
gui_module.py:
import gooey
import cli_module
@gooey.Gooey()
def main():
gooey_parser = gooey.GooeyParser()
gooey_parser.parser = cli_module.cli_parser
gooey_parser.add_argument('directory', help="folder to search in", widget='DirChooser')
args = gooey_parser.parse_args()
if __name__ == "__main__":
sys.exit(main())
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
all right, thanks for help @iamkroot , looks like I have actually approached the problem from the other way around and haven’t stumbled upon
--ignore-gooey
which should do the trick. Thanks for sharing your monkey patching snippet, will try it out if--ignore-gooey
won’t be sufficient@simplynail This isn’t exactly what you want, but assuming you can edit the
cli_module
source, you could convert thatArgumentParser
into aGooeyParser
(and specify the extra gooey_options), and then just pass an--ignore-gooey
as an (implicit) CLI arg by modifyingsys.argv
when you want to run it as CLI.To go one step further, and not have a dependency on Gooey for the
cli_module
, you can monkey patch the stdlib parser to ignore the gooey_options (I did something like that here)