ISCSI-INQ: convert to getopt_long instead of popt

This commit is contained in:
Ronnie Sahlberg
2013-04-21 09:53:12 -07:00
parent c0d1c122c3
commit c3886cf9c4

View File

@@ -26,7 +26,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <popt.h>
#include <getopt.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
@@ -219,40 +219,52 @@ void print_help(void)
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
}
int main(int argc, const char *argv[])
int main(int argc, char *argv[])
{
poptContext pc;
struct iscsi_context *iscsi;
const char **extra_argv;
int extra_argc = 0;
const char *url = NULL;
struct iscsi_url *iscsi_url = NULL;
int evpd = 0, pagecode = 0;
int show_help = 0, show_usage = 0, debug = 0;
int res;
int c;
struct poptOption popt_options[] = {
{ "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL },
{ "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL },
{ "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" },
{ "evpd", 'e', POPT_ARG_INT, &evpd, 0, "evpd", "integer" },
{ "pagecode", 'c', POPT_ARG_INT, &pagecode, 0, "page code", "integer" },
{ "debug", 'd', POPT_ARG_INT, &debug, 0, "Debugging level", "integer" },
POPT_TABLEEND
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"usage", no_argument, NULL, 'u'},
{"debug", no_argument, NULL, 'd'},
{"initiator_name", required_argument, NULL, 'i'},
{"evpd", required_argument, NULL, 'e'},
{"pagecode", required_argument, NULL, 'c'},
{0, 0, 0, 0}
};
int option_index;
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_POSIXMEHARDER);
if ((res = poptGetNextOpt(pc)) < -1) {
fprintf(stderr, "Failed to parse option : %s %s\n",
poptBadOption(pc, 0), poptStrerror(res));
exit(10);
}
extra_argv = poptGetArgs(pc);
if (extra_argv) {
url = strdup(*extra_argv);
extra_argv++;
while (extra_argv[extra_argc]) {
extra_argc++;
while ((c = getopt_long(argc, argv, "h?udi:e:c:", long_options,
&option_index)) != -1) {
switch (c) {
case 'h':
case '?':
show_help = 1;
break;
case 'u':
show_usage = 1;
break;
case 'd':
debug = 1;
break;
case 'i':
initiator = optarg;
break;
case 'e':
evpd = atoi(optarg);
break;
case 'c':
pagecode = atoi(optarg);
break;
default:
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
print_help();
exit(0);
}
}
@@ -266,8 +278,6 @@ int main(int argc, const char *argv[])
exit(0);
}
poptFreeContext(pc);
iscsi = iscsi_create_context(initiator);
if (iscsi == NULL) {
fprintf(stderr, "Failed to create context\n");
@@ -279,6 +289,7 @@ int main(int argc, const char *argv[])
iscsi_set_log_fn(iscsi, iscsi_log_to_stderr);
}
url = strdup(argv[optind]);
if (url == NULL) {
fprintf(stderr, "You must specify the URL\n");
print_usage();