C Code Generator for command line arguments

Public: C Developpers.

Suppose you want to write an application that can take command line arguments, for example like gcc does:

   gcc -Wall -s -o prog prog.c

You could rip some code from an existing app. But this will require manual editing to fit your specific needs for your application. As time is crucial in development, this is where this C code generator comes in.

The generator is reading an input file, to generate a new file called gen.c. The generated file can be compiled as is.

Suppose you have an application that can be called with:

./app [-v] [-i] [-q] -o output [-f input]

Where options between brackets are optionnal. The equivalent input file to be parsed with the generator would be:
  -v;	-	;	Verbose			;	int verbose	;0
  -i;	-	;	Initialiser truc	;	void Init()	;0
  -q;	-	;	Agit en silence		;	char quiet	;0
  -o;	OUTPUT	;	Fichier de sortie	;	char* output	;1
  -f;	INPUT	;	Fichier d'entrée	;	char* input	;0
Each line contains five informations:


Informations are separated with a semi-colon. This is very important. Tab characters will be ignored. They are used for presentation.
The process is pretty easy, is't it? What a lot of time was gained here! How long to write the corresponding C code by hand?

The above example file, after being parsed, will produce this C file:
/* Generated by args_generator
   Input data:
  -v             -                 Verbose             int verbose    0
  -i             -        Initialiser truc             void Init()    0
  -q             -         Agit en silence              char quiet    0
  -o        OUTPUT       Fichier de sortie            char* output    1
  -f         INPUT        Fichier d'entrée             char* input    0

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Write here the purpose of your app for usage printing */
#define APPLICATION_PURPOSE "Purpose of this application"

typedef struct {
  char* option;
  char* param;
  char* description;
} opt;

opt cfg[5] = {
  { "-v",NULL,"Verbose"},
  { "-i",NULL,"Initialiser truc" },
  { "-q",NULL,"Agit en silence" },
  { "-o","OUTPUT","Fichier de sortie" },
  { "-f","INPUT","Fichier d'entrée" }

/* Configuration Variables */
int verbose;
char quiet;
char* output;
char* input;

/* Functions called by command line args. Fill with code. */
void Init()

/* Parse command line arguments, show usage if required */
void do_args(int argc, char **argv)
  int i, h=0;

/* Parse argv[] array */
  if (argc<2) h=1;
    for (i=1; i<argc; i++)
      if (*argv[i]!='-') { h=1; break; }
      else if (strcmp(argv[i],"-v")==0) verbose=1;
      else if (strcmp(argv[i],"-i")==0) Init();
      else if (strcmp(argv[i],"-q")==0) quiet=1;
      else if (strcmp(argv[i],"-o")==0) output=argv[++i];
      else if (strcmp(argv[i],"-f")==0) input=argv[++i];
      else { h=1; break;} /* unknown option */

  /* Print usage */
  if (h)
    printf("%s : %s\n"
           " usage: %s [%s] [%s] [%s] %s %s [%s %s]\n"
           " Les options entre crochets sont facultatives.\n"
              ,argv[0], APPLICATION_PURPOSE, argv[0],
              cfg[3].option, cfg[3].param,
              cfg[4].option, cfg[4].param,
              cfg[0].option, cfg[0].description,
              cfg[1].option, cfg[1].description,
              cfg[2].option, cfg[2].description,
              cfg[3].option, cfg[3].description,
              cfg[4].option, cfg[4].description);

int main(int argc, char **argv)
  do_args(argc, argv);

  return 0;
Rename it as main.c, but you could also rename it as .h file.

Get the generator here, and the example file here.
Compile the generator with
$ gcc -Wall -s -o argsgen argsgen.c
Convert the example file into a complete C file with:
$ ./argsgen test.argsgen
And open up gen.c, that's all!

This program could be part of an IDE with a wizzard that allows total non-programmers to write applications.

Have fun and enjoy your free time!

Masta 2006