question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Amending stdlib ArgumentParser

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
simplynailcommented, Jan 6, 2020

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

0reactions
iamkrootcommented, Jan 6, 2020

@simplynail This isn’t exactly what you want, but assuming you can edit the cli_module source, you could convert that ArgumentParser into a GooeyParser (and specify the extra gooey_options), and then just pass an --ignore-gooey as an (implicit) CLI arg by modifying sys.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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

argparse update choices of an argument - Stack Overflow
The argument itself has a choices attribute, but it's easiest if you save a reference to the argument instead of trying to retrieve...
Read more >
argparse — Parser for command-line options, arguments and ...
In a script, parse_args() will typically be called with no arguments, and the ArgumentParser will automatically determine the command-line arguments from sys.
Read more >
argparse does not accept options taking arguments beginning ...
Apparently argparse uses a heuristic to try to guess whether an argument looks like an argument or an option, going so far as...
Read more >
[Example code]-argparse action or type for comma-separated list
I want to create a command line flag that can be used as ./prog.py --myarg=abcd,e,fg. and inside the parser have this be turned...
Read more >
Module isort.main
ArgumentParser ( description="Sort Python import definitions ... help="Causes imports to be sorted based on their sections like STDLIB, THIRDPARTY, etc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found