Browse Source

getopt: minor improvements.

legacy
Sam Hocevar 8 years ago
parent
commit
afada09f64
3 changed files with 25 additions and 24 deletions
  1. +1
    -1
      src/lol/sys/getopt.h
  2. +15
    -14
      src/sys/getopt.cpp
  3. +9
    -9
      tools/lolremez/lolremez.cpp

+ 1
- 1
src/lol/sys/getopt.h View File

@@ -27,7 +27,7 @@ public:
getopt(int argc, char * const * _argv);
~getopt();

void add_arg(int short_opt, char const *long_opt, bool has_arg);
void add_opt(int short_opt, char const *long_opt, bool has_arg);
int parse();

int index;


+ 15
- 14
src/sys/getopt.cpp View File

@@ -66,9 +66,9 @@ getopt::~getopt()
{
}

void getopt::add_arg(int short_opt, char const *long_opt, bool has_arg)
void getopt::add_opt(int short_opt, char const *long_opt, bool has_arg)
{
getopt_private::optdesc o = { long_opt, has_arg, nullptr, short_opt };
getopt_private::optdesc o { long_opt, has_arg, nullptr, short_opt };
m_private->m_opts.push(o);

if (isalnum(short_opt))
@@ -119,23 +119,25 @@ int getopt::parse()
if (!tmp || ret == ':')
return '?';

this->index++;
++this->index;
if (tmp[1] == ':')
{
if (flag[2] != '\0')
this->arg = flag + 2;
else if (this->index < m_private->m_argc)
this->arg = argv[this->index++];
else
goto too_few;
{
if (this->index >= m_private->m_argc)
goto too_few;
this->arg = argv[this->index++];
}
return ret;
}

if (flag[2] != '\0')
{
flag[1] = '-';
this->index--;
argv[this->index]++;
--this->index;
++argv[this->index];
}

return ret;
@@ -146,7 +148,7 @@ int getopt::parse()
if (flag[2] == '\0')
return -1;

for (int i = 0; m_private->m_opts[i].name; i++)
for (int i = 0; m_private->m_opts[i].name; ++i)
{
size_t l = strlen(m_private->m_opts[i].name);

@@ -159,18 +161,17 @@ int getopt::parse()
if (!m_private->m_opts[i].has_arg)
goto bad_opt;
longindex = i;
this->index++;
++this->index;
this->arg = flag + 2 + l + 1;
return m_private->m_opts[i].val;
case '\0':
longindex = i;
this->index++;
++this->index;
if (m_private->m_opts[i].has_arg)
{
if (this->index < m_private->argc)
this->arg = argv[this->index++];
else
if (this->index >= m_private->argc)
goto too_few;
this->arg = argv[this->index++];
}
return m_private->m_opts[i].val;
default:


+ 9
- 9
tools/lolremez/lolremez.cpp View File

@@ -57,11 +57,11 @@ static void usage()
printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n");
}

static void FAIL(char const *message)
static void FAIL(char const *message = nullptr)
{
printf("Error: %s\n", message);
printf("\n");
usage();
if (message)
printf("Error: %s\n", message);
printf("Try 'lolremez --help' for more information.\n");
exit(EXIT_FAILURE);
}

@@ -73,10 +73,10 @@ int main(int argc, char **argv)
int degree = 4;

lol::getopt opt(argc, argv);
opt.add_arg('d', "degree", true);
opt.add_arg('r', "range", true);
opt.add_arg('h', "help", false);
opt.add_arg('v', "version", false);
opt.add_opt('h', "help", false);
opt.add_opt('v', "version", false);
opt.add_opt('d', "degree", true);
opt.add_opt('r', "range", true);

for (;;)
{
@@ -103,7 +103,7 @@ int main(int argc, char **argv)
version();
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
FAIL();
}
}



Loading…
Cancel
Save