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:
OPTION ; PARAMETER ; DESRIPTION ; FONCTION/VARIABLE ; REQUIERED
Informations are separated with a semi-colon. This is very important.
Tab characters will be ignored. They are used for presentation.
- OPTION: This is the argument of the command linen for exampel -v .
- PARAMETER: some command line arguments take a parameter. For example gcc
permits to specify the name of the output file with "-o file"
In this case, "file" is the parameter.
Important: if the argument has no parameter, put '-', NOT a blank.
- DESCRIPTION: This sentence will appear when print_usage will be printed, for
example when the applicationis launched without any argument
and some are required.
- FUNCTION/VARIABLE: Specifying an argument via the commandline implies the program
to do something. It will either set a value to 1, or call
a function internally.
If a parameter was specified, the value passed trough
command line will be copied into this variable.
- REQUIRED: The value is 0 or 1. A required command line argument will stop
the execution of program and show usage, if it was not specified.
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()
{
puts("Init()");
return;
}
/* 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;
else
{
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"
"\t%s\t%s.\n"
"\t%s\t%s.\n"
"\t%s\t%s.\n"
"\t%s\t%s.\n"
"\t%s\t%s.\n"
,argv[0], APPLICATION_PURPOSE, argv[0],
cfg[0].option,
cfg[1].option,
cfg[2].option,
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);
exit(1);
}
}
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