Skip to main content

A simple and easy-to-use module that streamlines the whole user-input side of your script.

Project description

PyCommandsTool 2.0.X

PyCommandsTool, through the magic of Decorators, allows you to easily bind commands to tokens that can be input in the console and executed seamlessly! Read below for more info!

For around two years, PyCommandsTool was deprecated. However, I have returned to development on it for a project that uses this tool. I have updated it immensely and hope it can be of as much use to you all as it has been to me.

Need the documentation?

Copyright Haven Selph 2022, MIT License, see LICENSE.md file for more info.

Key Features

  • Allows any function to be linked to a custom name and aliases.
  • Parses user input and allows the passing of function arguments ex:
    • <command> arg1 arg2 "arg 3"
    • <command> arg1=str arg2=#int
    • <command> arg1=[this, is, a, list] arg2=str
    • Parser also has specific errors, and you can handle them as you wish! For instance, if a command is not found, execute will return with a NoSuchCommand error. Or, if no input is passed, it will return NoValidInput. There are a couple more instances, check below for more info on specific parts!
  • Auto generated help command to list all of your commands and their uses. (Requires you to set them explicitly.)
  • Very customizable settings so you can use this tool how you want to!

Installing The Tool

Linux/Windows:  
pip install PyCommandsTool  

Using The Tool

from src.PyCommandsTool import Commands

COMMANDMODULE = Commands()


# This is a decorator, it passes the below function as the first argument!
@COMMANDMODULE.add_command("hi", does="prints hello world")
def hi():
	print("Hello world!")


# You can pass an infinite number of names for the command to be known as
# then at the end, you can pass "does" so you can have a description for it!
@COMMANDMODULE.add_command("echo", "repeat", "print", does="prints any passed arguments")
def echo(*args):
	for x in args:
		print(str(x))


@COMMANDMODULE.add_command("echotwice", "repeattwice", "printtwice", does="prints any passed arguments; but twice")
def echotwice(*args):
	print(args)
	for x in args:
		print(str(x), str(x), sep="\n")


# Decorators are just a pretty way of doing the below:
def add(a: int, b: int, *args: int, print_it: bool = True) -> int:
	x = sum((a, b, *args))
	if print_it:
		print(x)
	return x


# Notice how in this, I pass the function as an argument. Functionally, this works EXACTLY the same as the above "decorators"
add = COMMANDMODULE.add_command("add", does="returns the sum of all passed arguments (integers required)")(add)

# Now that you've made and registered all your commands, you can create an input loop:
try:
	while True:
		try:
			last = COMMANDMODULE.execute(input(">>> "))
			if last[0] == 0:
				# Code did not encounter an error!
				pass
		except COMMANDMODULE.NoValidInput as e:
			# This is actually a ParseError which can be caught as shown below this except statement!
			print(e)

		except COMMANDMODULE.ParseError as e:
			# Code here runs when any parse error is thrown!
			print(e)
		except COMMANDMODULE.NoSuchCommand as e:
			# Code here runs if the command passed doesn't exist!
			print(e)
		except TypeError as e:
			# This is the except statement you need to add.
			print(e)

except KeyboardInterrupt:
	# Code here runs when CTRL+C is pressed, or when KeyboardInterrupt
	# is thrown as an error.
	pass

The above code will wait for input. Here's what it would output if you were to input "invalidcommand":

inavalid command output

Here's what it would output if you input the add command, but didn't pass integers to it:

error output

Since this tool doesn't actually catch errors, you are going to have to do this yourself. To stop this from happening, you could add an except to your input loop:

try:
	...
except ... as e:
	...
except TypeError as e:
	# This is the except statement you need to add.
	print(e)

Now, the output after this change would be:

better error output

Make sure to read the DOCs for more info. Thanks for using PyCommandsTool!

Issues?

If you believe you have found a REAL issue with my tool, open a GitHub Issue and describe it thoroughly. Please include your code (or something alike that reproduces it) and ANY changes to mine. If you know of a fix, you could also describe that to help me get it fixed quicker!

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

PyCommandsTool-2.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyCommandsTool-2.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file PyCommandsTool-2.0.1.tar.gz.

File metadata

  • Download URL: PyCommandsTool-2.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for PyCommandsTool-2.0.1.tar.gz
Algorithm Hash digest
SHA256 60ab2a5c32dd29ea39ce7603d58e05305df414e0ae92cb6f583daf24ad671648
MD5 e43b67a07a79b806d103ab29f3dbeb29
BLAKE2b-256 883c709365eecce85c1141f2b1871c86e4d6c829228442c7caad1a8985248b4d

See more details on using hashes here.

File details

Details for the file PyCommandsTool-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: PyCommandsTool-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for PyCommandsTool-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bde571c172be8b9aa4afc41f8882ccb9f601651ba39b6f1f4df054937d7b3580
MD5 a8ecb0cf2396d718e847e8abcc770d88
BLAKE2b-256 a28e7fb2442ee5705dd538936f34bc331493974c939ae9ef42de8f77dbd89fc3

See more details on using hashes here.

Supported by

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