Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for creating a unix/linux command-line tool in C/C++?

Tags:

People also ask

How do you create a command in Unix?

To prepare to use make, you must write a file called the makefile that describes the relationships among files in your program, and the states the commands for updating each file. In a program, typically the executable file is updated from object files, which are in turn made by compiling source files.

Does Linux use Unix commands?

Linux system is derived from Unix and it is a continuation of the basis of Unix design. Linux distributions are the most famous and healthiest example of the direct Unix derivatives. BSD (Berkley Software Distribution) is also an example of a Unix derivative.

What is a Unix command?

Unix commands are inbuilt programs that can be invoked in multiple ways. Here, we will work with these commands interactively from a Unix terminal. A Unix terminal is a graphical program that provides a command-line interface using a shell program.

How do you create a file in Unix?

To create a new file run the cat command followed by the redirection operator > and the name of the file you want to create. Press Enter type the text and once you are done press the CRTL+D to save the files.


I currently am tasked with creating some command-line helper utilities for our internal development team to use. However, I want to know the best practice for creating unix command-line tools. I have tried viewing git source code for an example of how to read parameters and display messages accordingly. However, I'm looking for a clear template for creating a tool, reading parameters safely, and displaying the standard "help" messages if a user types in an incorrect parameter or --help I want to show the help message. Is there a standard library for reading -abcFGH and --parameter and switching which process starts based upon the passed parameter?

Command-Line:

git

or

git --help

Output:

usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
       [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
       [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
       [-c name=value] [--help]
       <command> [<args>]
...

Command-Line:

MyTool CommandName --CommandArgs

Output:

Whatever that specific command does.


What I have working so far:

Code:

int main(int argc, char **argv)
{
    if(argc < 2)
    helpMessage();
    char* commandParameter = argv[1];
    if (strncmp(argv [1],"help", strlen(commandParameter)) == 0)
        helpMessage();
    else if (strncmp(argv [1],"pull", strlen(commandParameter)) == 0)
        pull();
    else
        helpMessage();
}

What would be ideal would look like this:

Code:

int main(int argc, char **argv)
{
    MagicParameters magicParameters = new MagicParameters(argv);
    switch(magicParameters[1])
    {
        case command1:
            Command1();
            break;
        case ...

        case help:
        default:
            HelpMessage();
            break;
    }
}