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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
/*
* correct - finds which parameter/s is/are most similar to the key parameter.
*
* Copyright (C) 2012 Chrisname.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROGRAM_NAME "correct"
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_PATCH 0
/*
* PASTE HERE.
*/
/** Shows the usage message */
static void show_usage(const char* argv0)
{
printf("correct - finds which parameter/s is/are most similar to the key parameter.\n"
"Usage: %s [options] <key> <value 1> [value 2 [value ... [value n ]]]\n"
"Options:\n"
"\t-h,--help\tShow this help message\n"
"\t-v,--version\tShow version information\n"
"\t-n,--number\tSet the number of parameters to print. 0 = all. Default: 1\n"
"\t-r,--reverse\tPrint output in reverse\n"
"\t-d,--detail\tPrint more detailed information\n", argv0);
}
/** Shows the version message */
static void show_version()
{
printf(PROGRAM_NAME " version %d.%d%d\n", VERSION_MAJOR, VERSION_MINOR,
VERSION_PATCH);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
show_usage(argv[0]);
exit(EXIT_FAILURE);
}
int reverse = 0, detail = 0;
long n = 1;
for (;;) {
static const char* optstring = "hvn:rd";
static struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "number", 1, NULL, 'n' },
{ "reverse", 0, NULL, 'r' },
{ "detail", 1, NULL, 'd' }
};
int c = getopt_long(argc, argv, optstring, longopts, NULL);
if (c < 0)
break;
switch (c) {
case 'h':
show_usage(argv[0]);
break;
case 'v':
show_version();
break;
case 'n':
n = strtol(optarg, NULL, 10);
break;
case 'r':
reverse = 1;
break;
case 'd':
detail = 1;
break;
default:
break;
}
}
if ((argc - optind) < 2) {
show_usage(argv[0]);
exit(EXIT_FAILURE);
}
correct(argv[optind], &argv[optind + 1], argc - optind - 1, n, reverse, detail);
return 0;
}
|