/** * Constructor. * @param of Whether or not this command requires an argument * @param ot The type of option (string, number, any, etc.) * @param shOpt The one character command line option. Set to 0 * if unused. * @param loOpt The long command option. Set to std::string() * if unused. * @param desc A string describing what this option does. * @param req Set to true if this is a required option. * @param optVectorList Use this to create your own * command option list if you want to use an alternate method * of parsing the command options. */ CommandOption(const CommandOptionFlag of, const CommandOptionType ot, const char shOpt, const std::string& loOpt, const std::string& desc, const bool req = false, CommandOptionVec& optVectorList = defaultCommandOptionList) : optFlag(of), optType(ot), shortOpt(shOpt), longOpt(loOpt), description(desc), required(req), count(0), maxCount(0), order(0) {optVectorList.push_back(this);}
CommandOption::CommandOption( const CommandOption::CommandOptionFlag of, const CommandOption::CommandOptionType ot, const char shOpt, const std::string& loOpt, const std::string& desc, const bool req, CommandOptionVec& optVectorList) : optFlag(of), optType(ot), shortOpt(shOpt), longOpt(loOpt), description(desc), required(req), count(0), maxCount(0), order(0) { if (ot == CommandOption::stdType) { if ( (shOpt == 0) && (loOpt.size() == 0) ) { InvalidParameter exc("A short or long command option must be specified"); GPSTK_THROW(exc); } // if short option is specified, allow only printable, non-space characters if ( (shOpt != 0) && !isgraph(shOpt) ) { InvalidParameter exc("Invalid short command option character"); GPSTK_THROW(exc); } // if long option is specified, allow only printable, non-space characters for ( size_t i = longOpt.size(); i > 0; --i ) { if ( !isgraph(longOpt[i - 1]) ) { InvalidParameter exc("Invalid long command option character"); GPSTK_THROW(exc); } } } optVectorList.push_back(this); }
int CommandOption_T::testCommandOption() { TestUtil tester( "CommandOption", "Initialization", __FILE__, __LINE__ ); CommandOptionVec testCmdOptVec; int expectedCount = 0; try // No arg, trailing, no flags { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::trailingType, 0, "", "", false, testCmdOptVec); tester.assert( true, "CommandOption was created successfully.", __LINE__ ); ++expectedCount; tester.assert( (cmdOpt.getArgString().compare("ARG") == 0), "CommandOption getArgString() should return ARG.", __LINE__ ); tester.assert( (cmdOpt.getCount() == 0), "CommandOption count should be 0.", __LINE__ ); tester.assert( (cmdOpt.getValue().size() == 0), "CommandOption value size should be 0.", __LINE__ ); tester.assert( (cmdOpt.getOrder() == 0), "CommandOption order should be 0.", __LINE__ ); tester.assert( (cmdOpt.checkArguments().size() == 0), "CommandOption checkArguments() should return nothing.", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ ); } try // No arg, standard, no flags { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType, 0, "", "", false, testCmdOptVec); ++expectedCount; tester.assert( false, "CommandOption creation should have failed due to missing short and long options.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // No arg, standard, short flag (valid) { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType, 'f', "", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (cmdOpt.getOptionString().compare("-f") == 0), "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '-f'", __LINE__ ); tester.assert( (cmdOpt.getFullOptionString().compare(" -f") == 0), "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected ' -f'", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // No arg, standard, short flag (bogus) { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType, ' ', "", "", false, testCmdOptVec); ++expectedCount; tester.assert( false, "CommandOption creation should have failed due to invalid short option.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // No arg, standard, long flag (valid) { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType, 0, "foo", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (cmdOpt.getOptionString().compare("--foo") == 0), "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '--foo'", __LINE__ ); tester.assert( (cmdOpt.getFullOptionString().compare(" --foo") == 0), "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected ' --foo'", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // No arg, standard, long flag (bogus) { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType, 0, "foo bar", "", false, testCmdOptVec); ++expectedCount; tester.assert( false, "CommandOption creation should have failed due to invalid long option.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // No arg, standard, both flags (valid) { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::stdType, 'f', "foo", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (cmdOpt.getOptionString().compare("-f | --foo") == 0), "CommandOption getOptionString() returned '" + cmdOpt.getOptionString() + "', expected '-f | --foo'", __LINE__ ); tester.assert( (cmdOpt.getFullOptionString().compare(" -f, --foo") == 0), "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected ' -f, --foo'", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ ); } try // No arg, meta, no flags { CommandOption cmdOpt(CommandOption::noArgument, CommandOption::metaType, 0, "", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ ); } try // Arg, trailing, no flags { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::trailingType, 0, "", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ ); } try // Arg, standard, no flags { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 0, "", "", false, testCmdOptVec); ++expectedCount; tester.assert( false, "CommandOption creation should have failed due to missing short and long options.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // Arg, standard, short flag (valid) { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 'f', "", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (cmdOpt.getOptionString().compare("-f") == 0), "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '-f'", __LINE__ ); tester.assert( (cmdOpt.getFullOptionString().compare(" -f ARG") == 0), "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected ' -f ARG'", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // Arg, standard, short flag (bogus) { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType, ' ', "", "", false, testCmdOptVec); ++expectedCount; tester.assert( false, "CommandOption creation should have failed due to invalid short option.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // Arg, standard, long flag (valid) { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 0, "foo", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (cmdOpt.getOptionString().compare("--foo") == 0), "CommandOption getOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected '--foo'", __LINE__ ); tester.assert( (cmdOpt.getFullOptionString().compare(" --foo=ARG") == 0), "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected ' --foo=ARG'", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // Arg, standard, long flag (bogus) { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 0, "foo bar", "", false, testCmdOptVec); ++expectedCount; tester.assert( false, "CommandOption creation should have failed due to invalid long option.", __LINE__ ); } catch ( ... ) { tester.assert( true, "CommandOption() threw an exception as expected.", __LINE__ ); } try // Arg, standard, both flags (valid) { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::stdType, 'f', "foo", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (cmdOpt.getOptionString().compare("-f | --foo") == 0), "CommandOption getOptionString() returned '" + cmdOpt.getOptionString() + "', expected '-f | --foo'", __LINE__ ); tester.assert( (cmdOpt.getFullOptionString().compare(" -f, --foo=ARG") == 0), "CommandOption getFullOptionString() returned '" + cmdOpt.getFullOptionString() + "', expected ' -f, --foo=ARG'", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ ); } // @todo - Test getDescription() try // Arg, meta, no flags { CommandOption cmdOpt(CommandOption::hasArgument, CommandOption::metaType, 0, "", "", false, testCmdOptVec); ++expectedCount; tester.assert( true, "CommandOption was created successfully.", __LINE__ ); tester.assert( (testCmdOptVec.size() == expectedCount), "CommandOption was not added to the supplied vector.", __LINE__ ); } catch ( ... ) { tester.assert( false, "CommandOption() threw an exception but should not have.", __LINE__ ); } return tester.countFails(); }