Package: python-console-creator
Console CreatorPython lib that makes creating a console a simple task !This lib enables you to create a console where the user can input commands, with features such as command aliases, automatic help generation, and much, much more !InstallInstall from PyPITo install the library, just typepip install python-console-creatorand this should be ok.VisitPyPIfor more info.Install from sourceJust download the file atthis link, and import it in your project.UsageSeeexamplesif wanted.This library provides multiple classes.TheConsoleclassThis class is the main console.How to use it ?Simply store it in a variable like so :fromconsole_creatorimport*console=Console()While constructing the class, you can pass in some of the following parameters (all optionals) :pointer: A string containing the element that will tell the user that he should input a command. By default,>>>.lowercase_commands: A boolean that indicates whether or not commands should match the case. E.g. if False(by default)testandTESTwon't be the same command. Otherwise, they will be the same.exit_aliases: A tuple or list of strings containing the name of the commands that should let the user quit the console. By default,exit,end, andquit.ignore_minor_errors: A boolean that indicates whether or not functions with no arguments should throw an exception if arguments are given. By default, False.optional_args_prefix: The prefix in front of an optional argument. By default,--.end_message: The message to display on console close. Can also be None, and in that case, nothing will be printed. By default, "Console closed."help_ignore_no_description: Boolean indicating whether or not the help command should print if no description is provided for a command. Default is False.This class contains a few methods that will help you create your commands.ThelaunchmethodThis method will launch the console.*/!\ Beware ! After this method is called, it is no longer possible to add commands ! /!*ThedestroymethodThis method will destroy the console.Past this point, you can add commands again or launch the console again.Theadd_commandmethodThe main method of theConsoleclass.This method allows you to add new commands to the console.The most important commands should be added first, as they will be checked in the order in which they have been added.The later you add a command, the further it is situated in the commands list, the longest it takes. (It is just a matter of milliseconds, still)**/!\ An exception will be raised if you try to create two commands with the same name. /!\ **This method takes as argument an instance of theCommandclass, which is described just below.Otherwise, a command will be created in place, but will no longer be capable of being modified.This method, even though is working and will be maintained, is not recommended.TheCommandclassThis class is the class that composes a command.It takes in as required positional arguments :name: A string that defines the name of the command. Should not contain any space, otherwise an exception will be raised.function_to_call: A callback function to call when the command is triggered.It can take as non-required keyword arguments the following statements :description: A short description of the function, for the help command.catch_others_args: A boolean indicating whether or not to return all the args inputted by the user as a tuple. False by default.help_args_msg: Whether or not the help command should print that the command does not have any arguments if this is the case. True by default.It can finally take two other keyword arguments :required_argumentsandoptional_arguments.These arguments are a tuple or a list of instances of theArgumentclass(see below), and for theoptional_arguments, each instance should have its parameteroptionalset toTrue.The callback function defined above should take each of therequired_argumentsas positional arguments, each of theoptional_argumentsas optional arguments, and another positional argument if the parametercatch_others_argsis set toTrue.TheArgumentclassThis class requires one argument :name, a string containing the name of the argument. This will mainly be used for the help command if the argument is required.The class also provides two "optional" arguments(although I recommend you to fill them):description: A short description of the argument, for the help command.optional: A boolean that indicates whether the argument is required or not.THIS PARAMETER ABSOLUTELY REQUIRES TO BE SET TOTrueIF THE ARGUMENT IS OPTIONAL !Theconvert_typefunctionAs the commands inputted by the user always returns strings, I left you this function to convert a variable to the type that fits it the most amongNone,bool,int,float, andstring.(The types will be tested in that order)ExamplesHere are some examples of consoles, from simple to more advanced.Example 1 : A console with only a help command and a way to exitI mean, why not ?You just need to import the library, create an instance ofConsole, and launch it.fromconsole_creatorimport*console=Console()console.launch()Let's say we want this console to be only left if the user types the commandleave, where the case of the commands doesn't count, and where no end message is printed when the console is closed.The code should now look this way :fromconsole_creatorimport*console=Console(exit_aliases=("leave",),lowercase_commands=True,end_message=None)console.launch()Example 2 : A console containing only a command printing "Hello World !"To do so, we need to create a function with no arguments, and to add a command in the console.fromconsole_creatorimport*# We create the hello world function heredefhello_world_function():print("Hello World !")# We create the consoleconsole=Console()# We add the hello-world commandconsole.add_command(#           name        callback functionCommand("hello-world",hello_world_function))# We launch the consoleconsole.launch()But this one does not feature an explicit description for users who need it, so let's add one !Just replace the line 13 with this line :Command("hello-world",hello_world_function,description="Don't look at me, I just say\"Hello World !\":D")Example 3 : A 'hello' command, that says hello to the user with his given nameHere, we will need to add a required argument that will be thenameof the user.fromconsole_creatorimport*# We create the hello function heredefhello_function(name):print(f"Hello,{name}!")# We create the consoleconsole=Console()# We add the hello commandconsole.add_command(Command("hello",hello_function,description="This commands says hello to the best user in the world ;)",required_arguments=(Argument("name",description="Well, your name xD"),)))# We launch the consoleconsole.launch()If you dohello <your name>, it should respondHello, <your name> !, and that without any effort, and a clean code.Example 4 : The same example as above, but this time thenameargument is not requiredLet's take the code from the last example, and remake a few lines.fromconsole_creatorimport*# We create the hello function here# The 'name' argument is set to None by defaultdefhello_function(name=None):# If name is none, we just print 'Hello !', otherwise we also print the user's nameifnameisNone:print("Hello !")else:print(f"Hello,{name}!")# We create the consoleconsole=Console()# We add the hello command# This time, we change 'required_arguments' to 'optional_arguments', and we switch the Argument 'optional' parameter to 'True'console.add_command(Command("hello",hello_function,description="This commands says hello to the best user in the world ;)",optional_arguments=(Argument("name",description="Well, your name xD",optional=True),)))# We launch the consoleconsole.launch()I let you try it yourself.Example 5 : Let's combine both !Let's create a command to send a message (required arg) to the user, which can be signed or anonymous (by default).fromconsole_creatorimport*# We create the function here :defsend_message_function(message,signature="This message was anonymous"):print(message)print(signature+".")# We create the consoleconsole=Console()# We add the commandconsole.add_command(Command("send-message",send_message_function,description="This send your lovely message to the user",required_arguments=(Argument("message",description="Your message to the user"),),optional_arguments=(Argument("signature",description="Your name, or leave it blank to be anonymous.",optional=True),)))# We launch the consoleconsole.launch()If you inputsend-message "I love you, my friend." David, you should receive in the console the following message :I love you, my friend.
David.Now, if you input this insteadYou've been hacked !(so the second arg is not filled), you should get this :You've been hacked !
This message was anonymous.Example 6 : Keeping the commands in variables to be able to change themI'll just leave the code, try for yourself, and have fun understanding it ;)fromconsole_creatorimport*defonly_call_me_once_1():print("It's ok :D")only_call_me_once_command.description="YOU ALREADY CALLED IT !"only_call_me_once_command.function_to_call=only_call_me_once_2defonly_call_me_once_2():print("TOLD YOU TO ONLY CALL ME ONCE !")only_call_me_once_command.description="YOU FOOL !"console=Console()only_call_me_once_command=Command("only_call_me_once",only_call_me_once_1,"ONLY CALL IT ONCE !",help_args_msg=False)console.add_command(only_call_me_once_command)console.launch()In ConclusionI hope you like this library as much as I do !I've put some effort in making it, so I would love to hear your thoughts and comments about it on myDiscord server.If you find any bug, or are not sure about something, just post it on the Discord oropen an issue.