Antithesis
Antithesis

Reputation: 337

Taking parameters on the command line in C++

I've a program that takes two csv files and a checkin date as inputs and renders a certain output. Thus I'd normally run the executable in this manner,

./my_executable file.csv 2015-10-13

However my requirement is to have the usage behave this way

my_executable --input1 ./file1.csv --input2 ./file2.csv --date 2015-08-01

How can I do this. Do I have write the words input1, input2 and date somewhere in my code. Any help appreciated.

Upvotes: 2

Views: 1626

Answers (4)

sehe
sehe

Reputation: 393114

Simplest way I can think of:

Live On Coliru

#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <cassert>

int main(int argc, char *raw_argv[]) {
    using namespace std;

    vector<string> const args { raw_argv+1, raw_argv+argc };

    assert(args.size() < 1 || args[0] == "--input1");
    assert(args.size() < 3 || args[2] == "--input2");

    if (args.size() > 4) {
        std::string const& csv1 = args[1];
        std::string const& csv2 = args[3];

        std::string date = args.size() > 4? args[4] : "(unspecified)";
        std::cout <<  "Arguments received: " << csv1 << ", " << csv2 << " date:" << date << "\n";
    }
}

Prints e.g.

./test --input1 stuff.csv --input2 other.csv
Arguments received: stuff.csv, other.csv date:(unspecified)

Upvotes: 2

SHR
SHR

Reputation: 8313

Usually when you give argument in that way the order should not matter, so you'll have to be able to parse the arguments in any order.

Here is a possible solution:

struct arguments
{
  std::string input1;
  std::string input2;
  std::string date;
};

bool parse_arguments(int argc, char** argv, arguments& args)
{
  if(argc < 7){ //or set defaults
   //print usage();//implement
   return false;
  }
  for(int i=1; i<argc;i+=2){
    string command = argv[i];
    string argument = argv[i+1];
    if(command == "--input1"){
      args.input1 = argument;
    }
    else if(command == "--input2"){
      args.input2 = argument;  
    }
    else if(command == "--date"){ 
      args.date = argument;
    }
    else{
      std::cerr<<"Unknown argument: " <<command<<std::endl;
      //print usage();
      return false;
    }
  }
  if(args.input1.empty() || args.input2.empty() || args.data.empty())
    return false; 
  return true;
}

int main(int argc, char* argv[]){
   arguments args;
   parse_arguments(argc,argv, args);
   //now you can use the struct.
   ...
}

Upvotes: 0

Kuldeep Dhaka
Kuldeep Dhaka

Reputation: 553

This should give you a kickstart.
https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3

or if you want to handle the arguments manually.
see: https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments

int main(int argc, const char **argv[])
{
  for(int i = 0; i < argc; i++) {
    std::cout << argv[i] << std::endl;
  }
  return 0;
}

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57708

Command line arguments are passed to your program via the argument count and argument list parameters of main:

int main(int argument_count, char * argument_list[]);

The first parameter is the number of arguments, including the name of your executable.

The second argument is an array of C-style strings, one for each argument (or word) on the command line. The first item is usually the name of the program.

You can always write a small program to test this out:

#include <iostream>
int main(int arg_count, char * arg_list[])
{
  for (unsigned int i = 0; i < arg_count; ++arg_count)
  {
    std::cout << "Argument " << i << ": " << arg_list[i] << std::endl;
  }
  return EXIT_SUCCESS;
}

Edit 1:
Your parameters would line up as:
Argument 0: my_executable
Argument 1: --input1
Argument 2: ./file1.csv
Argument 3: --input2
Argument 4: ./file2.csv
//...

If you want to compare these parameters, then yes, you would need to type "input1":

//...
std::string arg1 = arg_list[1];
if (arg1 == "--arg1")
{
  //...
}

Upvotes: 1

Related Questions