[boost-users] [program options]
HI how would I use program options to accept multiple input. Example I have a diffieHellmanKeyExchange program running through command prompt. // begin main program #include <iostream> #include <boost/tuple/tuple_io.hpp> #include <boost/program_options.hpp> #include "cppdef/BitOperation.cpp" #include "cppdef/mathfunc.cpp" #include "cppdef/IsPrime.cpp" #include "cppdef/DiffieHellman.cpp" #include <BigInteger.hpp> #include <BigIntegerUtils.hpp> int main( int ac, char* av[] ) { namespace po = boost::program_options; po::options_description desc("Allowed options"); boost::tuple<BigInteger, BigInteger> key_pair; desc.add_options() ("generate", "generates a pair of key") ("secert", po::value(&key_pair), "creates the share secret key"); po::variables_map vm; po::store(po::parse_command_line(ac, av, desc), vm); po::notify(vm); if (vm.count("generate")) { BigInteger my_prime = encryptcpw::random_prime<BigInteger>(&algocpw::Miller_Rabin<BigInteger>, 10); std::cout << "my prime: " << my_prime << "\n"; boost::tuple<BigInteger, BigInteger> pare(encryptcpw::dh_init<BigInteger>( my_prime )); std::cout << "pair of generated number: " << pare << "\n"; } if (vm.count("secert")) { const BigInteger p( encryptcpw::random_prime<BigInteger>(&algocpw::Miller_Rabin<BigInteger>, 10)); std::cout << encryptcpw::dh_secret<BigInteger>( p, boost::tuples::get<0>(key_pair), boost::tuples::get<1>(key_pair)); } } // end program The thing compiles but when I do DiffieHellman.exe --secert 817 697 I get this error. C:\CPW\cs_classes>DiffieHellmanEncrypt --secert 817 697 terminate called after throwing an instance of 'boost::program_options::invalid_ option_value' what(): in option 'secert': invalid option value '817' This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
Hi! On 10/31/07, chun ping wang <cablepuff@gmail.com> wrote:
HI how would I use program options to accept multiple input.
The thing compiles but when I do DiffieHellman.exe --secert 817 697
I get this error. C:\CPW\cs_classes>DiffieHellmanEncrypt --secert 817 697 terminate called after throwing an instance of 'boost::program_options::invalid_ option_value' what(): in option 'secert': invalid option value '817'
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
the option is in your case: --secert="817 697" Regards, Ovanes
One more addition: boost::tuple<BigInteger, BigInteger> key_pair; This will not parse your key. You need to do it manually. So your param should be std::string. And than you can tokenize you string. Or you define 2 separate params and let program_options parse them into Integers for you. But I am unsure if this is supported. Probably yes. I am 100% sure program options will not tokenize one param for you. May be only if your key_pair supports construction from a space separted string. Regards, Ovanes On 10/31/07, Ovanes Markarian <om_boost@keywallet.com> wrote:
Hi!
On 10/31/07, chun ping wang <cablepuff@gmail.com> wrote:
HI how would I use program options to accept multiple input.
The thing compiles but when I do DiffieHellman.exe --secert 817 697
I get this error. C:\CPW\cs_classes>DiffieHellmanEncrypt --secert 817 697 terminate called after throwing an instance of 'boost::program_options::invalid_ option_value' what(): in option 'secert': invalid option value '817'
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
the option is in your case:
--secert="817 697"
Regards, Ovanes
You can use something like this as well. std::vector<std::string> key_pair; ("secert", boost::program_options::value< std::vector<std::string>
(&key_pair), "creates the share secret key");
Then you can do you error checks ... if ( key_pair.size() == 2 ) { // Do your processing here } else { // error } This example uses string vector. I guess std::vector<BigInteger> should also work for you. Thanks, Edwin On 10/31/07, Ovanes Markarian <om_boost@keywallet.com> wrote:
One more addition:
boost::tuple<BigInteger, BigInteger> key_pair;
This will not parse your key. You need to do it manually. So your param should be std::string. And than you can tokenize you string. Or you define 2 separate params and let program_options parse them into Integers for you. But I am unsure if this is supported. Probably yes. I am 100% sure program options will not tokenize one param for you. May be only if your key_pair supports construction from a space separted string.
Regards, Ovanes
On 10/31/07, Ovanes Markarian <om_boost@keywallet.com> wrote:
Hi!
On 10/31/07, chun ping wang <cablepuff@gmail.com> wrote:
HI how would I use program options to accept multiple input.
The thing compiles but when I do DiffieHellman.exe --secert 817 697
I get this error. C:\CPW\cs_classes>DiffieHellmanEncrypt --secert 817 697 terminate called after throwing an instance of 'boost::program_options::invalid_ option_value' what(): in option 'secert': invalid option value '817'
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
the option is in your case:
--secert="817 697"
Regards, Ovanes
The problem is that your user still has to enter the keys like: appname.exe --key="public key" --key="secret key" And if the user enters: appname.exe --key="secret key" --key="public key" How do you know which is the right one? The best solution would be to enforce to specify public and secret params: appname.exe public="public key" secret="secret key" The last point also applies to my previous post. This way you will avoid some errors for your users. Best Regards, Ovanes On 11/2/07, Edwin Savarimuthu <zerobook@gmail.com> wrote:
You can use something like this as well.
std::vector<std::string> key_pair; ("secert", boost::program_options::value< std::vector<std::string>
(&key_pair), "creates the share secret key");
Then you can do you error checks ...
if ( key_pair.size() == 2 ) { // Do your processing here } else { // error }
This example uses string vector. I guess std::vector<BigInteger> should also work for you.
Thanks, Edwin
One more addition:
boost::tuple<BigInteger, BigInteger> key_pair;
This will not parse your key. You need to do it manually. So your param should be std::string. And than you can tokenize you string. Or you define 2 separate params and let program_options parse them into Integers for you. But I am unsure if this is supported. Probably yes. I am 100% sure
On 10/31/07, Ovanes Markarian <om_boost@keywallet.com> wrote: program
options will not tokenize one param for you. May be only if your key_pair supports construction from a space separted string.
Regards, Ovanes
On 10/31/07, Ovanes Markarian <om_boost@keywallet.com> wrote:
Hi!
On 10/31/07, chun ping wang <cablepuff@gmail.com> wrote:
HI how would I use program options to accept multiple input.
The thing compiles but when I do DiffieHellman.exe --secert 817 697
I get this error. C:\CPW\cs_classes>DiffieHellmanEncrypt --secert 817 697 terminate called after throwing an instance of 'boost::program_options::invalid_ option_value' what(): in option 'secert': invalid option value '817'
This application has requested the Runtime to terminate it in an
unusual
way. Please contact the application's support team for more information.
the option is in your case:
--secert="817 697"
Regards, Ovanes
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
chun ping wang
-
Edwin Savarimuthu
-
Ovanes Markarian