From bc16db8ce1e990dbfadcf6c08f68bd4cb2c445a5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 26 Oct 2009 18:54:09 -0400 Subject: Add some command-line options to `dimension' program. --- dimension/main.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'dimension/main.c') diff --git a/dimension/main.c b/dimension/main.c index 46cb798..8f5aa9f 100644 --- a/dimension/main.c +++ b/dimension/main.c @@ -19,8 +19,71 @@ #include "../libdimension/dimension.h" #include +#include + +const char *output = NULL, *input = NULL; +int tokenize = 0; int -main() { +main(int argc, char **argv) { + /* Parse the command-line options */ + + static struct option long_options[] = { + { "output", required_argument, NULL, 'o' }, + { "input", required_argument, NULL, 'i' }, + { "tokenize", no_argument, &tokenize, 1 }, + { 0, 0, 0, 0 } + }; + int opt, opt_index; + + while (1) { + opt = getopt_long(argc, argv, "o:i:", long_options, &opt_index); + + if (opt == -1) + break; + + switch (opt) { + case 0: + /* Option set a flag - do nothing here */ + break; + + case 'o': + if (output) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "--output specified more than once."); + } else { + output = optarg; + } + break; + + case 'i': + if (input) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "--input specified more than once."); + } else { + input = optarg; + } + break; + + default: + dmnsn_error(DMNSN_SEVERITY_HIGH, "Error parsing command line."); + }; + } + + if (optind == argc - 1) { + if (input) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Multiple input files specified."); + } else { + input = argv[optind]; + } + } else if (optind < argc) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid extranious command line options."); + } + + if (!output) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "No output file specified."); + } + if (!input) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "No input file specified."); + } + return EXIT_SUCCESS; } -- cgit v1.2.3