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
Cmd: list available completions from the cmd.Cmd subclass and filter out EOF handler(s) #57423
Comments
class CLI(cmd.Cmd):
def register_subcommand(self, cmd, cli_class):
def call_cli(self):
cli = cli_class()
cli.cmdloop()
setattr(self, 'do_%s' % (cmd, ), call_cli) it won't register the command in the completion list or help list.
def do_EOF(self, arg):
raise EOFError so what I'm proposing instead is that it be filtered out by default. Given that there's some value in allowing developers to build custom filter rules for the completions (example: certain commands can be undocumented, hidden to lower privilege users, etc), I figured it would be wise to create a custom mechanism for filtering out commands. This could be cleaned up to use better python idioms like a generator, cache the data and have a refresh method, etc, but this is a good first start. |
This looks to be a reasonable request. I think the patch would be better if the filtering were done directly in get_names(). A subclass can override or extend that method if it wants to customize the filter. |
Here's a version incorporating your suggestion and better documenting the choices and the method for overriding purposes. I have a few reservations with the current implementation:
This module could be better cleaned up (isn't PEP-8 compliant, overrides built-ins, is pythonic but not super pythonic, etc), but I'll see what other modules exist out there that could be used in its place, because they could have resolved some of these issues. There is some value that can be obtained from pexpect, some of the other cmd module variants, etc .. I just like this module because it's nice, simple, and standard -- it just needs a little love and it will be awesome. Anyhow -- thanks again for the work :). |
Needed to update the patch slightly for Python 3; now that filter() returns an iterator, |
Change to test_cmd.py to test for help displaying the name of the registered subcommand (as well as a simple test for the basic operation of the registered sub-CLI). |
If you write a handler for EOF like so: from cmd import Cmd
class FooShell(Cmd):
def do_EOF(self, args):
# exit on EOF
raise SystemExit()
shell = FooShell()
shell.cmdloop() Then when running the shell, you can see "EOF" as an undocumented command in the help screen. You can see this when typing "?". $ python fooshell.py
(Cmd) ? Documented commands (type help <topic>): Undocumented commands: I believe the correct behaviour should be (1) don't show it in the undocumented commands, since it's not really a command; and (2) maybe create a built-in command for this, since the literal string "EOF" is also caught by this handler. |
I know that many people do something like: def do_EOF(self, arg):
return True to exit the program when you press Ctrl+d. Others might prefer something like ngie https://bugs.python.org/issue13214#msg145856: def do_EOF(self, arg):
raise EOFError The issue that I have is if the command you enter is 'EOF' (or 'EOF --some --arg'), you end up calling your do_EOF function. I think this unintended side effect could be avoided if the check for EOFError was removed and the exception was just raised. (see https://github.com/python/cpython/blob/master/Lib/cmd.py#L127). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: