-1

If I wanted to validate if the user has entered a specific parameter to the program how would I do it? This is my current code:

#include <iostream>
#include <cstdlib>

int main (int argc, char* argv[]) {

    if (argv[1] == "--para") {
        std::cout << "Some text here..." << std::endl;
    }

}

The code above will not work because I need some way to convert the parameter to a string so I can validate it in my if statement.

What I do not know is how to check if the --para exists at all when using multiple parameters into the program.

Example:

./main parameter1 parameter2 --para
Kdoel
  • 11
  • 2
  • Seriously, it _really_ helps to learn something about C before diving in and trying to do something. Otherwise you are in for a world of pain. – dandan78 May 01 '16 at 18:14
  • 1
    @dandan78: Well, it _really_ helps if you could elaborate. Maybe it could guide me in the right direction? – Kdoel May 01 '16 at 18:19
  • Well, I see questions of this sort question depressingly often. What it tells me is that there are people want to program in C or C++ and but don't even bother to read a tutorial or two on the subject beforehand. On Stackoverflow, the assumption is that you've done your homework first before asking your question, which you clearly have not. That being the case, you're on your own. – dandan78 May 01 '16 at 18:31
  • See [Boost.Program_options](http://www.boost.org/doc/libs/1_60_0/doc/html/program_options.html) – Thomas May 01 '16 at 18:50

4 Answers4

3

You can construct and use a std::vector of std::strings directly:

#include <iostream>
#include <string>
#include <vector>

int main(int argc,char* argv[])
{
    std::vector<std::string> params{argv, argv+argc};

    for (auto i : params)
        if (i == "--para") {
            std::cout << "Some text here..." << std::endl;
        }

    return 0;
}

If you want to know if at least one of the parameters was a match you could use a standard library algorithm to check for you:

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

int main(int argc,char* argv[])
{
    std::vector<std::string> params{argv, argv+argc};
    const std::string para{"--para"};

    if (std::any_of(params.begin(),params.end(),[&para](std::string val) {return val==para; }))
            std::cout << "Some text here..." << std::endl;

    return 0;
}
wally
  • 10,717
  • 5
  • 39
  • 72
1

You need to do the check in a loop:

for( int i = 1; i < argc; i++ ) {
    if (strcmp(argv[i], "--para") == 0) {
        std::cout << "Some text here..." << std::endl;
    }
}

Instead of using strcmp you can convert the character pointer to a string:

for( int i = 1; i < argc; i++ ) {
    std::string str(argv[i]); 
    if (str ==  "--para") {
        std::cout << "Some text here..." << std::endl;
    }
}
Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
0

you can do it in pure c using strcmp. For example to check first parameter:

if(argc > 1 && strcmp(argv[1], "--para") == 0) {
// do some stuff here
}

You can just loop over other arguments and apply the same logic to check others

Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
0

This is a solved problem. There are lots of libraries out there. I like argp, but it's gnu specific (I think). It's also C, not C++, but can be made to work. Boost has a utility for this also. I'm sure there are dozens of others. Don't reinvent the wheel.

http://optionparser.sourceforge.net/

http://tclap.sourceforge.net/

boost

What parameter parser libraries are there for C++?

Community
  • 1
  • 1
Jfevold
  • 422
  • 2
  • 11
  • 1
    Basically true, but depending on what the OP wants to do, these libraries might be overkill. Using a library can have disadvantages as well. – Frank Puffer May 01 '16 at 18:57
  • @FrankPuffer: Absolutely agree. Flatmouse's answer is the one I upvoted, I think it's best of those here (after modification in response to your comment). I wanted to offer another set of solutions to this problem, and try to give further insight for future searchers, not just the OP. – Jfevold May 01 '16 at 19:07