| 877 | } |
| 878 | |
| 879 | int ParseCommand(int argc, const char *argv[]) |
| 880 | { |
| 881 | auto args = bhf::Commandline{ usage, argc, argv }; |
| 882 | |
| 883 | // drop argv[0] program name |
| 884 | args.Pop<const char *>(); |
| 885 | const auto str = args.Pop<const char *>("Target is missing"); |
| 886 | if (!strcmp("--help", str)) { |
| 887 | usage(); |
| 888 | } else if (!strcmp("--version", str)) { |
| 889 | return version(); |
| 890 | } |
| 891 | const auto split = std::strcspn(str, ":"); |
| 892 | const auto netId = std::string{ str, split }; |
| 893 | const auto port = bhf::try_stoi<uint16_t>(str + split); |
| 894 | LOG_VERBOSE("NetId>" << netId << "< port>" << port << "<\n"); |
| 895 | |
| 896 | bhf::ParameterList global = { |
| 897 | { "--gw" }, |
| 898 | { "--localams" }, |
| 899 | { "--log-level" }, |
| 900 | { "--retry" }, |
| 901 | }; |
| 902 | args.Parse(global); |
| 903 | |
| 904 | const auto retries = global.Get<size_t>("--retry", 0); |
| 905 | const auto localNetId = global.Get<std::string>("--localams"); |
| 906 | if (!localNetId.empty()) { |
| 907 | bhf::ads::SetLocalAddress(make_AmsNetId(localNetId)); |
| 908 | } |
| 909 | |
| 910 | const auto logLevel = global.Get<size_t>("--log-level", 1); |
| 911 | // highest loglevel is error==3, we allow 4 to disable all messages |
| 912 | Logger::logLevel = std::min(logLevel, (size_t)4); |
| 913 | |
| 914 | const auto cmd = args.Pop<const char *>("Command is missing"); |
| 915 | if (!strcmp("addroute", cmd)) { |
| 916 | return Run(std::bind(RunAddRoute, netId, args), retries); |
| 917 | } else if (!strcmp("netid", cmd)) { |
| 918 | return Run(std::bind(RunNetId, netId), retries); |
| 919 | } |
| 920 | |
| 921 | const auto commands = CommandMap{ |
| 922 | { "dc-diag", RunDCDiag }, { "ecat", RunECat }, |
| 923 | { "file", RunFile }, { "registry", RunRegistry }, |
| 924 | { "license", RunLicense }, { "pciscan", RunPCIScan }, |
| 925 | { "plc", RunPLC }, { "raw", RunRaw }, |
| 926 | { "rtime", RunRTime }, { "startprocess", RunStartProcess }, |
| 927 | { "state", RunState }, { "var", RunVar }, |
| 928 | }; |
| 929 | const auto it = commands.find(cmd); |
| 930 | if (it != commands.end()) { |
| 931 | return Run(std::bind(it->second, make_AmsNetId(netId), port, |
| 932 | global.Get<std::string>("--gw"), args), |
| 933 | retries); |
| 934 | } |
| 935 | usage(std::string{ "Unknown command >" } + cmd + "<\n"); |
| 936 | } |
nothing calls this directly
no test coverage detected