★ wanayoo — archive 1999 https://github.com/python-cmd2/cmd2/issues/515Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help does not work with all argparse wrapped functions #515

Closed
kmvanbrunt opened this issue Sep 12, 2018 · 8 comments
Closed

Help does not work with all argparse wrapped functions #515

kmvanbrunt opened this issue Sep 12, 2018 · 8 comments
Assignees
Labels
bug
Milestone

Comments

@kmvanbrunt
Copy link
Member

@kmvanbrunt kmvanbrunt commented Sep 12, 2018

For an argparse command, the help function adds '-h' to the command to print help text. For most cases, this approach works. However, there are cases in which '-h' won't be available because the ArgumentParser has add_help set to False.

The help function needs to be updated to do something similar to the argparse_completer module. It's able to process a line of text and figure out what parser or subparser is active. When it identifies the parser it can just call parser.print_help().

@kmvanbrunt kmvanbrunt added the bug label Sep 12, 2018
@kmvanbrunt kmvanbrunt added this to the 0.9.5 milestone Sep 12, 2018
@tleonhardt
Copy link
Member

@tleonhardt tleonhardt commented Sep 17, 2018

Since this Issue and #522 are vaguely related, it might be good to work them together.

@anselor
Copy link
Contributor

@anselor anselor commented Sep 23, 2018

I just want to summarize the items from this issue as well as #522 and #523 so everyone is in agreement about what the desired behavior.

  • #515 - For argparse commands, it currently incorrectly assumes all commands accept '-h' as the parameter to print help. If possible, try to generate the help output without making assumptions about the flag.
  • #523 - When help_command() is present, always use that - ignore the argparse description for the command.
  • #522 - If 2 commands share the same argparse object, and the description is not set, try to use the appropriate command's doc-string text. Right now it's just taking the first one.

Does that summarize everything?
@kmvanbrunt @xNinjaKittyx @faraazc @tleonhardt

@tleonhardt
Copy link
Member

@tleonhardt tleonhardt commented Sep 23, 2018

That does a good job of summarizing my understanding for the desired behavior as it relates to the three Issues you mention.

@kmvanbrunt
Copy link
Member Author

@kmvanbrunt kmvanbrunt commented Sep 23, 2018

  • #515 I agree with your description. Just call print_help() on the correct parser, which may be a subparser if a subcommand is being called.

  • #523 Always calling help_command() when dealing with an argparse command is a tricky one. The user would have to understand that the help command will be called for the top level command even if the user is actually running a subcommand. This kind of usage seems to be a misunderstanding of argparse. I would rather encourage users to learn the argparse API. For instance, they could easily override a parser's or subparser's print_help() command instead.

  • #522 I still don't feel right about this one. It opens the door for filling up our argparse decorators with so many corner cases. I honestly believe reusing an argparse object like this is bad code. Either make a deep copy of the parser, which is supported on newer versions of Python, or create the parser using a function as I demonstrated in an earlier post.

@tleonhardt
Copy link
Member

@tleonhardt tleonhardt commented Sep 23, 2018

I’m also OK with Kevin’s interpretation. I just want all features to work correctly as documented. If there are exceptions and/or gotchas, we need to make that abundantly clear in the documentation and examples.

@kmvanbrunt
Copy link
Member Author

@kmvanbrunt kmvanbrunt commented Sep 23, 2018

Let me clarify my comments about #522. Calling it bad code is harsh. I only mean to caution that it can introduce unpredictability. Here is an example based on things I do in my own application.

Let's say I have a function called do_print() which uses a parser called print_parser. There are times when I need information from print_parser like its prog value to print an error message. There are also times when I might call another argparse-based command from do_print(). If I call the other command and it happens to use the same parser object, then that object will be altered when the function returns execution back to do_print(). The parser will now how a prog value matching the other command.

@kotfu
Copy link
Member

@kotfu kotfu commented Sep 23, 2018

  • #515 - agree with proposed fix
  • #522 - I think that we shouldn't write workarounds to allow the same argparser to be used on multiple do_ methods. I agree with @kmvanbrunt that is introduces unpredictability.
  • #523 - No preference as long as it's clearly documented either way
@kmvanbrunt
Copy link
Member Author

@kmvanbrunt kmvanbrunt commented Sep 24, 2018

#523 - This one may cause an issue if a user types command -h
That won't go through our help function, so argparse will print its own usage statement. I suppose we could overwrite print_help() in with_argparser(). But that still wouldn't cover running command subcommand -h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.