1 /*
2  * Copyright (c) 2022-2023 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 #ifndef OHOS_IDL_OPTIONS_H
17 #define OHOS_IDL_OPTIONS_H
18 
19 #include "util/string.h"
20 
21 namespace OHOS {
22 namespace Idl {
23 class Options {
24 public:
Options(int argc,char ** argv)25     Options(int argc, char** argv)
26     {
27         Parse(argc, argv);
28     }
29 
30     ~Options() = default;
31 
32 struct Attribute {
33         String hitraceTag;
34         String logTag;
35         String domainId;
36         bool doHitrace;
37         bool doLog;
38     };
39 
DoShowUsage()40     bool DoShowUsage() const
41     {
42         return doShowUsage_;
43     }
44 
DoShowVersion()45     bool DoShowVersion() const
46     {
47         return doShowVersion_;
48     }
49 
DoCompile()50     bool DoCompile() const
51     {
52         return doCompile_;
53     }
54 
DoDumpAST()55     bool DoDumpAST() const
56     {
57         return doDumpAST_;
58     }
59 
DoDumpMetadata()60     bool DoDumpMetadata() const
61     {
62         return doDumpMetadata_;
63     }
64 
DoSaveMetadata()65     bool DoSaveMetadata() const
66     {
67         return doSaveMetadata_;
68     }
69 
DoGenerateCode()70     bool DoGenerateCode() const
71     {
72         return doGenerateCode_;
73     }
74 
DoHitraceState()75     bool DoHitraceState() const
76     {
77         return doHitrace_;
78     }
79 
DoSearchKeywords()80     bool DoSearchKeywords() const
81     {
82         return doKeywords_;
83     }
84 
DoLogOn()85     bool DoLogOn() const
86     {
87         if (!domainId_.IsNull() && !logTag_.IsNull()) {
88             return true;
89         }
90         return false;
91     }
92 
DoIllegalParameter(const String argv)93     bool DoIllegalParameter(const String argv) const
94     {
95         if (argv.IsEmpty() || argv.Equals("-t") || argv.Equals("-log-domainid") ||
96             argv.Equals("-log-tag")) {
97             return true;
98         }
99         return false;
100     }
101 
DoLegalLog()102     bool DoLegalLog() const
103     {
104         if (targetLanguage_.Equals("cpp")) {
105             if (!domainId_.IsNull() && !logTag_.IsNull()) {
106                 return true;
107             } else if (domainId_.IsNull() && logTag_.IsNull()) {
108                 return true;
109             } else {
110                 return false;
111             }
112         }
113         return true;
114     }
115 
HasErrors()116     bool HasErrors() const
117     {
118         return !illegalOptions_.IsEmpty() || sourceFile_.IsEmpty() || !DoLegalLog() || !doLegalParameters_;
119     }
120 
GetSourceFile()121     String GetSourceFile() const
122     {
123         return sourceFile_;
124     }
125 
GetMetadataFile()126     String GetMetadataFile() const
127     {
128         return metadataFile_;
129     }
130 
GetTargetLanguage()131     String GetTargetLanguage() const
132     {
133         return targetLanguage_;
134     }
135 
GetGenerationDirectory()136     String GetGenerationDirectory() const
137     {
138         return generationDirectory_;
139     }
140 
GetGenerateHitraceTag()141     String GetGenerateHitraceTag() const
142     {
143         return hitraceTag_;
144     }
145 
GetDomainId()146     String GetDomainId() const
147     {
148         return domainId_;
149     }
150 
GetLogTag()151     String GetLogTag() const
152     {
153         return logTag_;
154     }
155 
GetAttribute()156     Attribute GetAttribute() const
157     {
158         return attribute_;
159     }
160 
161     void ShowErrors();
162 
163     void ShowVersion();
164 
165     void ShowUsage();
166 
167     void ShowWarning();
168 
169 private:
170     void Parse(int argc, char** argv);
171 
172     bool ParseSub(const String& option, int& i, char** argv);
173 
174     static constexpr int VERSION_MAJOR = 0;
175     static constexpr int VERSION_MINOR = 1;
176 
177     String program_;
178     String sourceFile_;
179     String metadataFile_;
180     String targetLanguage_;
181     String generationDirectory_;
182     String illegalOptions_;
183     String hitraceTag_;
184     String domainId_;
185     String logTag_;
186     Attribute attribute_ = {"", "", "", false, false};
187 
188     bool doShowUsage_ = false;
189     bool doShowVersion_ = false;
190     bool doCompile_ = false;
191     bool doDumpAST_ = false;
192     bool doDumpMetadata_ = false;
193     bool doSaveMetadata_ = false;
194     bool doGenerateCode_ = false;
195     bool doHitrace_ = false;
196     bool doKeywords_ = false;
197     bool doLegalParameters_ = true;
198 };
199 }
200 }
201 #endif // OHOS_IDL_OPTIONS_H
202