1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #ifndef __ARGS_H__
- #define __ARGS_H__
- #include <stdlib.h>
- #include <argp.h>
- #include <unistd.h>
- #include <string.h>
- #include "io.h"
- const char *argp_program_version = "Cider 0.1";
- const char *argp_program_bug_address = "https://notabug.org/cryptarch/dice/issues";
- /*
- OPTIONS. Field 1 in ARGP.
- Order of fields: {NAME, KEY, ARG, FLAGS, DOC}.
- */
- static struct argp_option options[] = {
- {"help", 'h', NULL, 0, "Print this help message."},
- {"version", 'v', NULL, 0, "Print version information."},
- {0}
- };
- /*
- PARSER. Field 2 in ARGP.
- Order of parameters: KEY, ARG, STATE.
- */
- static error_t parse_opt (int key, char *arg, struct argp_state *state) {
- struct arguments *arguments = state->input;
- switch (key) {
- case 'h':
- {
- argp_state_help(state, stdout, ARGP_HELP_USAGE | ARGP_HELP_LONG | ARGP_HELP_DOC);
- exit(EXIT_SUCCESS);
- }
- break;
- case 'v':
- {
- printf("%s\n", argp_program_version);
- exit(0);
- }
- break;
- case ARGP_KEY_ARG:
- {
- {
- arguments->mode = SCRIPTED;
- if(0 != strcmp(arg, "-")) {
- errno = 0;
- arguments->ist = fopen(arg, "r");
- if(arguments->ist == NULL) {
- fprintf(stderr, "Error %d (%s) opening file %s\n", errno, strerror(errno), arg);
- exit(1);
- }
- }
- }
- }
- break;
- default:
- return ARGP_ERR_UNKNOWN;
- }
- return 0;
- }
- /*
- ARGS_DOC. Field 3 in ARGP.
- A description of the non-option command-line arguments
- that we accept.
- */
- static char args_doc[] = "[file]";
- /*
- DOC. Field 4 in ARGP.
- Program documentation.
- */
- static char doc[] = "Convert IPv4 ranges into CIDR form";
- /*
- The ARGP structure itself.
- */
- static struct argp argp = {options, parse_opt, args_doc, doc};
- #endif // __ARGS_H__
|