Package: barim
BarimBarim is a Python library that helps building CLI applications.TheBarim APImakes it easy to build terminal app that have one or more commands.RequirementsPython (3.7.X or later)Dev requirementsGit (latest version)Python (3.7.X or later)Poetry (latest version)InstallingInstall with pip.python-mpipinstallbarimInstall with poetry.poetryaddbarimQuickstartThis following section is dedicated to explain how barim can be used to
create a simple CLI application. We will see how to make a terminal application
with a default command and how we can update it to allow multiple subcommand.Create your first commandThe first step will be to create a class that inherit frombarim.Commandand
provide some values to the class attributes.# File name: example.pyfromtypesimportSimpleNamespacefrombarimimportCommandclassMyCommand(Command):name="example.py"description="Print 'Hello, World!'"version="0.1.0"defhandle(self,argv:SimpleNamespace,opts:SimpleNamespace)->None:print("Hello, World!")Take note that the class attributes 'description' and 'version' aren't mandatory.
Only 'name' is.Thehandle()methodmust be overridehas it will be the entry point for your
software. The argv and opts parameters are two SimpleNamespace that contain
all the arguments (argv) and all the options (opts) declared in the
command class. To see how to use them check out theBarim APIor just continue
the quickstart guide.The next logical step now is just to tell barim to run the command when the
script is run. This can be done like the following.fromtypesimportSimpleNamespacefrombarimimportApplication,CommandclassMyCommand(Command):name="my_command"description="Print 'Hello, World!'"version="0.1.0"defhandle(self,argv:SimpleNamespace,opts:SimpleNamespace)->None:print("Hello, World!")# Notice we add a main method to be called once the script is runneddefmain()->None:application=Application(name="example.py")application.register(MyCommand)application.run(default=True)if__name__=="__main__":main()That should be all ! You can now test your CLI application by running the
following command.pythonexample.py--helpThe output of the command should be:example.pyversion0.1.0

DESCRIPTIONPrint'Hello, World!'USAGEexample.py[-h][-V][-v]GLOBALOPTIONS-h(--help)Displayhelpmessage-V(--version)Displayversionnumber-v(--verbose)DisplaymorelogmessageSomething to notice here is that the output doesn't show any colors (in this README)
which will not be the case on the terminal.Add arguments and options to the commandLet's say now that we want to take a string as an argument to print 'Hello, {input}!'.
For this we need to declare another class variable named 'arguments' that
is a list ofbarim.Argument.fromtypesimportSimpleNamespacefrombarimimportApplication,Argument,CommandclassMyCommand(Command):name="my_command"description="Print 'Hello,{input}!'"version="0.1.0"arguments=[Argument(name="input",description="Use input string in print statement"),]defhandle(self,argv:SimpleNamespace,opts:SimpleNamespace)->None:# Make use of the newly added argumentprint(f"Hello,{argv.input}")defmain()->None:application=Application(name="example.py")application.register(MyCommand)application.run(default=True)if__name__=="__main__":main()Note that you can declare abarim.Argumentor by providing the needed data
during initialization like in the example above or by subclassing it and declaring the data as class variable.You should now be able to run your script like the following.pythonexample.pyDemoExpected output:Hello,Demo!Now when it comes to options, it doesn't change that much. Instead of declaring
arguments you declare options and provide a list ofbarim.Option.
In our example let's say we want to turn all the letter uppercase.fromtypesimportSimpleNamespacefrombarimimportApplication,Argument,Command,OptionclassMyCommand(Command):name="my_command"description="Print 'Hello,{input}!'"version="0.1.0"arguments=[Argument(name="input",description="Use input string in print statement"),]options=[Option(short="u",long="upper",description="Turn all the letter uppercase",),]defhandle(self,argv:SimpleNamespace,opts:SimpleNamespace)->None:# Make use of newly added optioninput_=argv.inputifopts.upper:input_=input_.upper()print(f"Hello,{input_}")defmain()->None:application=Application(name="example.py")application.register(MyCommand)application.run(default=True)if__name__=="__main__":main()Now run your script as below.pythonexample.pydemo--upperExpected output:Hello,DEMO!Create subcommandsThe default parameter declare earlier inapplication.run(default=True)indicate that we only have one registered
command and that we want to use it as the default one. By removing this parameter we can now register multiple command
to act as multiple sub command.fromtypesimportSimpleNamespacefrombarimimportApplication,Argument,Command,OptionclassMyCommand(Command):name="my_command"description="Print 'Hello,{input}!'"version="0.1.0"arguments=[Argument(name="input",description="Use input string in print statement"),]options=[Option(short="u",long="upper",description="Turn all the letter uppercase",),]defhandle(self,argv:SimpleNamespace,opts:SimpleNamespace)->None:input_=argv.inputifopts.upper:input_=input_.upper()print(f"Hello,{input_}")classMyOtherCommand(Command):name="my_other_command"description="Print 'Hello, World!'"version="0.1.0"defhandle(self,argv:SimpleNamespace,opts:SimpleNamespace)->None:print("Hello, World")defmain()->None:application=Application(name="example.py")application.register(MyCommand)application.register(MyOtherCommand)application.run()if__name__=="__main__":main()Create command dynamicallyAs seen before, to create a command, we have to subclassbarim.Command.
But this is not the only way we can create them. In any case you need to create them,
for example on the fly, you could do it like in the following example.fromtypesimportSimpleNamespacefrombarimimportApplication,Commanddefmy_command_handle(argv:SimpleNamespace,opts:SimpleNamespace)->None:print("Hello, World")defmain()->None:my_command=Command(name="my_command",description="Print 'Hello, World!'",handle=my_command_handle)application=Application(name="example.py")application.register(my_command)application.run(default=True)if__name__=="__main__":main()The output of the command should look like the following:example.pyversion0.1.0

DESCRIPTIONPrint'Hello, World!'USAGEexample.py[-h][-V][-v]GLOBALOPTIONS-h(--help)Displayhelpmessage-V(--version)Displayversionnumber-v(--verbose)Displaymorelogmessage