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-rust")) {
47 doGenerateCode_ = true;
48 targetLanguage_ = "rust";
49 } else if (option.Equals("-gen-cpp")) {
50 doGenerateCode_ = true;
51 targetLanguage_ = "cpp";
52 } else if (option.Equals("-gen-ts")) {
53 doGenerateCode_ = true;
54 targetLanguage_ = "ts";
55 } else if (option.Equals("-d")) {
56 generationDirectory_ = argv[i++];
57 } else if (!option.StartsWith("-")) {
58 sourceFile_ = option;
59 } else {
60 errors.Append(option);
61 errors.Append(" ");
62 }
63 }
64
65 illegalOptions_ = errors.ToString();
66 }
67
ShowErrors()68 void Options::ShowErrors()
69 {
70 if (!illegalOptions_.IsEmpty()) {
71 String options = illegalOptions_;
72 int index;
73 while ((index = options.IndexOf(' ')) != -1) {
74 printf("The Option \"%s\" is illegal.\n", options.Substring(0, index).string());
75 options = options.Substring(index + 1);
76 }
77 }
78 printf("Use \"--help\" to show usage.\n");
79 }
80
ShowVersion()81 void Options::ShowVersion()
82 {
83 printf("idl %d.%d\n"
84 "Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.\n\n",
85 VERSION_MAJOR, VERSION_MINOR);
86 }
87
ShowUsage()88 void Options::ShowUsage()
89 {
90 printf("Compile a .idl file and generate metadata, or generate Rust/C++/Ts codes from metadata.\n"
91 "Usage: idl [options] file\n"
92 "Options:\n"
93 " --help Display command line options\n"
94 " --version Display toolchain version information\n"
95 " -dump-ast Display the AST of the compiled file\n"
96 " -dump-metadata Display the metadata generated from the compiled file\n"
97 " -c Compile the .idl file\n"
98 " -s <file> Place the metadata into <file>\n"
99 " -gen-rust Generate Rust codes\n"
100 " -gen-cpp Generate C++ codes\n"
101 " -gen-ts Generate Ts codes\n"
102 " -d <directory> Place generated codes into <directory>\n");
103 }
104 }
105 }
106