Skip to content

Display help when ran without any arguments#1

Open
millefalcon wants to merge 8 commits intocdunklau:masterfrom
millefalcon:master
Open

Display help when ran without any arguments#1
millefalcon wants to merge 8 commits intocdunklau:masterfrom
millefalcon:master

Conversation

@millefalcon
Copy link
Copy Markdown

If ran without any arguments, it will raise the error:

  gistenv  ~/.../tkinter/cdunklu  ./gistit.py
Traceback (most recent call last):
  File "./gistit.py", line 245, in <module>
    main()
  File "./gistit.py", line 104, in main
    command = args.func
AttributeError: 'Namespace' object has no attribute 'func'
 ✘   gistenv  ~/.../tkinter/cdunklu  

@cdunklau
Copy link
Copy Markdown
Owner

Thanks! Can you see if you can write a test for this?

@millefalcon
Copy link
Copy Markdown
Author

Sure :)

@cdunklau
Copy link
Copy Markdown
Owner

As discussed on IRC, please just make the tests pass on py2, more work is apparently necessary to get them passing on py3, will handle this separately (in #4 ).

Also make a CONTRIBUTORS file with yourself in it, formatted like Joe Schmoe <jschmoe@example.com>

@millefalcon
Copy link
Copy Markdown
Author

millefalcon commented Dec 31, 2019

This seems a bit weird, since it doesn't need any change on python2, while it is raising the particular error mentioned in python3 only [without the fix].

python2 run:

 ✘   gistenv~/.../cdunklu/gistit   master  ../gistenv/bin/python2 gistit.py
usage: gistit.py [-h] [--token TOKEN] {create,token} ...
gistit.py: error: too few arguments

python3 run:

  gistenv~/.../cdunklu/gistit   master ●  ../gistenv/bin/python3 gistit.py
Traceback (most recent call last):
  File "gistit.py", line 245, in <module>
    main()
  File "gistit.py", line 104, in main
    command = args.func
AttributeError: 'Namespace' object has no attribute 'func'

@cdunklau cdunklau self-requested a review December 31, 2019 13:06
Comment thread gistit.py Outdated
Comment thread gistit.py Outdated
@millefalcon
Copy link
Copy Markdown
Author

millefalcon commented Dec 31, 2019 via email

@cdunklau
Copy link
Copy Markdown
Owner

cdunklau commented Jan 1, 2020

I merged PR #5 and fixed the existing tests so they work on py3 as well, so feel free to update your branch with master and adjust as necessary.

@millefalcon
Copy link
Copy Markdown
Author

So i am wondering, how to overcome the different behavior between python2 argparse and python3 argparse change.

One thing i saw is we could set the subparser.required = True like,

   ...
   parser.add_argument(
        '--token', '-t', default=default_token_file,
        help='Path to token file')
    subparsers = parser.add_subparsers(
        dest='command', help='Available commands')

    subparsers.required = True # <--- here

    create_parser = subparsers.add_parser('create', help='Create new gist')
    ...

Then, we don't need to have the fix that i raised the PR for :)

@cdunklau
Copy link
Copy Markdown
Owner

cdunklau commented Jan 2, 2020

One thing i saw is we could set the subparser.required = True

Looks like that was added in 3.7, but I'd like to keep 3.6 support (and 2.7, at least for now).

@millefalcon
Copy link
Copy Markdown
Author

Okay. Then this new fix will fail for the test in 2.7 :|

@cdunklau
Copy link
Copy Markdown
Owner

cdunklau commented Jan 2, 2020

I think comparing the outputs is unnecessary. Instead, just check that [sys.executable, 'gistit.py'] exits with status 1, and that the first line of the output looks like usage: gistit.py [-h] [--token TOKEN] {create,token} ...

@millefalcon
Copy link
Copy Markdown
Author

Okay. I will update with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants