1 /*
2 * Copyright (c) 2024 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 "abc2program_options.h"
17
18 namespace panda::abc2program {
19
Parse(int argc,const char ** argv)20 bool Abc2ProgramOptions::Parse(int argc, const char **argv)
21 {
22 panda::PandArg<bool> help("help", false, "Print this message and exit");
23 panda::PandArg<bool> debug(
24 "debug", false, "enable debug messages (will be printed to standard output if no --debug-file was specified) ");
25 panda::PandArg<std::string> debug_file("debug-file", "",
26 "(--debug-file FILENAME) set debug file name. default is std::cout");
27 panda::PandArg<std::string> input_file("input_file", "", "Path to the source binary code");
28 panda::PandArg<std::string> output_file("output_file", "", "Path to the generated assembly code");
29 pa_parser_.Add(&help);
30 pa_parser_.Add(&debug);
31 pa_parser_.Add(&debug_file);
32 pa_parser_.PushBackTail(&input_file);
33 pa_parser_.PushBackTail(&output_file);
34 pa_parser_.EnableTail();
35
36 if (!pa_parser_.Parse(argc, argv)) {
37 ConstructErrorMsg();
38 PrintErrorMsg();
39 pa_parser_.DisableTail();
40 return false;
41 }
42 if (debug.GetValue()) {
43 if (debug_file.GetValue().empty()) {
44 panda::Logger::InitializeStdLogging(
45 panda::Logger::Level::DEBUG,
46 panda::Logger::ComponentMask().set(panda::Logger::Component::ABC2PROGRAM));
47 } else {
48 panda::Logger::InitializeFileLogging(
49 debug_file.GetValue(), panda::Logger::Level::DEBUG,
50 panda::Logger::ComponentMask().set(panda::Logger::Component::ABC2PROGRAM));
51 }
52 } else {
53 panda::Logger::InitializeStdLogging(panda::Logger::Level::ERROR,
54 panda::Logger::ComponentMask().set(panda::Logger::Component::ABC2PROGRAM));
55 }
56 input_file_path_ = input_file.GetValue();
57 output_file_path_ = output_file.GetValue();
58 if (input_file_path_.empty() || output_file_path_.empty()) {
59 ConstructErrorMsg();
60 PrintErrorMsg();
61 pa_parser_.DisableTail();
62 return false;
63 }
64
65 pa_parser_.DisableTail();
66 return true;
67 }
68
ConstructErrorMsg()69 void Abc2ProgramOptions::ConstructErrorMsg()
70 {
71 std::stringstream ss;
72 ss << "Usage:" << std::endl;
73 ss << "abc2prog [options] input_file output_file" << std::endl;
74 ss << "Supported options:" << std::endl;
75 ss << pa_parser_.GetHelpString() << std::endl;
76 error_msg_ = ss.str();
77 }
78
GetInputFilePath() const79 const std::string &Abc2ProgramOptions::GetInputFilePath() const
80 {
81 return input_file_path_;
82 }
83
GetOutputFilePath() const84 const std::string &Abc2ProgramOptions::GetOutputFilePath() const
85 {
86 return output_file_path_;
87 }
88
PrintErrorMsg() const89 void Abc2ProgramOptions::PrintErrorMsg() const
90 {
91 std::cerr << error_msg_;
92 }
93
94 } // namespace panda::abc2program
95