How To Build a CLI Application With C++ in Linux
One of the great things about Linux and Unix platforms is the control you have from executing commands in the terminal. If like me then you probably spend more time automating tasks than actually performing the tasks especially at work. It's very easy to automate certain tasks or improve your workflow by creating CLI scripts to perform the heavy lifting. In this article I'm going to show you how to create a simple CLI that will fix the indentation in a file but the process of creating a CLI in C++ can be used on any task you need.
Why C++ is the GOAT
C and C++ are the languages that were used to develop most of the CLIs and services on Linux operating systems. This is because it not only have better performance than higher level languages but it also allows for more access to memory addresses and pointers. Shell scripts are often used for basic tasks but Posix shell doesn't have the same functionality as other programming languages. C++ is often the best choice for development when shell can't hack it.
Development
I had a simple task I needed for the CLI. I needed to automatically un-indent all the lines in a file. It's often the case when copying over large bodies of text that the lines become over-indented and unusable. This can be seen below.
>Text before
>Text After
I wrote a script that would open the file, edit the lines and write these lines to a new file. I explained the code in more detail in the video above if you want to see it. The code is written below.
int main(int argc, char *argv[]){
char *fvalue;
int c;
opterr = 0;
string line;
char *ivalue;
int indent;
// Checking the file flag is specified
while ((c = getopt (argc, argv, "f:i:")) != -1)
switch (c)
{
case 'f':
fvalue = optarg;
break;
case 'i':
ivalue = optarg;
break;
case '?':
if (optopt == 'f' || optopt == 'i')
fprintf (stderr, "Options -%f and -%i require an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option
-%f'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
indent = atoi(ivalue);
fstream myFile;
ofstream write;
myFile.open(fvalue);
write.open("new_file.txt");
if(!myFile){
cout << "Get gud";
return 0;
}
while(myFile){
getline(myFile, line);
if(line=="-1")
break;
line.erase(0, indent);
write << line << endl;
}
myFile.close();
When the script is developed it can be compiled using the gcc/g++ compiler in the terminal. Simply run the below commands to get an executable command. You may also need to install the 'build-essentials' package before you run the g++ command.
g++ main.cpp -o <Name of your command>
This will give you an executable file that will you can run in the local directory. However, if we want to run this command anywhere we need to add it to the /usr/local/bin/ directory.
cp <Name of your command> /usr/local/bin
Now you can easily execute this command no matter which directory you're in. There are a million different ideas for CLIs out there and depending on what type of work you do on your machine this will change. Coding them from scratch or with other libraries will give you much more opportunities than a simple shell script.
Stay happy and stay private.