Skip to main content

ptkcmd

View API documentation

ptkcmd adapts the built-in cmd.py standard library module to use prompt-toolkit instead of readline

Constructor inputs:

from ptkcmd import PtkCmd, Completion, complete_files
class MyCmd(PtkCmd):
    prompt='MyPtkCmd$ ' #change the prompt
    def __init__(self,stdin=None,stdout=None,intro=None,
		interactive=True,do_complete_cmd=True,
		default_shell=False,**psession_kwargs):

        super().__init__(stdin,stdout,intro,interactive,
		do_complete_cmd,default_shell,**psession_kwargs)

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'.

The following snippet defines a command named 'mycmd'. The args input is the list of arguments entered after the command.

class MyCmd(PtkCmd):
    ...

    def do_mycmd(self,args)
        self.stdout.write('mycmd args were %s\n' % repr(args))

    def complete_mycmd(self,prev_args,curr_arg,document,complete_event):
        yield from complete_files(curr_arg)

Regarding the completion method, prev_args = the arguments prior to the current one being completed curr_arg = the current argument being completed. curr_arg 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

The completion method must 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.

complete_files(curr_arg,path=None) provides the a filename completer

Typing 'help mycmd' will show the docstring of do_mycmd(). To add miscellaneous help topics, define a help function:

class MyCmd(PtkCmd):
    ...

    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')

Release files for ptkcmd 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ptkcmd 0.1.3
File Size Uploaded
ptkcmd-0.1.3.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ptkcmd 0.1.3
File Interpreter ABI Platform
ptkcmd-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / ptkcmd-0.1.3.tar.gz

Download URL ptkcmd-0.1.3.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
29d4338e1c30603d9f0bd5cd39318751845444e90f19490054f0b54773ac50bf
BLAKE2b-256 checksum
How to use checksums
30ea12299d0046206214c447caae3ce4ea7f13e676bf227b0c56d970e5ddaf20
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / ptkcmd-0.1.3-py3-none-any.whl

Download URL ptkcmd-0.1.3-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72f985021cf667b9aeac6bbc12006122fcb86dd64edf82cc0618ae9d288c854b
BLAKE2b-256 checksum
How to use checksums
f1a15c08a1b9dfb82e87efe2a92720ea9f81fd6e9a97bf00254273a74aec1a97
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page