1

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.

AwaitedOne
  • 992
  • 3
  • 19
  • 42
  • 2
    If you want to write a method, but you don't even understand how to pass arguments ... maybe, you should spent a few more hours educating yourself about the absolut beginner basics of your favorite language. This is not meant as insult, but you really should get some idea of the most important concepts before doing the next step. – GhostCat May 03 '16 at 10:59
  • 1
    Possible duplicate of [C ++ process argc argv outside of main()](http://stackoverflow.com/questions/12862798/c-process-argc-argv-outside-of-main) – gdlmx May 03 '16 at 11:06

4 Answers4

2

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);
}
tobspr
  • 8,200
  • 5
  • 33
  • 46
  • How to use them in header file. `extern int argc` and `extern char **argv` and definition `int argc`, and `char **argv` in `cpp` file and calling in `main.cpp` like `int main(int argc, char** argv)` . Is it right.?? – AwaitedOne May 03 '16 at 15:21
1

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();
}
CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • How to use them in header file. `extern int argc` and `extern char **argv `and definition `int argc`, and `char **argv `in cpp file and calling in `main.cpp` like `int main(int argc, char** argv)` . Is it right.?? – AwaitedOne May 03 '16 at 16:04
1

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);
}
Aivars
  • 93
  • 7
0

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
Sam Daniel
  • 1,800
  • 12
  • 22