cmd.py from the standard library adapted to use prompt-toolkit instead of readline
Project description
ptkcmd
ptkcmd adapts the built-in cmd.py standard library module to use prompt-toolkit instead of readline
from ptkcmd import PtkCmd, Completion, complete_files
class MyCmd(PtkCmd):
"""
stdin, stdout, intro are as defined in the standard library cmd.py
If interactive is True, then the prompt-toolkit prompt()
method will be utilized from a PromptSession.
If interactive is False, then the prompt will be written
to stdout and a line read from stdin
If do_complete_cmd is True, then completion will be performed
for the initial command of each line against the list of known
commands.
If do_complete_cmd is False, no completion will be attempted for
the initial command.
In either case, completion can be attempted for the arguments
according to any 'complete_' methods defined.
If default_shell is False, then receiving a command that does not
have a "do_" method will result in writing an error to
self.stdout.
If default_shell is True, then the command will be used as an
input to subprocess.run(). The shell input to run() will be
False.
If additional keyword arguments are provided, they will be passed
to the PromptSession constructor that is used for prompts.
The only PromptSession keyword argument not allowed is 'completer'.
"""
prompt='MyPtkCmd$ ' #change the prompt
def do_mycmd(self,args)
"""
This is a command named 'mycmd'.
When a command is executed by PtkCmd, the initial line that is
entered is split by shlex.split().
The first item, i.e. the command, is used to determine which
"do_" method to call.
The args input is the list, excluding the command itself.
All commands will show up when the help command is invoked.
Typing 'help mycmd' will show the docstring of do_mycmd().
"""
self.stdout.write('mycmd args were %s\n' % repr(args))
def help_mytopic(self):
"""
This is a help topic named 'mytopic'.
All topics declared in this way will show up when the help
command is invoked.
Typing 'help mytopic' will execute the help function, which
typically will write some text to self.stdout
"""
self.stdout.write('You called help for mytopic\n')
def complete_mycmd(self,prev_args,curr_arg,document,complete_event):
"""
This method defines completion rules for the command named 'mycmd'
prev_args = the arguments prior to the current one being completed
curr_arg = the current argument being completed.
completed
May be an empty string or the start of an argument that
from the list of completable arguments.
document and complete_event are as defined in the prompt-toolkit
documentation
Should yield Completion objects as defined in prompt-toolkit
Completion(text,start_position)
text = a suggested completion
start_position = non-positive number representing
how many characters from the cursor to go back
and overwrite.
"""
yield from complete_files(curr_arg)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ptkcmd-0.1.2.tar.gz
(5.9 kB
view details)
Built Distribution
File details
Details for the file ptkcmd-0.1.2.tar.gz
.
File metadata
- Download URL: ptkcmd-0.1.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6e9c37bd785f22baed3fb09f985bbdc7eb80dcffc1cf32d8488043ff21c494b |
|
MD5 | 8bd13aafe06b4b8777ec15b7259184a2 |
|
BLAKE2b-256 | 1363b011ae850cdc70c60d470eed3d605b3f35f6754fc528dbce1464e916c3e2 |
File details
Details for the file ptkcmd-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: ptkcmd-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abf21fc474051c6d0617e5fae93eb4061697214cf7bc402a35db079315681839 |
|
MD5 | ede808f8f81cbe8ce33d1e0bd6dc249a |
|
BLAKE2b-256 | 7589443549f3e6d2e94a891baa2c25a192c9fb65a92de498b16f2cac1c670000 |