Skip to content

How do I add a command to PercOS?

ThePerkinrex edited this page Mar 8, 2018 · 5 revisions

To add a command to PercOS you will need some knowledge of python 3. In any case, adding a command to PercOS is really simple.


Step 1: Adding the command file and the class

First you will need to create a file where your command is going to reside. It's better to name the file in the same way as the command, so in our case we're going to make a command that responds to what the user asks, like a virtual assistant. For that reason we're going to name the file assistant.py. We need the .py to say that it is a python file, that makes PercOS know that it can read it.
For adding the class which is going to house the command we'll need to import the base command class, which has all the things a command needs so that if you forget to add something to your command (it's very unlikely) PercOS wont give out an error. To import that class you only will need to add from command import Command. Then you'll need to add the class, in our case class Assistant(Command):, the '(Command)' is there to say that this class inherits the properties of the Command class.

Step 2: Adding the variables and function

This is really simple, a command has 3 predefined properties (they all are strings):

  1. Name called name, we'll say name = "assistant" so that when the users type in assistant they'll get our command
  2. Description called desc, we'll say desc = "this command starts the assistant" so that the help message shows this
  3. Author called author, in my case I'll say author = "ThePerkinrex"
    And it also has 1 function:
  4. call(self, args=None) where self is your command class, self.dire is a custom class that stores the current directory in PercOS's filesystem (self.dire.dir), the real directory where it is now (self.dire.realdir) and the base directory where all the filesystem is located (self.dire.bd) (PercOS_filesystem/by default). In the command object self.usr is a string with the name of the current user, and args is an optional argument which passes what the user did put after the command. We'll write a print statement so that we see how it works. Additionaly you can add the usage variable, wich tells the usage command how your command works. This is just a string like this: usage='assistant [question]'
    So, your class should look something like this:
from command import Command


class Assistant(Command):
    name = "assistant"
    desc = "This command starts the assistant"
    author = "ThePerkinrex"
    usage = "assistant [question]"

    def call(self, dire, usr, args=None):
        print("this is my assistant")

For more info check out the command API