std::shared_ptr CommandLineOptionsParser::parseCommandLine()

in syncd/CommandLineOptionsParser.cpp [13:157]


std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine(
        _In_ int argc,
        _In_ char **argv)
{
    SWSS_LOG_ENTER();

    auto options = std::make_shared<CommandLineOptions>();

#ifdef SAITHRIFT
    const char* const optstring = "dp:t:g:x:b:B:w:uSUCsz:lrm:h";
#else
    const char* const optstring = "dp:t:g:x:b:B:w:uSUCsz:lh";
#endif // SAITHRIFT

    while (true)
    {
        static struct option long_options[] =
        {
            { "diag",                    no_argument,       0, 'd' },
            { "profile",                 required_argument, 0, 'p' },
            { "startType",               required_argument, 0, 't' },
            { "useTempView",             no_argument,       0, 'u' },
            { "disableExitSleep",        no_argument,       0, 'S' },
            { "enableUnittests",         no_argument,       0, 'U' },
            { "enableConsistencyCheck",  no_argument,       0, 'C' },
            { "syncMode",                no_argument,       0, 's' },
            { "redisCommunicationMode",  required_argument, 0, 'z' },
            { "enableSaiBulkSupport",    no_argument,       0, 'l' },
            { "globalContext",           required_argument, 0, 'g' },
            { "contextContig",           required_argument, 0, 'x' },
            { "breakConfig",             required_argument, 0, 'b' },
            { "watchdogWarnTimeSpan",    optional_argument, 0, 'w' },
            { "supportingBulkCounters",  required_argument, 0, 'B' },
#ifdef SAITHRIFT
            { "rpcserver",               no_argument,       0, 'r' },
            { "portmap",                 required_argument, 0, 'm' },
#endif // SAITHRIFT
            { "help",                    no_argument,       0, 'h' },
            { 0,                         0,                 0,  0  }
        };

        int option_index = 0;

        int c = getopt_long(argc, argv, optstring, long_options, &option_index);

        if (c == -1)
        {
            break;
        }

        switch (c)
        {
            case 'd':
                options->m_enableDiagShell = true;
                break;

            case 'p':
                options->m_profileMapFile = std::string(optarg);
                break;

            case 't':
                options->m_startType = CommandLineOptions::startTypeStringToStartType(optarg);

                if (options->m_startType == SAI_START_TYPE_UNKNOWN)
                {
                    SWSS_LOG_ERROR("unknown start type '%s'", optarg);
                    exit(EXIT_FAILURE);
                }
                break;

            case 'u':
                options->m_enableTempView = true;
                break;

            case 'S':
                options->m_disableExitSleep = true;
                break;

            case 'U':
                options->m_enableUnittests = true;
                break;

            case 'C':
                options->m_enableConsistencyCheck = true;
                break;

            case 's':
                SWSS_LOG_WARN("param -s is depreacated, use -z");
                options->m_enableSyncMode = true;
                break;

            case 'z':
                sai_deserialize_redis_communication_mode(optarg, options->m_redisCommunicationMode);
                break;

            case 'l':
                options->m_enableSaiBulkSupport = true;
                break;

            case 'g':
                options->m_globalContext = (uint32_t)std::stoul(optarg);
                break;

            case 'x':
                options->m_contextConfig = std::string(optarg);
                break;

            case 'b':
                options->m_breakConfig = std::string(optarg);
                break;

            case 'w':
                options->m_watchdogWarnTimeSpan = (int64_t)std::stoll(optarg);
                break;

#ifdef SAITHRIFT
            case 'r':
                options->m_runRPCServer = true;
                break;
            case 'm':
                options->m_portMapFile = std::string(optarg);
                break;
#endif // SAITHRIFT

            case 'B':
                options->m_supportingBulkCounterGroups = std::string(optarg);
                break;

            case 'h':
                printUsage();
                exit(EXIT_SUCCESS);

            case '?':
                SWSS_LOG_WARN("unknown option %c", optopt);
                printUsage();
                exit(EXIT_FAILURE);

            default:
                SWSS_LOG_ERROR("getopt_long failure");
                exit(EXIT_FAILURE);
        }
    }

    return options;
}