FHPythonUtils / Cli2Gui

Use this module to convert a cli program to a gui
MIT License
88 stars 6 forks source link

Support for parser.add_subparsers() #10

Closed Evidlo closed 2 years ago

Evidlo commented 2 years ago

When parser.add_subparsers() is used, Cli2Gui fails with the message

[evan@blackbox picklecast] python /tmp/test.py
Traceback (most recent call last):
  File "/tmp/test.py", line 30, in <module>
    gui()
  File "/home/evan/resources/venv3/lib/python3.10/site-packages/cli2gui/decorators.py", line 286, in inner
    return callingFunction(*args, **kwargs)
  File "/tmp/test.py", line 14, in main
    args = parser.parse_args()
  File "/home/evan/resources/venv3/lib/python3.10/site-packages/cli2gui/decorators.py", line 249, in runCli2Gui
    buildSpec = createFromParser(
  File "/home/evan/resources/venv3/lib/python3.10/site-packages/cli2gui/decorators.py", line 85, in createFromParser
    return FullBuildSpec(**convertMap["self"][parser](selfParser), **buildSpec)
  File "/home/evan/resources/venv3/lib/python3.10/site-packages/cli2gui/tojson/argparse2json.py", line 186, in convert
    for _, subparser in iterParsers(parser):
  File "/home/evan/resources/venv3/lib/python3.10/site-packages/cli2gui/tojson/argparse2json.py", line 42, in iterParsers
    ].choices.arg_items()
AttributeError: 'dict' object has no attribute 'arg_items'
#!/usr/bin/env python3
from cli2gui import Cli2Gui
import argparse

def run(args):
    print(args.arg)

def main():
    parser = argparse.ArgumentParser(description="this is an example parser")
    parser.add_argument("arg", type=str, help="positional arg")

    parser.add_subparsers()

    args = parser.parse_args()
    run(args)

decorator_function = Cli2Gui(
    run_function=run,
    auto_enable=True,
)

gui = decorator_function(main)

if __name__ == "__main__":
    gui()

Ideally Cli2Gui would support these subparsers (perhaps as additional tabs in the GUI), but at least they could be ignored and not cause an exception.

FredHappyface commented 2 years ago

Hey, cheers for the issue and apologies for the delayed response

Probably best to opt for the ignore case for now as there's a decent chunk of work there to remind myself how things work and then to investigate subparsers. Not sure if https://github.com/chriskiehl/Gooey has what you need here?

FredHappyface commented 2 years ago

Taken a look and this is the best I can do thus far:

image

Namespace(arg='foo', **{'==SUPPRESS==': '???'})
Evidlo commented 2 years ago

I like Cli2Gui because I can continue to use argparse.

Thanks for the work on this. Why not simply ignore the subparser instead of adding that ==SUPPRESS== argument? That might be confusing to end users.

FredHappyface commented 2 years ago

Argparse adds the ==suppress== arg. Seems to contain info on the subparser. So if you give it a description then it renders in place of none. There's an example under the tests