|
| 1 | +#include "CommandLineOptionsParser.h" |
| 2 | + |
| 3 | +#include <getopt.h> |
| 4 | + |
| 5 | +std::shared_ptr<CommandLineOptions> CommandLineOptionsParser::parseCommandLine( |
| 6 | + _In_ int argc, |
| 7 | + _In_ char **argv) |
| 8 | +{ |
| 9 | + SWSS_LOG_ENTER(); |
| 10 | + |
| 11 | + auto options = std::make_shared<CommandLineOptions>(); |
| 12 | + |
| 13 | +#ifdef SAITHRIFT |
| 14 | + const char* const optstring = "dp:t:uSUCsrm:h"; |
| 15 | +#else |
| 16 | + const char* const optstring = "dp:t:uSUCsh"; |
| 17 | +#endif // SAITHRIFT |
| 18 | + |
| 19 | + while(true) |
| 20 | + { |
| 21 | + static struct option long_options[] = |
| 22 | + { |
| 23 | + { "diag", no_argument, 0, 'd' }, |
| 24 | + { "profile", required_argument, 0, 'p' }, |
| 25 | + { "startType", required_argument, 0, 't' }, |
| 26 | + { "useTempView", no_argument, 0, 'u' }, |
| 27 | + { "disableExitSleep", no_argument, 0, 'S' }, |
| 28 | + { "enableUnittests", no_argument, 0, 'U' }, |
| 29 | + { "enableConsistencyCheck", no_argument, 0, 'C' }, |
| 30 | + { "syncMode", no_argument, 0, 's' }, |
| 31 | +#ifdef SAITHRIFT |
| 32 | + { "rpcserver", no_argument, 0, 'r' }, |
| 33 | + { "portmap", required_argument, 0, 'm' }, |
| 34 | +#endif // SAITHRIFT |
| 35 | + { "help", no_argument, 0, 'h' }, |
| 36 | + { 0, 0, 0, 0 } |
| 37 | + }; |
| 38 | + |
| 39 | + int option_index = 0; |
| 40 | + |
| 41 | + int c = getopt_long(argc, argv, optstring, long_options, &option_index); |
| 42 | + |
| 43 | + if (c == -1) |
| 44 | + { |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + switch (c) |
| 49 | + { |
| 50 | + case 'd': |
| 51 | + options->m_enableDiagShell = true; |
| 52 | + break; |
| 53 | + |
| 54 | + case 'p': |
| 55 | + options->m_profileMapFile = std::string(optarg); |
| 56 | + break; |
| 57 | + |
| 58 | + case 't': |
| 59 | + options->m_startType = CommandLineOptions::startTypeStringToStartType(optarg); |
| 60 | + |
| 61 | + if (options->m_startType == SAI_START_TYPE_UNKNOWN) |
| 62 | + { |
| 63 | + SWSS_LOG_ERROR("unknown start type '%s'", optarg); |
| 64 | + exit(EXIT_FAILURE); |
| 65 | + } |
| 66 | + break; |
| 67 | + |
| 68 | + case 'u': |
| 69 | + options->m_enableTempView = true; |
| 70 | + break; |
| 71 | + |
| 72 | + case 'S': |
| 73 | + options->m_disableExitSleep = true; |
| 74 | + break; |
| 75 | + |
| 76 | + case 'U': |
| 77 | + options->m_enableUnittests = true; |
| 78 | + break; |
| 79 | + |
| 80 | + case 'C': |
| 81 | + options->m_enableConsistencyCheck = true; |
| 82 | + break; |
| 83 | + |
| 84 | + case 's': |
| 85 | + options->m_enableSyncMode = true; |
| 86 | + break; |
| 87 | + |
| 88 | +#ifdef SAITHRIFT |
| 89 | + case 'r': |
| 90 | + options->m_runRPCServer = true; |
| 91 | + break; |
| 92 | + case 'm': |
| 93 | + options->m_portMapFile = std::string(optarg); |
| 94 | + break; |
| 95 | +#endif // SAITHRIFT |
| 96 | + |
| 97 | + case 'h': |
| 98 | + printUsage(); |
| 99 | + exit(EXIT_SUCCESS); |
| 100 | + |
| 101 | + case '?': |
| 102 | + SWSS_LOG_WARN("unknown option %c", optopt); |
| 103 | + printUsage(); |
| 104 | + exit(EXIT_FAILURE); |
| 105 | + |
| 106 | + default: |
| 107 | + SWSS_LOG_ERROR("getopt_long failure"); |
| 108 | + exit(EXIT_FAILURE); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return options; |
| 113 | +} |
| 114 | + |
| 115 | +void CommandLineOptionsParser::printUsage() |
| 116 | +{ |
| 117 | + SWSS_LOG_ENTER(); |
| 118 | + |
| 119 | +#ifdef SAITHRIFT |
| 120 | + std::cout << "Usage: syncd [-d] [-p profile] [-t type] [-u] [-S] [-U] [-C] [-s] [-r] [-m portmap] [-h]" << std::endl; |
| 121 | +#else |
| 122 | + std::cout << "Usage: syncd [-d] [-p profile] [-t type] [-u] [-S] [-U] [-C] [-s] [-h]" << std::endl; |
| 123 | +#endif // SAITHRIFT |
| 124 | + |
| 125 | + std::cout << " -d --diag" << std::endl; |
| 126 | + std::cout << " Enable diagnostic shell" << std::endl; |
| 127 | + std::cout << " -p --profile profile" << std::endl; |
| 128 | + std::cout << " Provide profile map file" << std::endl; |
| 129 | + std::cout << " -t --startType type" << std::endl; |
| 130 | + std::cout << " Specify start type (cold|warm|fast|fastfast) " << std::endl; |
| 131 | + std::cout << " -u --useTempView" << std::endl; |
| 132 | + std::cout << " Use temporary view between init and apply" << std::endl; |
| 133 | + std::cout << " -S --disableExitSleep" << std::endl; |
| 134 | + std::cout << " Disable sleep when syncd crashes" << std::endl; |
| 135 | + std::cout << " -U --enableUnittests" << std::endl; |
| 136 | + std::cout << " Metadata enable unittests" << std::endl; |
| 137 | + std::cout << " -C --enableConsistencyCheck" << std::endl; |
| 138 | + std::cout << " Enable consisteny check DB vs ASIC after comparison logic" << std::endl; |
| 139 | + std::cout << " -s --syncMode" << std::endl; |
| 140 | + std::cout << " Enable synchronous mode" << std::endl; |
| 141 | + |
| 142 | +#ifdef SAITHRIFT |
| 143 | + |
| 144 | + std::cout << " -r --rpcserver" << std::endl; |
| 145 | + std::cout << " Enable rpcserver" << std::endl; |
| 146 | + std::cout << " -m --portmap portmap" << std::endl; |
| 147 | + std::cout << " Specify port map file" << std::endl; |
| 148 | + |
| 149 | +#endif // SAITHRIFT |
| 150 | + |
| 151 | + std::cout << " -h --help" << std::endl; |
| 152 | + std::cout << " Print out this message" << std::endl; |
| 153 | +} |
| 154 | + |
0 commit comments