• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "command_line.h"
17 #include <cstdint>
18 #include <cstdio>
19 #include <list>
20 #include <map>
21 #include <ostream>
22 #include "command_param.h"
23 #include "command_param_switch.h"
24 #include "command_param_text.h"
25 
GetInstance()26 CommandLine& CommandLine::GetInstance()
27 {
28     static CommandLine commandLine;
29     return commandLine;
30 }
31 
CommandLine()32 CommandLine::CommandLine()
33 {
34     paramIdx_ = 0;
35 }
~CommandLine()36 CommandLine::~CommandLine() {}
37 
AddParamText(const std::string & filter1,const std::string & filter2,std::string & pstr,const std::string & desc)38 void CommandLine::AddParamText(const std::string& filter1,
39                                const std::string& filter2,
40                                std::string& pstr,
41                                const std::string& desc)
42 {
43     auto pret = std::make_shared<CommandParamText>(pstr);
44     if (!filter1.empty()) {
45         pret->AddFilter(filter1);
46     }
47     if (!filter2.empty()) {
48         pret->AddFilter(filter2);
49     }
50     pret->paramType_ = PARAM_TYPE_TEXT;
51     pret->sDescriptor_ = desc;
52     commandParams_[paramIdx_++] = pret;
53 }
54 
AddParamSwitch(const std::string & filter1,const std::string & filter2,bool & pbool,const std::string & desc)55 void CommandLine::AddParamSwitch(const std::string& filter1,
56                                  const std::string& filter2,
57                                  bool &pbool,
58                                  const std::string& desc)
59 {
60     auto pret = std::make_shared<CommandParamSwitch>(pbool);
61     if (!filter1.empty()) {
62         pret->AddFilter(filter1);
63     }
64     if (!filter2.empty()) {
65         pret->AddFilter(filter2);
66     }
67     pret->paramType_ = PARAM_TYPE_SWITCH;
68     pret->sDescriptor_ = desc;
69     commandParams_[paramIdx_++] = pret;
70 }
71 
72 namespace {
73 const int USED_PARAM_ONE = 1;
74 const int USED_PARAM_TWO = 2;
75 const int USED_PARAM_ERR = 99999;
76 
77 const int MAX_TAB_COUNT = 2;
78 } // namespace
79 
CheckParam(const std::string & s1,const std::string & s2)80 int CommandLine::CheckParam(const std::string& s1, const std::string& s2)
81 {
82     for (auto it = commandParams_.begin(); it != commandParams_.end(); ++it) {
83         auto p = it->second;
84         if (!p->IsInFilter(s1)) {
85             continue;
86         }
87         switch (p->paramType_) {
88             case PARAM_TYPE_SWITCH: {
89                 CommandParamSwitch* pswitch = (CommandParamSwitch*)p.get();
90                 pswitch->SetValue(true);
91                 return USED_PARAM_ONE;
92             }
93                 break;
94             case PARAM_TYPE_TEXT: {
95                 CommandParamText* ptext = (CommandParamText*)p.get();
96                 if (s2 != "") {
97                     ptext->SetValue(s2);
98                     return USED_PARAM_TWO;
99                 }
100                 printf("%s lost content\n", s1.c_str());
101                 return USED_PARAM_ERR;
102             }
103                 break;
104             default:
105                 printf("unknown type\n");
106                 break;
107         }
108     }
109     printf("unknown param '%s'\n", s1.c_str());
110     return USED_PARAM_ERR;
111 }
AnalyzeParam(std::vector<std::string> argv)112 int CommandLine::AnalyzeParam(std::vector<std::string> argv)
113 {
114     size_t i = 1;
115     while (i < argv.size()) {
116         i += CheckParam(argv[i], (i + 1 < argv.size()) ? argv[i + 1] : "");
117     }
118     if (i >= USED_PARAM_ERR) {
119         return -1;
120     }
121     return 1;
122 }
123 
PrintHelp()124 void CommandLine::PrintHelp()
125 {
126     printf("%shelp :\n", VT100_RED);
127     for (auto it = commandParams_.begin(); it != commandParams_.end(); ++it) {
128         auto p = it->second;
129 
130         int i = 0;
131         for (std::string temp : p->paramFilter_) {
132             i++;
133             if (i == 1) {
134                 printf("  %-16s", temp.c_str());
135             } else {
136                 printf(" %-2s", temp.c_str());
137                 break;
138             }
139         }
140         for (; i < MAX_TAB_COUNT; i++) {
141             printf("   ");
142         }
143         printf("     : %s\n", p->sDescriptor_.c_str());
144     }
145     printf("%s", VT100_CLOSE);
146 }
147