In C, if you are developing a complex application that requires a large number of command line option you should use the getopt function to handle them, if your application needs a small number of command line arguments you can think to manage them with your own code.
Sometimes you need only a parameter (for example if you are developing a simple test application), so the parameter is simply a switch. In this case you can use the application name as your command line argument, simply make a soft link to your application for each case you want to handle.
/*+———————————————————-+
| test.c |
| an alternative way to parse command line arguments. |
| |
| Created by Luigi D'Andrea on 14/08/2011. |
+———————————————————-+*/
/*+———————————————————-+
| SYSTEM INCLUDE |
+———————————————————-+*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*+———————————————————-+
| DEFINES |
+———————————————————-+*/
#define TEST1 "test1"
#define TEST2 "test2"
#define TEST3 "test3"
#define TEST4 "test4"
#define TEST5 "test5"
/*+———————————————————-+
| FUNCTIONS PROTOTYPES |
+———————————————————-+*/
int main(int argc, char **argv);
void test1(void);
void test2(void);
void test3(void);
void test4(void);
void test5(void);
void test1(void)
{
printf("Running test1\n");
}
void test2(void)
{
printf("Running test2\n");
}
void test3(void)
{
printf("Running test3\n");
}
void test4(void)
{
printf("Running test4\n");
}
void test5(void)
{
printf("Running test5\n");
}
int main(int argc, char **argv)
{
if(strstr(argv[0], TEST1)!=NULL)
test1();
else if(strstr(argv[0], TEST2))
test2();
else if(strstr(argv[0], TEST3))
test3();
else if(strstr(argv[0], TEST4))
test4();
else if(strstr(argv[0], TEST5))
test5();
else
printf("Nothing to do!!!\n");
exit(EXIT_FAILURE);
}
Now compile this program with the following command:
peppo:test $ gcc -o test test.c
and run the following command to generate the soft links.
peppo:test $ ln -s test test1
peppo:test $ ln -s test test2
peppo:test $ ln -s test test3
peppo:test $ ln -s test test4
peppo:test $ ln -s test test5
memcmp suggested me to add an use case to this article because it looks like very strange. So in the following few lines I'm going to explain a possible use of this way of parsing command line arguments.
Often my customers ask me to re-write tools for their old (and qualified) application.
Please imagine an application that uses a system() call to launch a new process to read from a RS232 serial device or a new process to write on the same serial device
some_stuff();
system("/my/path/read");
some_stuff()
system("/my/path/write");
exit(0);
Please imagine a new system without the serial device but with an ethernet board.
You can choose two ways:
– You can rewrite all the application (and qualify again all the applications)
– You can write a new tool that uses the ethernet board (and qualify only the tool).
The second one is the best choice, especially for the customer.
In this case I use this method and then I can provide only an executable with two links instead of two executables (more room on occupied on the target disk, two applications on my versioning system, etc etc)
I hope I have clarified the benefit in using this method.
Gg1