user2773084
user2773084

Reputation: 107

C++ Accepting a command-line argument with parameters

I've got a program that needs to accept multiple command line arguments. I've gotten to a stage where I need to set it up to accept argument n, which specifies the max and min lengths of the string that will eventually be printed. Basically input could look like this:

-a -n7,7 -i // with -a and -i being other arguments

I'm fine with picking out arguments on their own, but I'm not sure how to pluck out those max and min values too. I've had a go (see below), but whenever I try and use variables minimum and maximum, I just get a run time error. Cheers guys.

int c;
while ((c = getopt(argc, argv, ":wpsaevin")) != -1) {
    switch (c) {
        case 'w': // pattern matches whole word
            mode = WHOLE;
            break;
        case 'p': // pattern matches prefix
            mode = PREFIX;
            break;
        case 'a': // pattern matches anywhere
            mode = ANYWHERE;
            break;
        case 's': // pattern matches suffix
            mode = SUFFIX;
            break;
        case 'e': // pattern matches anywhere
            mode = EMBEDDED;
            break;
        case 'v': // reverse sense of match
            reverse_match = true;
            break;
        case 'i': // ignore case of pattern
            ignore_case = true;
            break;
        case 'n': //Specifies word length
            length_spec = true;
            cin >> minimum >> maximum;
            if (minimum == 0 && maximum == 0) { //no word limit
                length_spec = false;
            } else if (maximum == 0) {
                maximum = 100;
            }
            break;
    }
}
argc -= optind;
argv += optind;

Upvotes: 3

Views: 482

Answers (2)

user1508519
user1508519

Reputation:

From this page:

This variable is set by getopt to point at the value of the option argument, for those options that accept arguments.

case 'n': //Specifies word length
            length_spec = true;
            char *cvalue = optarg;
            // TODO: Split cvalue by delimiter
            // to obtain minimum and maximum
            if (minimum == 0 && maximum == 0) { //no word limit
                length_spec = false;
            } else if (maximum == 0) {
                maximum = 100;
            }
            break;

And an example of splitting a string:

#include <iostream>
#include <string>
#include <algorithm>

int
main()
{
  const char* test = "1000,2000";
  std::string str = std::string(test);
  auto find = std::find(str.begin(), str.end(), ',');
  std::string first = std::string(str.begin(), find);
  std::string second = std::string(find+1,str.end());
  std::cout << first << " " << second;
  // 1000 2000
}

EDIT

Reference link

If you're able to use C++11, consider using std::stoi, like so:

  int first_int = std::stoi( first );
  int second_int = std::stoi ( second );

If not, try this:

  std::replace(str.begin(), str.end(), ',', ' ');
  std::istringstream ss(str);
  ss >> first_int;
  ss >> second_int;
  std::cout << first_int << " " << second_int << std::endl;

I would use atoi as a last resort.

A naive implementation might look like this (use at your own risk):

int convert(std::string s)
{
    int size = s.size();
    int exp = size - 1;
    int result = 0;
    for (int i = 0; i < size; i++)
    {
        char c = s[i];
        result += (int)(c - '0') * std::pow(10, exp--);
    }
    return result;
}

Upvotes: 3

Michael Simbirsky
Michael Simbirsky

Reputation: 3103

You can use Boost Library Program Options, as @aaronman suggested above.

Upvotes: 0

Related Questions