1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| int main(int argc, char* argv[])
{
const struct option long_options[]= /*to specify long options*/
{
{"help",0,NULL,'h'},
{"version",0,NULL,'V'},
{"verbose",0,NULL,'v'},
{"output",1,NULL,'o'},/*output has an argument in which results
will be stored*/
{"generate",2,NULL,'g'},/*optional size argument, default=9*/
{NULL,0,NULL,0}
};
program_name=basename(argv[0]);
out_file=stdout;
int optc;
while ((optc=getopt_long(argc, argv, "hVvo:g::",long_options,NULL))!=-1)
{
switch(optc) /*to consider the different options*/
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
case 'v':
verbose=true;
break;
case 'o':
out_file=fopen(optarg,"w+");
if (out_file==NULL) /*to treat the case when the file can't
be opened*/
{
fprintf(stderr,"Error : the file %s cannot be opened !\n",optarg);
usage(EXIT_FAILURE);
}
break;
case 'g':
if(optarg)
{
grid_size=atoi(optarg);
fprintf(out_file,"argument %s\n",optarg);
}
else
{
grid_size = 9;
fprintf(out_file,"size = %d\n",grid_size);
}
fprintf(out_file,"option g\n");
break;
default:
usage(EXIT_FAILURE);
}
}
if (optind>=argc) /*tests the presence of argument in line command*/
usage(EXIT_FAILURE);
FILE* grid_file;
grid_file=fopen(argv[optind],"r");
if(grid_file==NULL)
{
fprintf(stderr,"Error : the file %s cannot be opened !\n",argv[optind]);
usage(EXIT_FAILURE);
} |