Exemple #1
0
    // Constructor
    Input(int argc, char **argv)
      : ec_(false), shortestOnly_(false), distances_(false), suppressRef_(false),
        overlaps_(true), delim_("|"), refFile_(""), nonRefFile_(""), chr_("all") {

      typedef Ext::UserError UE;
      if ( 1 == argc )
        throw(NoInput());
      int argcntr = 1;
      bool outoption = false; // may choose up to one type of non-default output option

      // [Process-Flags]
      std::string next = "";
      while ( argcntr < argc ) {
        next = argv[argcntr];
        if ( next == "--help" )
          throw(HelpException());
        else if ( next == "--version" )
          throw(VersionException());
        else if ( next == "--ec" || next == "--header" )
          ec_ = true;
        else if ( next == "--no-overlaps" )
          overlaps_ = false;
        else if ( next == "--delim" ) {
          Ext::Assert<UE>(++argcntr < argc, "No value given for --delim.");
          delim_ = argv[argcntr];
        }
        else if ( next == "--chrom" ) {
          Ext::Assert<UE>(++argcntr < argc, "No value given for --chrome.");
          chr_ = argv[argcntr];
        }
        else if ( next == "--closest" ) {
          Ext::Assert<UE>(!outoption, "Multiple output options not allowed.");
          shortestOnly_ = true;
          outoption = true;
        }
        else if ( next == "--dist" )
          distances_ = true;
        else if ( next == "--no-ref" )
          suppressRef_ = true;
        else if ( next == "--help" )
          throw(HelpException());
        else if ( next == "--shortest" ) { // silently supported for bckwd compatibility
          Ext::Assert<UE>(!outoption, "Multiple output options not allowed.");
          shortestOnly_ = true;
          outoption = true;
        } else { // the rest are two input files?
          Ext::Assert<UE>(argcntr + 2 == argc, "Unknown option: " + next + ".");
          break;
        }
        ++argcntr;
      } // while

      Ext::Assert<UE>(argcntr + 2 == argc, "Not enough input files given.");
      refFile_ = argv[argcntr++];
      nonRefFile_ = argv[argcntr];
      Ext::Assert<UE>(refFile_.find("--") != 0, "Option given where file expected: " + refFile_ + ".");
      Ext::Assert<UE>(nonRefFile_.find("--") != 0, "Option given where file expected: " + nonRefFile_ + ".");
    }
void CommandLineParser::parseArgs(const std::vector<StringView>& args, CommandLineFlags& result) const
{
	size_t numPositionalConsumed = 0;
	size_t numFlagConsumed = 0;

	while (numFlagConsumed < args.size())
	{
		auto flag = args[numFlagConsumed];
		if (!isOption(flag))	// Positional arguments
		{
			if (numPositionalConsumed >= posFlagMap.size())
				throw ParseException("too many positional arguments");

			auto inserted = result.addFlag(posFlagMap[numPositionalConsumed].first, flag);
			assert(inserted && "Insertion failed");
			++numPositionalConsumed;
		}
		else	// Optional arguments
		{
			flag.remove_prefix(1);

			// See if flag is "-help"
			if (flag == "help")
				throw HelpException();

			auto itr = optFlagMap.find(flag);
			if (itr == optFlagMap.end())
				throw ParseException("Option not recognized: " + flag.to_string());

			if (itr->second.defaultValue)
			{
				++numFlagConsumed;
				if (numFlagConsumed >= args.size())
					throw ParseException("More argument needs to be provided after option " + flag.to_string());
				auto flagArg = args[numFlagConsumed];
				if (isOption(flagArg))
					throw ParseException("Found another option instead of an argument after option " + flag.to_string());

				auto inserted = result.addFlag(flag, flagArg);
				assert(inserted && "Insertion failed");
			}
			else
			{
				auto inserted = result.addFlag(flag, "");
				assert(inserted && "Insertion failed");
			}
		}
		++numFlagConsumed;
	}

	if (numPositionalConsumed < posFlagMap.size())
		throw ParseException("Not enough positional arguments are provided");

	for (auto const& mapping: optFlagMap)
	{
		if (mapping.second.defaultValue && result.lookup(mapping.first) == nullptr)
			result.addFlag(mapping.first, *mapping.second.defaultValue);
	}
}
/**
 * Supported options are '-h', '-v'.
 * @throws HelpException when only help is need
 * @throws LogicException when used option is unknown
 */
    void
OptionAgent::parseDashOpt(const std::string &arg,
        const OptionParams &params)
{
    if ("-h" == arg || "--help" == arg) {
        throw HelpException(ExInfo(getHelpInfo(params)));
    }
    else if ("-v" == arg || "--version" == arg) {
        throw HelpException(ExInfo(getVersionInfo()));
    }
    else if ("-c" == arg || "--config" == arg) {
        throw HelpException(ExInfo(params.getConfig(m_environ)));
    }
    else {
        throw LogicException(ExInfo("unknown option")
                .addInfo("arg", arg)
                .addInfo("use",
                    getParam("program") + " --help"));
    }
}