Boost program option parsing issue.
Hi All,
I am using program option for my program. Here is my code snippet
string inputFile, outputFile;
/* Populate command line arguments */
po::options_description desc("Usage: KDMGenerator [OPTIONS]");
{
desc.add_options()
("help", "prints this message")
( "input-file", po::value<string>(&inputFile), "input file
name" )
( "output-file", po::value<string>(&outputFile), "output file
name");
}
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help") || !vm.size())
{
cout <
Hi,
why would you need to run the program with --input-file without a value?
If you want to set the inputFile variable to a default value if it is not
specified
on the command line simply assign it where you declare the variable.
Alternatively
you can assign the variable like this:
if (vm.count("input-file")) {
...
} else {
inputFile = "whatever";
}
Best regards,
Tarcisio Fedrizzi
On Mon, Aug 19, 2013 at 6:46 PM, Vivek Anand
Hi All,
I am using program option for my program. Here is my code snippet
string inputFile, outputFile;
/* Populate command line arguments */ po::options_description desc("Usage: KDMGenerator [OPTIONS]"); { desc.add_options() ("help", "prints this message") ( "input-file", po::value<string>(&inputFile), "input file name" ) ( "output-file", po::value<string>(&outputFile), "output file name"); }
po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm);
if (vm.count("help") || !vm.size()) { cout <
if(vm.count("input-file")) { cout << "Input file :" << inputFile << '\n'; }
if(vm.count("output-file")) { cout << outputFile << '\n'; }
The issue I am facing is that if i I call my program without passing value for input file, something like
./myapp --input-file --output-file out.txt
I get Input file: --output-file
i.e the value "--output-file" get stored in inputFile and vm.count("output-file") returns zero.
Is this the desired behavior? Is there any workaround for this?
-- Vivek,
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Thu, Aug 22, 2013 at 5:58 PM, Tarcisio Fedrizzi
Hi, why would you need to run the program with --input-file without a value?
A third-party client will be using my program through a script or through exec(). I do not have control over that code. Currently, my validation check throws error that output-file not specified. Actually its the input file which is missing.
If you want to set the inputFile variable to a default value if it is not specified on the command line simply assign it where you declare the variable. Alternatively you can assign the variable like this:
if (vm.count("input-file")) { ... } else { inputFile = "whatever"; }
Best regards, Tarcisio Fedrizzi
On Mon, Aug 19, 2013 at 6:46 PM, Vivek Anand
wrote: Hi All,
I am using program option for my program. Here is my code snippet
string inputFile, outputFile;
/* Populate command line arguments */ po::options_description desc("Usage: KDMGenerator [OPTIONS]"); { desc.add_options() ("help", "prints this message") ( "input-file", po::value<string>(&inputFile), "input file name" ) ( "output-file", po::value<string>(&outputFile), "output file name"); }
po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm);
if (vm.count("help") || !vm.size()) { cout <
if(vm.count("input-file")) { cout << "Input file :" << inputFile << '\n'; }
if(vm.count("output-file")) { cout << outputFile << '\n'; }
The issue I am facing is that if i I call my program without passing value for input file, something like
./myapp --input-file --output-file out.txt
I get Input file: --output-file
i.e the value "--output-file" get stored in inputFile and vm.count("output-file") returns zero.
Is this the desired behavior? Is there any workaround for this?
-- Vivek,
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Warm regards , Vivek, (¨`·.·´¨) Always `·.¸(¨`·.·´¨) Keep Smiling (¨`·.·´¨)¸.·´ N Be Happy!! `·.¸.·´
Hi,
this does the trick:
( "input-file", po::value<string>(&inputFile)->implicit_value(""), "input
file name" )
note the implcit_value call added.
In this way the parse works as you need.
Best regards,
Tarcisio Fedrizzi
On Thu, Aug 22, 2013 at 3:58 PM, Vivek Anand
On Thu, Aug 22, 2013 at 5:58 PM, Tarcisio Fedrizzi
wrote: Hi, why would you need to run the program with --input-file without a value?
A third-party client will be using my program through a script or through exec(). I do not have control over that code.
Currently, my validation check throws error that output-file not specified. Actually its the input file which is missing.
If you want to set the inputFile variable to a default value if it is
not specified
on the command line simply assign it where you declare the variable. Alternatively you can assign the variable like this:
if (vm.count("input-file")) { ... } else { inputFile = "whatever"; }
Best regards, Tarcisio Fedrizzi
On Mon, Aug 19, 2013 at 6:46 PM, Vivek Anand
wrote: Hi All,
I am using program option for my program. Here is my code snippet
string inputFile, outputFile;
/* Populate command line arguments */ po::options_description desc("Usage: KDMGenerator [OPTIONS]"); { desc.add_options() ("help", "prints this message") ( "input-file", po::value<string>(&inputFile), "input file
name" )
( "output-file", po::value<string>(&outputFile), "output
file name");
}
po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm);
if (vm.count("help") || !vm.size()) { cout <
if(vm.count("input-file")) { cout << "Input file :" << inputFile << '\n'; }
if(vm.count("output-file")) { cout << outputFile << '\n'; }
The issue I am facing is that if i I call my program without passing
value for input file, something like
./myapp --input-file --output-file out.txt
I get Input file: --output-file
i.e the value "--output-file" get stored in inputFile and
vm.count("output-file") returns zero.
Is this the desired behavior? Is there any workaround for this?
-- Vivek,
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Warm regards , Vivek, (¨`·.·´¨) Always `·.¸(¨`·.·´¨) Keep Smiling (¨`·.·´¨)¸.·´ N Be Happy!! `·.¸.·´ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thanks Tarcisio, This worked for me.
On Thu, Aug 22, 2013 at 9:06 PM, Tarcisio Fedrizzi
Hi, this does the trick:
( "input-file", po::value<string>(&inputFile)->implicit_value(""), "input file name" )
note the implcit_value call added. In this way the parse works as you need.
Best regards, Tarcisio Fedrizzi
On Thu, Aug 22, 2013 at 3:58 PM, Vivek Anand
wrote: On Thu, Aug 22, 2013 at 5:58 PM, Tarcisio Fedrizzi
wrote: Hi, why would you need to run the program with --input-file without a value?
A third-party client will be using my program through a script or through exec(). I do not have control over that code.
Currently, my validation check throws error that output-file not specified. Actually its the input file which is missing.
If you want to set the inputFile variable to a default value if it is not specified on the command line simply assign it where you declare the variable. Alternatively you can assign the variable like this:
if (vm.count("input-file")) { ... } else { inputFile = "whatever"; }
Best regards, Tarcisio Fedrizzi
On Mon, Aug 19, 2013 at 6:46 PM, Vivek Anand
wrote: Hi All,
I am using program option for my program. Here is my code snippet
string inputFile, outputFile;
/* Populate command line arguments */ po::options_description desc("Usage: KDMGenerator [OPTIONS]"); { desc.add_options() ("help", "prints this message") ( "input-file", po::value<string>(&inputFile), "input file name" ) ( "output-file", po::value<string>(&outputFile), "output file name"); }
po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm);
if (vm.count("help") || !vm.size()) { cout <
if(vm.count("input-file")) { cout << "Input file :" << inputFile << '\n'; }
if(vm.count("output-file")) { cout << outputFile << '\n'; }
The issue I am facing is that if i I call my program without passing value for input file, something like
./myapp --input-file --output-file out.txt
I get Input file: --output-file
i.e the value "--output-file" get stored in inputFile and vm.count("output-file") returns zero.
Is this the desired behavior? Is there any workaround for this?
-- Vivek,
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Warm regards , Vivek, (¨`·.·´¨) Always `·.¸(¨`·.·´¨) Keep Smiling (¨`·.·´¨)¸.·´ N Be Happy!! `·.¸.·´ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Warm regards , Vivek, (¨`·.·´¨) Always `·.¸(¨`·.·´¨) Keep Smiling (¨`·.·´¨)¸.·´ N Be Happy!! `·.¸.·´
participants (2)
-
Tarcisio Fedrizzi
-
Vivek Anand