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