Configuring CLI with Protobuf and Python
Many of us may think that the easiest way to interact with computers is through a teeny-tiny device; we all know as a mouse. However, we programmers love to use another one. Even before the mouse came into the picture, the interaction between people and operating systems happened with a keyboard where the user typed commands to instruct machines to do things. The easiest way to give these commands is through the Command Line Interface (CLI). CLI is a text-based user interface that you can use to view and manage computer files and programs. Usually, the CLI features a black box with white text. Nowadays, many users choose to use the graphical user interface (GUI) compatible with operating systems like Windows, Linux, and Mac. The best examples of CLIs are the MS-DOS operating system and the Windows operating system’s command shell. Even the programming languages like Python supports the CLIs. And that is exactly what this blog is about.
In this blog, I will try to explain an easy way to configure CLI with protobuf using the Python language. But before the actual process, some general trivia:
What is protobuf?
Protobuf, popularly known as Protocol Buffers, is developed by Google. It is Google’s language-neutral, platform-neutral, and a binary encoding format that helps create an outline for data. You can use a specially generated source code once you figure out to structure your data. This generated source code allows you to write and read your structured data easily. You can do it from various data streams by using different languages. The structure of protobuf looks something like this:
message Employee {
required int32 id = 1;
required string name = 2;
optional string email = 3;
}
You can implement the Protocol Buffers specifications in many languages. It supports all the leading languages such as Java, C, Go, etc. Protobuf compiles files from specified directory to generate output files. The compiler reads the files as src/foo.proto and src/bar/baz.proto which are the protobuf files extensions. It produces two output files that are build/gen/foo_pb2.py and build/gen/bar/baz_pb2.py. The compiler also creates the build/gen/bar automatically if required, but it won’t create build or build/gen as they should exist already.
Note that if the .proto file or its path contains any characters which cannot be used in Python module names like hyphens, they will be replaced with underscores. E.g. the file foo-bar.proto becomes the Python file foo_bar_pb2.py. To know more about Python protobuf you can visit protobuf docs by Google developers here.
What is Python?
Now, everyone reading this must be aware of what Python is. However, here is some necessary information. Python is an object-oriented programming language used to develop web applications, workflow creation, reading and modifying files, handling big data, and performing complex mathematical operations and system scripting. It is one of the popular languages for Rapid Application Development because of its ability to offer dynamic typing and dynamic binding options. Now, let’s find out the relation between Python and Cmd2 Library…read more.