Reputation: 3393
I am trying to use Boost program_options in order to parse my program inputs. In general the docs provide the necessary info for parsing. However, I would like to make my program print the usage instructions when no inputs are provided and I can't seem to figure it out. There does not seem to be a "default" options nor can I find how to count the number of inputs provided (to test).
This is my code at the moment:
boost::program_options::options_description help("Usage");
help.add_options()
("help", "print help info");
boost::program_options::options_description req("Required inputs");
req.add_options()
("root", boost::program_options::value<std::string>(&images_root), "Root directory")
boost::program_options::options_description opt("Option inputs");
opt.add_options()
("verbose", boost::program_options::value<bool>(&verbose)->implicit_value(1), "Verbose");
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, all), vm);
if (vm.count("help"))
{
std::cout << help;
return 1;
}
boost::program_options::notify(vm);
How can I produce the following (i.e. if no inputs do std::cout << help
)?
./test-file
>> print help info
Upvotes: 2
Views: 1399
Reputation: 2061
You could do it with argc
for example:
int main(int argc, char** argv)
{
if( argc <= 1 )
std::cout << "Print Usage\n";
return 0;
}
Upvotes: 1