21#define FIX_UNUSED(X) (void) (X)
27const char *gengetopt_args_info_purpose =
"Creates simple lattice structures. It requires an initial, but\nskeletal OpenMD file to specify the components that are to be placed\non the lattice. The total number of placed molecules will be shown at\nthe top of the configuration file that is generated, and that number\nmay not match the original meta-data file, so a new meta-data file is\nalso generated which matches the lattice structure.";
29const char *gengetopt_args_info_usage =
"Usage: simpleBuilder [OPTIONS]... [FILES]...";
31const char *gengetopt_args_info_versiontext =
"";
33const char *gengetopt_args_info_description =
"";
35const char *gengetopt_args_info_help[] = {
36 " -h, --help Print help and exit",
37 " -V, --version Print version and exit",
38 " -o, --output=STRING Output file name (mandatory)",
39 " --density=DOUBLE density (g/cm^3) (mandatory)",
40 " --nx=INT number of unit cells in x (mandatory)",
41 " --ny=INT number of unit cells in y (mandatory)",
42 " --nz=INT number of unit cells in z (mandatory)",
43 " -l, --lattice=STRING Lattice Type (possible values=\"FCC\", \"BCC\",\n \"SC\")",
51} cmdline_parser_arg_type;
63cmdline_parser_required2 (
struct gengetopt_args_info *args_info,
const char *prog_name,
const char *additional_error);
65const char *cmdline_parser_lattice_values[] = {
"FCC",
"BCC",
"SC", 0};
68gengetopt_strdup (
const char *s);
86 FIX_UNUSED (args_info);
103 args_info->
help_help = gengetopt_args_info_help[0] ;
105 args_info->
output_help = gengetopt_args_info_help[2] ;
107 args_info->
nx_help = gengetopt_args_info_help[4] ;
108 args_info->
ny_help = gengetopt_args_info_help[5] ;
109 args_info->
nz_help = gengetopt_args_info_help[6] ;
115cmdline_parser_print_version (
void)
121 if (strlen(gengetopt_args_info_versiontext) > 0)
122 printf(
"\n%s\n", gengetopt_args_info_versiontext);
125static void print_help_common(
void) {
126 cmdline_parser_print_version ();
128 if (strlen(gengetopt_args_info_purpose) > 0)
129 printf(
"\n%s\n", gengetopt_args_info_purpose);
131 if (strlen(gengetopt_args_info_usage) > 0)
132 printf(
"\n%s\n", gengetopt_args_info_usage);
136 if (strlen(gengetopt_args_info_description) > 0)
137 printf(
"%s\n\n", gengetopt_args_info_description);
141cmdline_parser_print_help (
void)
145 while (gengetopt_args_info_help[i])
146 printf(
"%s\n", gengetopt_args_info_help[i++]);
152 clear_given (args_info);
153 clear_args (args_info);
154 init_args_info (args_info);
183free_string_field (
char **s)
200 free_string_field (&(args_info->
nx_orig));
201 free_string_field (&(args_info->
ny_orig));
202 free_string_field (&(args_info->
nz_orig));
208 free (args_info->
inputs [i]);
213 clear_given (args_info);
224check_possible_values(
const char *val,
const char *values[])
234 for (i = 0, len = strlen(val); values[i]; ++i)
236 if (strncmp(val, values[i], len) == 0)
240 if (strlen(values[i]) == len)
248 return (found ? -2 : -1);
253write_into_file(FILE *outfile,
const char *opt,
const char *arg,
const char *values[])
258 found = check_possible_values(arg, values);
261 fprintf(outfile,
"%s=\"%s\" # %s\n", opt, arg, values[found]);
263 fprintf(outfile,
"%s=\"%s\"\n", opt, arg);
265 fprintf(outfile,
"%s\n", opt);
282 write_into_file(outfile,
"help", 0, 0 );
284 write_into_file(outfile,
"version", 0, 0 );
286 write_into_file(outfile,
"output", args_info->
output_orig, 0);
288 write_into_file(outfile,
"density", args_info->
density_orig, 0);
290 write_into_file(outfile,
"nx", args_info->
nx_orig, 0);
292 write_into_file(outfile,
"ny", args_info->
ny_orig, 0);
294 write_into_file(outfile,
"nz", args_info->
nz_orig, 0);
296 write_into_file(outfile,
"lattice", args_info->
lattice_orig, cmdline_parser_lattice_values);
309 outfile = fopen(filename,
"w");
326 cmdline_parser_release (args_info);
331gengetopt_strdup (
const char *s)
337 result = (
char*)malloc(strlen(s) + 1);
338 if (result == (
char*)0)
355 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
372 result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0);
380 int result = EXIT_SUCCESS;
382 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
383 result = EXIT_FAILURE;
389cmdline_parser_required2 (
struct gengetopt_args_info *args_info,
const char *prog_name,
const char *additional_error)
391 int error_occurred = 0;
392 FIX_UNUSED (additional_error);
397 fprintf (stderr,
"%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error :
""));
403 fprintf (stderr,
"%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error :
""));
409 fprintf (stderr,
"%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error :
""));
415 fprintf (stderr,
"%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error :
""));
421 fprintf (stderr,
"%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error :
""));
428 return error_occurred;
486#ifndef required_argument
487#define required_argument 1
490#ifndef optional_argument
491#define optional_argument 2
494struct custom_getopt_data {
536static char *custom_optarg;
553static int custom_optind = 1;
559static int custom_opterr = 1;
565static int custom_optopt =
'?';
575static void exchange(
char **argv,
struct custom_getopt_data *d)
577 int bottom = d->first_nonopt;
578 int middle = d->last_nonopt;
579 int top = d->custom_optind;
588 while (top > middle && middle > bottom) {
589 if (top - middle > middle - bottom) {
591 int len = middle - bottom;
595 for (i = 0; i < len; i++) {
596 tem = argv[bottom + i];
598 argv[top - (middle - bottom) + i];
599 argv[top - (middle - bottom) + i] = tem;
605 int len = top - middle;
609 for (i = 0; i < len; i++) {
610 tem = argv[bottom + i];
611 argv[bottom + i] = argv[middle + i];
612 argv[middle + i] = tem;
619 d->first_nonopt += (d->custom_optind - d->last_nonopt);
620 d->last_nonopt = d->custom_optind;
624static void custom_getopt_initialize(
struct custom_getopt_data *d)
631 d->first_nonopt = d->last_nonopt = d->custom_optind;
636#define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
639static int shuffle_argv(
int argc,
char *
const *argv,
const struct option *longopts,
640 struct custom_getopt_data *d)
646 if (d->last_nonopt > d->custom_optind)
647 d->last_nonopt = d->custom_optind;
648 if (d->first_nonopt > d->custom_optind)
649 d->first_nonopt = d->custom_optind;
654 if (d->first_nonopt != d->last_nonopt &&
655 d->last_nonopt != d->custom_optind)
656 exchange((
char **) argv, d);
657 else if (d->last_nonopt != d->custom_optind)
658 d->first_nonopt = d->custom_optind;
663 while (d->custom_optind < argc && NONOPTION_P)
665 d->last_nonopt = d->custom_optind;
671 if (d->custom_optind != argc && !strcmp(argv[d->custom_optind],
"--")) {
673 if (d->first_nonopt != d->last_nonopt
674 && d->last_nonopt != d->custom_optind)
675 exchange((
char **) argv, d);
676 else if (d->first_nonopt == d->last_nonopt)
677 d->first_nonopt = d->custom_optind;
678 d->last_nonopt = argc;
679 d->custom_optind = argc;
685 if (d->custom_optind == argc) {
690 if (d->first_nonopt != d->last_nonopt)
691 d->custom_optind = d->first_nonopt;
699 d->custom_optarg = argv[d->custom_optind++];
706 d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] ==
'-'));
720static int check_long_opt(
int argc,
char *
const *argv,
const char *optstring,
721 const struct option *longopts,
int *longind,
722 int print_errors,
struct custom_getopt_data *d)
725 const struct option *p;
726 const struct option *pfound = NULL;
732 for (nameend = d->nextchar; *nameend && *nameend !=
'='; nameend++)
736 for (p = longopts, option_index = 0; p->name; p++, option_index++)
737 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
738 if ((
unsigned int) (nameend - d->nextchar)
739 == (
unsigned int) strlen(p->name)) {
742 indfound = option_index;
745 }
else if (pfound == NULL) {
748 indfound = option_index;
749 }
else if (pfound->has_arg != p->has_arg
750 || pfound->flag != p->flag
751 || pfound->val != p->val)
755 if (ambig && !exact) {
758 "%s: option `%s' is ambiguous\n",
759 argv[0], argv[d->custom_optind]);
761 d->nextchar += strlen(d->nextchar);
763 d->custom_optopt = 0;
767 option_index = indfound;
770 if (pfound->has_arg != no_argument)
771 d->custom_optarg = nameend + 1;
774 if (argv[d->custom_optind - 1][1] ==
'-') {
776 fprintf(stderr,
"%s: option `--%s' doesn't allow an argument\n",
777 argv[0], pfound->name);
780 fprintf(stderr,
"%s: option `%c%s' doesn't allow an argument\n",
781 argv[0], argv[d->custom_optind - 1][0], pfound->name);
785 d->nextchar += strlen(d->nextchar);
786 d->custom_optopt = pfound->val;
789 }
else if (pfound->has_arg == required_argument) {
790 if (d->custom_optind < argc)
791 d->custom_optarg = argv[d->custom_optind++];
795 "%s: option `%s' requires an argument\n",
797 argv[d->custom_optind - 1]);
799 d->nextchar += strlen(d->nextchar);
800 d->custom_optopt = pfound->val;
801 return optstring[0] ==
':' ?
':' :
'?';
804 d->nextchar += strlen(d->nextchar);
806 *longind = option_index;
808 *(pfound->flag) = pfound->val;
819 if (argv[d->custom_optind][1] ==
'-') {
822 "%s: unrecognized option `--%s'\n",
823 argv[0], d->nextchar);
827 "%s: unrecognized option `%c%s'\n",
828 argv[0], argv[d->custom_optind][0],
832 d->nextchar = (
char *)
"";
834 d->custom_optopt = 0;
838static int check_short_opt(
int argc,
char *
const *argv,
const char *optstring,
839 int print_errors,
struct custom_getopt_data *d)
841 char c = *d->nextchar++;
842 const char *temp = strchr(optstring, c);
845 if (*d->nextchar ==
'\0')
847 if (!temp || c ==
':') {
849 fprintf(stderr,
"%s: invalid option -- %c\n", argv[0], c);
851 d->custom_optopt = c;
854 if (temp[1] ==
':') {
855 if (temp[2] ==
':') {
857 if (*d->nextchar !=
'\0') {
858 d->custom_optarg = d->nextchar;
861 d->custom_optarg = NULL;
865 if (*d->nextchar !=
'\0') {
866 d->custom_optarg = d->nextchar;
873 }
else if (d->custom_optind == argc) {
876 "%s: option requires an argument -- %c\n",
879 d->custom_optopt = c;
880 if (optstring[0] ==
':')
890 d->custom_optarg = argv[d->custom_optind++];
967static int getopt_internal_r(
int argc,
char *
const *argv,
const char *optstring,
968 const struct option *longopts,
int *longind,
969 struct custom_getopt_data *d)
971 int ret, print_errors = d->custom_opterr;
973 if (optstring[0] ==
':')
977 d->custom_optarg = NULL;
983 if (d->custom_optind == 0 || !d->initialized) {
984 if (d->custom_optind == 0)
985 d->custom_optind = 1;
986 custom_getopt_initialize(d);
988 if (d->nextchar == NULL || *d->nextchar ==
'\0') {
989 ret = shuffle_argv(argc, argv, longopts, d);
993 if (longopts && (argv[d->custom_optind][1] ==
'-' ))
994 return check_long_opt(argc, argv, optstring, longopts,
995 longind, print_errors, d);
996 return check_short_opt(argc, argv, optstring, print_errors, d);
999static int custom_getopt_internal(
int argc,
char *
const *argv,
const char *optstring,
1000 const struct option *longopts,
int *longind)
1004 static struct custom_getopt_data d;
1006 d.custom_optind = custom_optind;
1007 d.custom_opterr = custom_opterr;
1008 result = getopt_internal_r(argc, argv, optstring, longopts,
1010 custom_optind = d.custom_optind;
1011 custom_optarg = d.custom_optarg;
1012 custom_optopt = d.custom_optopt;
1016static int custom_getopt_long (
int argc,
char *
const *argv,
const char *options,
1017 const struct option *long_options,
int *opt_index)
1019 return custom_getopt_internal(argc, argv, options, long_options,
1024static char *package_name = 0;
1045int update_arg(
void *field,
char **orig_field,
1046 unsigned int *field_given,
unsigned int *prev_given,
1047 char *value,
const char *possible_values[],
1048 const char *default_value,
1049 cmdline_parser_arg_type arg_type,
1050 int check_ambiguity,
int override,
1051 int no_free,
int multiple_option,
1052 const char *long_opt,
char short_opt,
1053 const char *additional_error)
1055 char *stop_char = 0;
1056 const char *val = value;
1058 char **string_field;
1064 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1066 if (short_opt !=
'-')
1067 fprintf (stderr,
"%s: `--%s' (`-%c') option given more than once%s\n",
1068 package_name, long_opt, short_opt,
1069 (additional_error ? additional_error :
""));
1071 fprintf (stderr,
"%s: `--%s' option given more than once%s\n",
1072 package_name, long_opt,
1073 (additional_error ? additional_error :
""));
1077 if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
1079 if (short_opt !=
'-')
1080 fprintf (stderr,
"%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n",
1081 package_name, (found == -2) ?
"ambiguous" :
"invalid", value, long_opt, short_opt,
1082 (additional_error ? additional_error :
""));
1084 fprintf (stderr,
"%s: %s argument, \"%s\", for option `--%s'%s\n",
1085 package_name, (found == -2) ?
"ambiguous" :
"invalid", value, long_opt,
1086 (additional_error ? additional_error :
""));
1090 if (field_given && *field_given && !
override)
1096 if (possible_values)
1097 val = possible_values[found];
1101 if (val) *((
int *)field) = strtol (val, &stop_char, 0);
1104 if (val) *((
double *)field) = strtod (val, &stop_char);
1108 string_field = (
char **)field;
1109 if (!no_free && *string_field)
1110 free (*string_field);
1111 *string_field = gengetopt_strdup (val);
1122 if (val && !(stop_char && *stop_char ==
'\0')) {
1123 fprintf(stderr,
"%s: invalid numeric value: %s\n", package_name, val);
1136 if (value && orig_field) {
1138 *orig_field = value;
1142 *orig_field = gengetopt_strdup (value);
1152cmdline_parser_internal (
1158 int error_occurred = 0;
1164 int check_ambiguity;
1171 package_name = argv[0];
1190 int option_index = 0;
1192 static struct option long_options[] = {
1193 {
"help", 0, NULL,
'h' },
1194 {
"version", 0, NULL,
'V' },
1195 {
"output", 1, NULL,
'o' },
1196 {
"density", 1, NULL, 0 },
1197 {
"nx", 1, NULL, 0 },
1198 {
"ny", 1, NULL, 0 },
1199 {
"nz", 1, NULL, 0 },
1200 {
"lattice", 1, NULL,
'l' },
1204 custom_optarg = optarg;
1205 custom_optind = optind;
1206 custom_opterr = opterr;
1207 custom_optopt = optopt;
1209 c = custom_getopt_long (argc, argv,
"hVo:l:", long_options, &option_index);
1211 optarg = custom_optarg;
1212 optind = custom_optind;
1213 opterr = custom_opterr;
1214 optopt = custom_optopt;
1221 cmdline_parser_print_help ();
1222 cmdline_parser_free (&local_args_info);
1223 exit (EXIT_SUCCESS);
1226 cmdline_parser_print_version ();
1227 cmdline_parser_free (&local_args_info);
1228 exit (EXIT_SUCCESS);
1233 if (update_arg( (
void *)&(args_info->
output_arg),
1235 &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
1236 check_ambiguity,
override, 0, 0,
1245 if (update_arg( (
void *)&(args_info->
lattice_arg),
1247 &(local_args_info.lattice_given), optarg, cmdline_parser_lattice_values, 0, ARG_STRING,
1248 check_ambiguity,
override, 0, 0,
1257 if (strcmp (long_options[option_index].name,
"density") == 0)
1261 if (update_arg( (
void *)&(args_info->
density_arg),
1263 &(local_args_info.density_given), optarg, 0, 0, ARG_DOUBLE,
1264 check_ambiguity,
override, 0, 0,
1271 else if (strcmp (long_options[option_index].name,
"nx") == 0)
1275 if (update_arg( (
void *)&(args_info->
nx_arg),
1277 &(local_args_info.nx_given), optarg, 0, 0, ARG_INT,
1278 check_ambiguity,
override, 0, 0,
1285 else if (strcmp (long_options[option_index].name,
"ny") == 0)
1289 if (update_arg( (
void *)&(args_info->
ny_arg),
1291 &(local_args_info.ny_given), optarg, 0, 0, ARG_INT,
1292 check_ambiguity,
override, 0, 0,
1299 else if (strcmp (long_options[option_index].name,
"nz") == 0)
1303 if (update_arg( (
void *)&(args_info->
nz_arg),
1305 &(local_args_info.nz_given), optarg, 0, 0, ARG_INT,
1306 check_ambiguity,
override, 0, 0,
1319 fprintf (stderr,
"%s: option unknown: %c%s\n",
CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error :
""));
1328 error_occurred += cmdline_parser_required2 (args_info, argv[0], additional_error);
1331 cmdline_parser_release (&local_args_info);
1333 if ( error_occurred )
1334 return (EXIT_FAILURE);
1339 int found_prog_name = 0;
1345 args_info->
inputs_num = argc - optind - found_prog_name;
1347 (
char **)(malloc ((args_info->
inputs_num)*
sizeof(
char *))) ;
1348 while (optind < argc)
1349 args_info->
inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1356 cmdline_parser_release (&local_args_info);
1357 return (EXIT_FAILURE);
#define CMDLINE_PARSER_VERSION
the program version
#define CMDLINE_PARSER_PACKAGE_NAME
the complete program name (used for help and version)
#define CMDLINE_PARSER_PACKAGE
the program name (used for printing errors)
The header file for the command line option parser generated by GNU Gengetopt version 2....
The additional parameters to pass to parser functions.
int print_errors
whether getopt_long should print an error message for a bad option (default 1)
int check_required
whether to check that all required options were provided (default 1)
int check_ambiguity
whether to check for options already specified in the option structure gengetopt_args_info (default 0...
int initialize
whether to initialize the option structure gengetopt_args_info (default 1)
int override
whether to override possibly already present options (default 0)
Where the command line options are stored.
unsigned int output_given
Whether output was given.
unsigned int density_given
Whether density was given.
char * output_orig
output file name original value given at command line.
const char * output_help
output file name help description.
unsigned int ny_given
Whether ny was given.
char * nx_orig
number of unit cells in x original value given at command line.
const char * ny_help
number of unit cells in y help description.
unsigned inputs_num
unamed options number
unsigned int nx_given
Whether nx was given.
unsigned int nz_given
Whether nz was given.
unsigned int lattice_given
Whether lattice was given.
char * density_orig
density (g/cm^3) original value given at command line.
const char * lattice_help
Lattice Type help description.
const char * help_help
Print help and exit help description.
char * ny_orig
number of unit cells in y original value given at command line.
char * output_arg
output file name.
char ** inputs
unamed options (options without names)
const char * nx_help
number of unit cells in x help description.
int ny_arg
number of unit cells in y.
char * nz_orig
number of unit cells in z original value given at command line.
int nz_arg
number of unit cells in z.
const char * nz_help
number of unit cells in z help description.
double density_arg
density (g/cm^3).
unsigned int help_given
Whether help was given.
int nx_arg
number of unit cells in x.
char * lattice_orig
Lattice Type original value given at command line.
unsigned int version_given
Whether version was given.
const char * version_help
Print version and exit help description.
char * lattice_arg
Lattice Type.
const char * density_help
density (g/cm^3) help description.
int cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
Save the contents of the option struct into an already open FILE stream.
int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
The command line parser.
int cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Save the contents of the option struct into a (text) file.
int cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
The command line parser (version with additional parameters - deprecated)
int cmdline_parser_required(struct gengetopt_args_info *args_info, const char *prog_name)
Checks that all the required options were specified.
struct cmdline_parser_params * cmdline_parser_params_create(void)
Allocates dynamically a cmdline_parser_params structure and initializes all its fields to their defau...
int cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info, struct cmdline_parser_params *params)
The command line parser (version with additional parameters)
void cmdline_parser_init(struct gengetopt_args_info *args_info)
Initializes the passed gengetopt_args_info structure's fields (also set default values for options th...
void cmdline_parser_free(struct gengetopt_args_info *args_info)
Deallocates the string fields of the gengetopt_args_info structure (but does not deallocate the struc...
void cmdline_parser_params_init(struct cmdline_parser_params *params)
Initializes all the fields a cmdline_parser_params structure to their default values.