1 /*
2 * Copyright (c) 2022 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 "options.h"
17
18 #include <cstdio>
19 #include <cstring>
20
21 #include "util/string_builder.h"
22
23 namespace OHOS {
24 namespace Idl {
Parse(int argc,char ** argv)25 void Options::Parse(int argc, char** argv)
26 {
27 StringBuilder errors;
28 program_ = argv[0];
29
30 int i = 1;
31 while (i < argc) {
32 String option(argv[i++]);
33 if (option.Equals("--help")) {
34 doShowUsage_ = true;
35 } else if (option.Equals("--version")) {
36 doShowVersion_ = true;
37 } else if (option.Equals("-c")) {
38 doCompile_ = true;
39 } else if (option.Equals("-dump-ast")) {
40 doDumpAST_ = true;
41 } else if (option.Equals("-dump-metadata")) {
42 doDumpMetadata_ = true;
43 } else if (option.Equals("-s")) {
44 doSaveMetadata_ = true;
45 metadataFile_ = argv[i++];
46 } else if (option.Equals("-gen-cpp")) {
47 doGenerateCode_ = true;
48 targetLanguage_ = "cpp";
49 } else if (option.Equals("-gen-ts")) {
50 doGenerateCode_ = true;
51 targetLanguage_ = "ts";
52 } else if (option.Equals("-d")) {
53 generationDirectory_ = argv[i++];
54 } else if (!option.StartsWith("-")) {
55 sourceFile_ = option;
56 } else {
57 errors.Append(option);
58 errors.Append(" ");
59 }
60 }
61
62 illegalOptions_ = errors.ToString();
63 }
64
ShowErrors()65 void Options::ShowErrors()
66 {
67 if (!illegalOptions_.IsEmpty()) {
68 String options = illegalOptions_;
69 int index;
70 while ((index = options.IndexOf(' ')) != -1) {
71 printf("The Option \"%s\" is illegal.\n", options.Substring(0, index).string());
72 options = options.Substring(index + 1);
73 }
74 }
75 printf("Use \"--help\" to show usage.\n");
76 }
77
ShowVersion()78 void Options::ShowVersion()
79 {
80 printf("idl %d.%d\n"
81 "Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.\n\n",
82 VERSION_MAJOR, VERSION_MINOR);
83 }
84
ShowUsage()85 void Options::ShowUsage()
86 {
87 printf("Compile a .idl file and generate metadata, or generate C++ and Ts codes from metadata.\n"
88 "Usage: idl [options] file\n"
89 "Options:\n"
90 " --help Display command line options\n"
91 " --version Display toolchain version information\n"
92 " -dump-ast Display the AST of the compiled file\n"
93 " -dump-metadata Display the metadata generated from the compiled file\n"
94 " -c Compile the .idl file\n"
95 " -s <file> Place the metadata into <file>\n"
96 " -gen-cpp Generate C++ codes\n"
97 " -gen-ts Generate Ts codes\n"
98 " -d <directory> Place generated codes into <directory>\n");
99 }
100 }
101 }
102