Add a --version option

This commit is contained in:
pooler 2012-02-09 15:19:32 +01:00
parent 4d2ee802cd
commit b25730c8f7

View file

@ -127,6 +127,9 @@ static struct option_help options_help[] = {
{ "help", { "help",
"(-h) Display this help text" }, "(-h) Display this help text" },
{ "version",
"(-V) Display version information and exit" },
{ "config FILE", { "config FILE",
"(-c FILE) Load a JSON-format configuration file" }, "(-c FILE) Load a JSON-format configuration file" },
@ -201,6 +204,7 @@ static struct option options[] = {
{ "url", 1, NULL, 'o' }, { "url", 1, NULL, 'o' },
{ "user", 1, NULL, 'u' }, { "user", 1, NULL, 'u' },
{ "userpass", 1, NULL, 'O' }, { "userpass", 1, NULL, 'O' },
{ "version", 0, NULL, 'V' },
{ } { }
}; };
@ -688,12 +692,17 @@ out:
return NULL; return NULL;
} }
static void show_usage(void) static void show_version_and_exit(void)
{
printf("%s\n%s\n", PACKAGE_STRING, curl_version());
exit(0);
}
static void show_usage_and_exit(int status)
{ {
int i; int i;
printf("minerd version %s\n\n", VERSION); printf("Usage: minerd [options]\n\nSupported options:\n");
printf("Usage:\tminerd [options]\n\nSupported options:\n");
for (i = 0; i < ARRAY_SIZE(options_help); i++) { for (i = 0; i < ARRAY_SIZE(options_help); i++) {
struct option_help *h; struct option_help *h;
@ -701,7 +710,7 @@ static void show_usage(void)
printf("--%s\n%s\n\n", h->name, h->helptext); printf("--%s\n%s\n\n", h->name, h->helptext);
} }
exit(1); exit(status);
} }
static void parse_arg (int key, char *arg) static void parse_arg (int key, char *arg)
@ -718,7 +727,7 @@ static void parse_arg (int key, char *arg)
} }
} }
if (i == ARRAY_SIZE(algo_names)) if (i == ARRAY_SIZE(algo_names))
show_usage(); show_usage_and_exit(1);
break; break;
case 'c': { case 'c': {
json_error_t err; json_error_t err;
@ -731,7 +740,7 @@ static void parse_arg (int key, char *arg)
#endif #endif
if (!json_is_object(opt_config)) { if (!json_is_object(opt_config)) {
applog(LOG_ERR, "JSON decode of %s failed", arg); applog(LOG_ERR, "JSON decode of %s failed", arg);
show_usage(); exit(1);
} }
break; break;
} }
@ -751,35 +760,35 @@ static void parse_arg (int key, char *arg)
case 'r': case 'r':
v = atoi(arg); v = atoi(arg);
if (v < -1 || v > 9999) /* sanity check */ if (v < -1 || v > 9999) /* sanity check */
show_usage(); show_usage_and_exit(1);
opt_retries = v; opt_retries = v;
break; break;
case 'R': case 'R':
v = atoi(arg); v = atoi(arg);
if (v < 1 || v > 9999) /* sanity check */ if (v < 1 || v > 9999) /* sanity check */
show_usage(); show_usage_and_exit(1);
opt_fail_pause = v; opt_fail_pause = v;
break; break;
case 's': case 's':
v = atoi(arg); v = atoi(arg);
if (v < 1 || v > 9999) /* sanity check */ if (v < 1 || v > 9999) /* sanity check */
show_usage(); show_usage_and_exit(1);
opt_scantime = v; opt_scantime = v;
break; break;
case 'T': case 'T':
v = atoi(arg); v = atoi(arg);
if (v < 1 || v > 99999) /* sanity check */ if (v < 1 || v > 99999) /* sanity check */
show_usage(); show_usage_and_exit(1);
opt_timeout = v; opt_timeout = v;
break; break;
case 't': case 't':
v = atoi(arg); v = atoi(arg);
if (v < 1 || v > 9999) /* sanity check */ if (v < 1 || v > 9999) /* sanity check */
show_usage(); show_usage_and_exit(1);
opt_n_threads = v; opt_n_threads = v;
break; break;
@ -790,14 +799,14 @@ static void parse_arg (int key, char *arg)
case 'o': /* --url */ case 'o': /* --url */
if (strncmp(arg, "http://", 7) && if (strncmp(arg, "http://", 7) &&
strncmp(arg, "https://", 8)) strncmp(arg, "https://", 8))
show_usage(); show_usage_and_exit(1);
free(rpc_url); free(rpc_url);
rpc_url = strdup(arg); rpc_url = strdup(arg);
break; break;
case 'O': /* --userpass */ case 'O': /* --userpass */
if (!strchr(arg, ':')) if (!strchr(arg, ':'))
show_usage(); show_usage_and_exit(1);
free(rpc_userpass); free(rpc_userpass);
rpc_userpass = strdup(arg); rpc_userpass = strdup(arg);
@ -808,8 +817,12 @@ static void parse_arg (int key, char *arg)
case 1004: case 1004:
use_syslog = true; use_syslog = true;
break; break;
case 'V':
show_version_and_exit();
case 'h':
show_usage_and_exit(0);
default: default:
show_usage(); show_usage_and_exit(1);
} }
#if defined(WIN32) || defined(WIN64) #if defined(WIN32) || defined(WIN64)
@ -868,7 +881,7 @@ static void parse_cmdline(int argc, char *argv[])
int key; int key;
while (1) { while (1) {
key = getopt_long(argc, argv, "hc:a:qDPr:s:T:t:o:O:u:p:", options, NULL); key = getopt_long(argc, argv, "hVc:a:qDPr:s:T:t:o:O:u:p:", options, NULL);
if (key < 0) if (key < 0)
break; break;