Nick Schappler
Nick Schappler

Reputation: 33

C++ Command Line Argument Identification

I need to be able to tell if the final argument in my command line is surrounded in double quotes or not. If it's in double quotes, I treat it as a string. If it's not, I need to treat it as a file to open and obtain the string. Argv by default will grab the double quoted string and strip the quotes, so I can't figure out a way to handle this problem.

pseudocode is something like this...

if(argv[argc-1] was called with surrounding double quotes){
    //handle as string (I already have code to do this)
}
else{
    //handle as filename (I already have code to do this)
}

Upvotes: 1

Views: 617

Answers (1)

Mike Steinert
Mike Steinert

Reputation: 814

All of the parameters in argv are strings. You are probably better off rethinking your strategy. Try opening the argument, if that fails then treat it as a string.

Alternatively you could escape the quotes on the command line and they will be passed to your application:

$ program "\"this is a string\""

Edit: The sample code assumes you are using a Bash shell or something similar

Upvotes: 2

Related Questions