Finally Releasing My Simple Command Module!
slickassassin03 (69)


This module is called PyCommands. It is made with the intent of streamlining command line applications made by you! Its memorable syntax and limitless possibilities make it a great choice for any Python program you want to make.

I do plan on making a different post on how to do more complex things, but this is what you get for now.


  • Create a file named
  • Paste the below code into said file
from shlex import split

class Command:
  def __init__(self, fn, name=None, aliases=None,): = name or fn.__name__
    self.aliases = aliases or []
    self.fn = fn

  def __call__(self, *args, **kwargs,):
    return self.fn(*args, **kwargs,)

  def all_names(self,):
    return, *self.aliases

class Commands(dict):
  def __init__(self, not_found: str='{} was not recognized.', argmismatch: str='{} takes {} arguments but {} were given.',):
    self.flag = [not_found, argmismatch,]

  def _add_command(self, command,):
    for cmd_name in command.all_names:
      if cmd_name in self:
        raise ValueError(f'Name or alias assigned to function {command.fn.__name__} is duplicate: {cmd_name}',)
      self[cmd_name] = command
  def add_command(self, name=None, aliases=None,):
    def inner_fn(fn,):
      self._add_command(Command(fn, name, aliases,),)
      return fn
    return inner_fn

  def execute(self, user_input,):
    self.command, *args = self.parse(user_input or 'no_input',)
    if self.command.lower() in self:
      except TypeError:
        return (False, self.flag[1].format(self.command, self[self.command].fn.__code__.co_argcount, len(args,),),)
        return (True, 'Command found!',)
      return (False, self.flag[0].format(self.command,),)
  def parse(string,):
    if (string=='no_input'):
      return ['']*2
      return split(string,)
    except ValueError:
      return ['']*2
  • Import pycommands
  • Done!

How to use it

  • Initialize the module:
import pycommands

commands = pycommands.Commands(
 '{} was not recognized as a command',
  • Add Commands
def clear():

def echo(*args,):
  • Create Input Loop
while True:
    cmd = commands.execute(input('>>> ',),)

    if not cmd[0]:  # Checks if command gave error
        print(cmd[1])  # Prints error message

And you are done!!! Here's what it would look like if you ran it:

>>> notacommand
notacommand was not recognized as a command
>>> echo quotes 'work in the' arguments
work in the

Wrapping Up

That's all you need to know to be able to use the module effectively, but there are many more possibilities. The more experienced of you will find them by reading the code, but for the people who need a tutorial, a post will come out soon. Please upvote this if you like it, and tell me in the comments what you plan to use it for! The program below is the program we wrote in this tutorial, I hope you enjoy, have a good time using PyCommands!

You are viewing a single comment. View All
slickassassin03 (69)

@MarcusWeinberger Did you use the '@' symbol? These are decorators. You cannot simply call the function.