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)


    else if(strstr(argv[0], TEST2))


    else if(strstr(argv[0], TEST3))


    else if(strstr(argv[0], TEST4))


    else if(strstr(argv[0], TEST5))



        printf("Nothing to do!!!\n");




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

Now if you run test1 the flow will go trough the test1 function
if you run test2 the flow will go trough the test2 function