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 #include <sstream>
18
19 namespace ark::abc2program {
20
Parse(int argc,const char ** argv)21 bool Abc2ProgramOptions::Parse(int argc, const char **argv)
22 {
23 ark::PandArg<bool> help("help", false, "Print this message and exit");
24 ark::PandArg<bool> debug(
25 "debug", false, "enable debug messages (will be printed to standard output if no --debug-file was specified) ");
26 ark::PandArg<std::string> debugFile("debug-file", "",
27 "(--debug-file FILENAME) set debug file name. default is std::cout");
28 ark::PandArg<std::string> inputFile("inputFile", "", "Path to the source binary code");
29 ark::PandArg<std::string> outputFile("outputFile", "", "Path to the generated assembly code");
30 helpArg_ = &help;
31 debugArg_ = &debug;
32 debugFileArg_ = &debugFile;
33 inputFileArg_ = &inputFile;
34 outputFileArg_ = &outputFile;
35 paParser_.Add(&help);
36 paParser_.Add(&debug);
37 paParser_.Add(&debugFile);
38 paParser_.PushBackTail(&inputFile);
39 paParser_.PushBackTail(&outputFile);
40 paParser_.EnableTail();
41 if (!ProcessArgs(argc, argv)) {
42 PrintErrorMsg();
43 paParser_.DisableTail();
44 return false;
45 }
46 paParser_.DisableTail();
47 return true;
48 }
49
ProcessArgs(int argc,const char ** argv)50 bool Abc2ProgramOptions::ProcessArgs(int argc, const char **argv)
51 {
52 if (!paParser_.Parse(argc, argv)) {
53 ConstructErrorMsg();
54 return false;
55 }
56 if (debugArg_->GetValue()) {
57 if (debugFileArg_->GetValue().empty()) {
58 ark::Logger::InitializeStdLogging(ark::Logger::Level::DEBUG,
59 ark::Logger::ComponentMask().set(ark::Logger::Component::ABC2PROGRAM));
60 } else {
61 ark::Logger::InitializeFileLogging(debugFileArg_->GetValue(), ark::Logger::Level::DEBUG,
62 ark::Logger::ComponentMask().set(ark::Logger::Component::ABC2PROGRAM));
63 }
64 } else {
65 ark::Logger::InitializeStdLogging(ark::Logger::Level::ERROR,
66 ark::Logger::ComponentMask().set(ark::Logger::Component::ABC2PROGRAM));
67 }
68 inputFilePath_ = inputFileArg_->GetValue();
69 outputFilePath_ = outputFileArg_->GetValue();
70 if (inputFilePath_.empty() || outputFilePath_.empty()) {
71 ConstructErrorMsg();
72 return false;
73 }
74 return true;
75 }
76
ConstructErrorMsg()77 void Abc2ProgramOptions::ConstructErrorMsg()
78 {
79 std::stringstream ss;
80 ss << "Usage:" << std::endl;
81 ss << "abc2prog [options] inputFile outputFile" << std::endl;
82 ss << "Supported options:" << std::endl;
83 ss << paParser_.GetHelpString() << std::endl;
84 errorMsg_ = ss.str();
85 }
86
GetInputFilePath() const87 const std::string &Abc2ProgramOptions::GetInputFilePath() const
88 {
89 return inputFilePath_;
90 }
91
GetOutputFilePath() const92 const std::string &Abc2ProgramOptions::GetOutputFilePath() const
93 {
94 return outputFilePath_;
95 }
96
PrintErrorMsg() const97 void Abc2ProgramOptions::PrintErrorMsg() const
98 {
99 std::cerr << errorMsg_;
100 }
101
102 } // namespace ark::abc2program
103