Skip to main content

cmd.py from the standard library adapted to use prompt-toolkit instead of readline

Project description

ptkcmd

View API documentation

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

from ptkcmd import PtkCmd, Completion

class MyCmd(PtkCmd):
    def __init__(self,stdin=None,stdout=None,intro=None,interactive=True,do_complete_cmd=True,default_shell=False,**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'.

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

    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().

	"""
	...
    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
	"""
	...
    def complete_mycmd(self,args,document,complete_event):
    	"""
	This method defines completion rules for the command named 'mycmd'

	args = the entered line parsed by shlex, excluding the name of the 
        command itself
	document and complete_event are as defined in the prompt-toolkit 
        documentation
	Should yield Completion objects as defined in prompt-toolkit
	"""

Project details


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.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

ptkcmd-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file ptkcmd-0.1.0.tar.gz.

File metadata

  • Download URL: ptkcmd-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 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

Hashes for ptkcmd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fb92acb47ec091bf59dc1247340d57a09b50bbee0007c041f86b8cc94c133c56
MD5 4b36a73da46793fbd1d1051dffa3484f
BLAKE2b-256 fe731741f5860fe5912499247ecb5ca8414b1563ddd851b12b6d41395a44f614

See more details on using hashes here.

File details

Details for the file ptkcmd-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ptkcmd-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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

Hashes for ptkcmd-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ae6490ef7c70a2387102f87f28b0ecf7ccef8807de89d5fb0fc4a54d982f064
MD5 311543aca5972eb65df22fba71d07d02
BLAKE2b-256 bf83b84fa0789fa117f7a3a7d9c9e3adf632d4dad28368948a3236ce5508ed7a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page