AwaitedOne
AwaitedOne

Reputation: 1012

Using command line arguments in CPP other than main function

I am trying to write a function to accept command line arguments. On Google search i got so many hits for that, but all use command line arguments with main function, something like below.

#include <iostream>

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

If i already have a main function and I want to write another function, later callable in main or any other program, how to write that, i.e function with user defined name.

Upvotes: 1

Views: 3172

Answers (4)

Sam Daniel
Sam Daniel

Reputation: 1902

Boost provides utilities to handle arguments effectively..

So, as other answers pointed, you can move the entire function body presented in this answer to you new function and pass the "argc" and "argv" from main() to your new function.

int
main(int argc, char *argv[])
{
   namespace po = boost::program_options;

   po::options_description desc("Usage: describe usage here [OPTIONS]");

   desc.add_options()
      ("help", "Print usage")
      ("list,l", po::value<std::string>(), "dummy command taking a value")
      ;

   po::variables_map vm;

   try
   {
      po::store(po::parse_command_line(argc, argv, desc), vm);

      if (vm.count("help"))
      {
         std::cout << desc << std::endl;
         return 0;
      }

      if (vm.count("list"))
      {
         std::cout << "Dummy command entered" << std::endl;
         return 0;
      }

      po::notify(vm);
   }
   catch (po::error& e)
   {
      std::cerr << "Error: " << e.what() << std::endl;
      std::cerr << desc << std::endl;
      return 1;
   }

}

Compilation & Sample OP:

g++ -std=c++11 -Iboost_root/boost -Lboost_root/boost/stage/lib/ Options.cpp -l boost_program_options


./a.out --list dummy_value
Dummy command entered

Upvotes: 0

Aivars
Aivars

Reputation: 93

You can pass arguments to function:

#include <iostream>

int my_function(int argc, char** argv) {
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

int main(int argc, char** argv) {
    my_function(argc, argv);
}

Upvotes: 1

CherryDT
CherryDT

Reputation: 29012

You can either pass them to the function you call, like this:

void printArguments(int argc, char** argv) {
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

int main(int argc, char** argv) {
    printArguments(argc, argv);
}

...or store them in global variables, like this:

int mainArgc;
char** mainArgv;

void printArguments() {
    std::cout << "Have " << mainArgc << " arguments:" << std::endl;
    for (int i = 0; i < mainArgc; ++i) {
        std::cout << mainArgv[i] << std::endl;
    }
}

int main(int argc, char** argv) {
    mainArgc = argc;
    mainArgv = argv;
    printArguments();
}

Upvotes: 1

tobspr
tobspr

Reputation: 8376

If you want the arguments to be available, you can just forward them to your method. For example:

void my_method(int argc, char** argv) {
   cout << "Num args: " << argc << endl;
}

int main(int argc, char** argv) {
    my_method(argc, argv);
}

Upvotes: 2

Related Questions