From f14f4a8700a8caa285f7412927de8d02d3a831f5 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 23 Jul 2017 20:07:40 -0400 Subject: [PATCH] Added -h and long options --- src/opt-pricer.c | 72 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/src/opt-pricer.c b/src/opt-pricer.c index 9e93fc5..bdfecdf 100644 --- a/src/opt-pricer.c +++ b/src/opt-pricer.c @@ -6,13 +6,70 @@ #include #include #include +#include + + +struct helptext { + const char *opt; + const char *desc; +}; + + +static struct helptext options[] = { + {"-s, --spot", + "Spot price"}, + {"-k, --strike", + "Strike price"}, + {"-r, --rfr", + "Risk-free rate"}, + {"-v, --implied-volatility", + "Implied volatility"}, + {"-d, --effective-date", + "Effective date [YYYY-MM-DD]"}, + {"-e, --expiry-date", + "Expiry date [YYYY-MM-DD]"}, + {"-N", + "Number of MC simulations"}, + {"-c, --call", + "Call flag"}, + {"-p, --put", + "Put or call flag"}, + {"-h", + "This help text"}, + { NULL , NULL } +}; + + +static struct option long_options[] = { + {"spot", required_argument, 0, 's'}, + {"strike", required_argument, 0, 'k'}, + {"rfr", required_argument, 0, 'r'}, + {"implied-volatility", required_argument, 0, 'v'}, + {"effective-date", required_argument, 0, 'd'}, + {"expiry-date", required_argument, 0, 'e'}, + {"call", no_argument, 0, 'c'}, + {"put", no_argument, 0, 'p'}, + {0, 0, 0, 0} +}; + + +int print_help(void) +{ + int i; + printf("Usage: opt-pricer [options...]\n"); + for(i=0; options[i].opt; i++) { + printf(" %-25s %s\n", options[i].opt, options[i].desc); + } + return 0; +} + int main(int argc, char *argv[]) { double spot = 0, strike = 0, rfr = 0, vol = 0, sims = 1000; double bs_price, mc_price; char expiry_date[11], buffer[512]; - int opt, type = 0; + int opt, option_index = 0, type = 0; struct tm expiry, value; extern char *optarg; @@ -21,9 +78,9 @@ int main(int argc, char *argv[]) memset(&expiry, 0, sizeof(expiry)); memset(&value, 0, sizeof(value)); - while ((opt = getopt(argc, argv, "s:k:r:v:d:e:n:cp")) != -1) { + while ((opt = getopt_long(argc, argv, "s:k:r:v:d:e:N:cp", long_options, &option_index)) != -1) { switch (opt) { - case 's': /* spot */ + case 's': if (sscanf(optarg, "%lf", &spot) == EOF) { return 1; }; @@ -49,7 +106,7 @@ int main(int argc, char *argv[]) case 'd': /* valuation date, must be YYYY-MM-DD */ strptime(optarg, "%Y-%m-%d", &value); break; - case 'n': /* number of simulations */ + case 'N': /* number of simulations */ if (sscanf(optarg, "%lf", &sims) == EOF) { return 1; }; @@ -60,8 +117,15 @@ int main(int argc, char *argv[]) case 'p': /* set as put */ type = -1; break; + case 'h': /* print help*/ + print_help(); + return 0; } } + if (spot == 0 || strike == 0 || rfr == 0 || vol == 0) { + print_help(); + return 1; + } strftime(expiry_date, 11, "%Y-%m-%d", &expiry); bs_price = bsm(spot, rfr, vol, strike, expiry, value, type);