pygame-web / pygbag

python and pygame wasm for everyone ( packager + test server + simulator )
https://github.com/pygame-web
MIT License
339 stars 40 forks source link

User doesn't pass arguments to command #96

Open ghost opened 1 year ago

ghost commented 1 year ago

Machine:

macOS 12.6.5 Python 3.11.3 Pip 22.0.x (I'm not sure what the "x" is...)

Problem:

When calling the pygbag command in a terminal without any arguments, you get the following error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/bin/pygbag", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pygbag/app.py", line 479, in main
    asyncio.run(main_run(app_folder, mainscript))
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pygbag/app.py", line 174, in main_run
    parser = argparse.ArgumentParser()
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/argparse.py", line 1760, in __init__
    prog = _os.path.basename(_sys.argv[0])
                             ~~~~~~~~~^^^

This is because the code tries to find an argument (with indices) when none are given, thus an IndexError is raised.

Solution (suggestion):

Instead of throwing an ugly, long error, just give the user the "help" message. I've seen this done in other CL tools. When I ran the command pygbag, I expected a help message. Logically, this makes sense as if the user is new to the tool, and runs the comand without any flags or arguments, maybe they are confused, or don't know the tool works. Thus, a help message would, well, be helpful.

pmp-p commented 1 year ago

"pygbag" is a shortcut generated by pip installer, the canonical way to run is python3 -m pygbag and that one seems to send correct error message, at least on linux. I never really had time to test on win/mac, so feel free to implement, test and PR. Patches and documentation are very welcome especially for win/mac.

ghost commented 1 year ago

@pmp-p - Hello Paul,

I'll first do a bit of research and learning on CLI tools and how pygbag works before jumping into a PR. I have absolutely no experience with Python CLI tools and how pip really works 😅.

Based on what you've told me, I assume this is a problem with pygbag and not pip generating the shortcut incorrectly, correct?

pmp-p commented 1 year ago

i think pygbag is lacking a detection mechanism when len(sys.argv) is == 0 That situation may happen when calling it via pip entry point ( not really a shortcut, it is a tiny python program generated by pip in the path).

ghost commented 1 year ago

@pmp-p - Thanks, that helps a ton! 😄

ghost commented 1 year ago

Might make a PR...

Looks like this can be solved with some code in line 17 in ./pygbag/__main__.py. Maybe this would work:

if len(sys.argv) == 0:
    print("...")
    raise SystemExit(0)

@pmp-p - I'm not sure how you're printing the help or support message. Is it just a massive string or something else...?

pmp-p commented 1 year ago

Is it just a massive string or something else

it's a list of all what precedently failed, when that list is non-zero the program halts and report

Rioran commented 7 months ago

If I may, the way to call documentation is also slightly frustrating. I wasn't expecting --help to require folder as an argument.

pmp-p commented 7 months ago

--help is relative to current project target, it may change in the future based on projects templates.

pygbag roots in posix, usage and help are different things.

usage is not currently implemented on entry point script feel free to PR.

python -m pygbag -> usage is quite ok python -m pygbag --help project/main.py -> help for project with specific paths. pygbag -> usage is indeed missing.