1b1994897Sopenharmony_ci/*
2b1994897Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#include "abc2program_options.h"
17b1994897Sopenharmony_ci#include <sstream>
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_cinamespace panda::abc2program {
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_cibool Abc2ProgramOptions::Parse(int argc, const char **argv)
22b1994897Sopenharmony_ci{
23b1994897Sopenharmony_ci    panda::PandArg<bool> help("help", false, "Print this message and exit");
24b1994897Sopenharmony_ci    panda::PandArg<bool> debug(
25b1994897Sopenharmony_ci        "debug", false, "enable debug messages (will be printed to standard output if no --debug-file was specified) ");
26b1994897Sopenharmony_ci    panda::PandArg<std::string> debug_file("debug-file", "",
27b1994897Sopenharmony_ci                                           "(--debug-file FILENAME) set debug file name. default is std::cout");
28b1994897Sopenharmony_ci    panda::PandArg<std::string> input_file("input_file", "", "Path to the source binary code");
29b1994897Sopenharmony_ci    panda::PandArg<std::string> output_file("output_file", "", "Path to the generated assembly code");
30b1994897Sopenharmony_ci    pa_parser_.Add(&help);
31b1994897Sopenharmony_ci    pa_parser_.Add(&debug);
32b1994897Sopenharmony_ci    pa_parser_.Add(&debug_file);
33b1994897Sopenharmony_ci    pa_parser_.PushBackTail(&input_file);
34b1994897Sopenharmony_ci    pa_parser_.PushBackTail(&output_file);
35b1994897Sopenharmony_ci    pa_parser_.EnableTail();
36b1994897Sopenharmony_ci
37b1994897Sopenharmony_ci    if (!pa_parser_.Parse(argc, argv)) {
38b1994897Sopenharmony_ci        ConstructErrorMsg();
39b1994897Sopenharmony_ci        PrintErrorMsg();
40b1994897Sopenharmony_ci        pa_parser_.DisableTail();
41b1994897Sopenharmony_ci        return false;
42b1994897Sopenharmony_ci    }
43b1994897Sopenharmony_ci    if (debug.GetValue()) {
44b1994897Sopenharmony_ci        if (debug_file.GetValue().empty()) {
45b1994897Sopenharmony_ci            panda::Logger::InitializeStdLogging(
46b1994897Sopenharmony_ci                panda::Logger::Level::DEBUG,
47b1994897Sopenharmony_ci                panda::Logger::ComponentMask().set(panda::Logger::Component::ABC2PROGRAM));
48b1994897Sopenharmony_ci        } else {
49b1994897Sopenharmony_ci            panda::Logger::InitializeFileLogging(
50b1994897Sopenharmony_ci                debug_file.GetValue(), panda::Logger::Level::DEBUG,
51b1994897Sopenharmony_ci                panda::Logger::ComponentMask().set(panda::Logger::Component::ABC2PROGRAM));
52b1994897Sopenharmony_ci        }
53b1994897Sopenharmony_ci    } else {
54b1994897Sopenharmony_ci        panda::Logger::InitializeStdLogging(panda::Logger::Level::ERROR,
55b1994897Sopenharmony_ci                                            panda::Logger::ComponentMask().set(panda::Logger::Component::ABC2PROGRAM));
56b1994897Sopenharmony_ci    }
57b1994897Sopenharmony_ci    input_file_path_ = input_file.GetValue();
58b1994897Sopenharmony_ci    output_file_path_ = output_file.GetValue();
59b1994897Sopenharmony_ci    if (input_file_path_.empty() || output_file_path_.empty()) {
60b1994897Sopenharmony_ci        ConstructErrorMsg();
61b1994897Sopenharmony_ci        PrintErrorMsg();
62b1994897Sopenharmony_ci        pa_parser_.DisableTail();
63b1994897Sopenharmony_ci        return false;
64b1994897Sopenharmony_ci    }
65b1994897Sopenharmony_ci
66b1994897Sopenharmony_ci    pa_parser_.DisableTail();
67b1994897Sopenharmony_ci    return true;
68b1994897Sopenharmony_ci}
69b1994897Sopenharmony_ci
70b1994897Sopenharmony_civoid Abc2ProgramOptions::ConstructErrorMsg()
71b1994897Sopenharmony_ci{
72b1994897Sopenharmony_ci    std::stringstream ss;
73b1994897Sopenharmony_ci    ss << "Usage:" << std::endl;
74b1994897Sopenharmony_ci    ss << "abc2prog [options] input_file output_file" << std::endl;
75b1994897Sopenharmony_ci    ss << "Supported options:" << std::endl;
76b1994897Sopenharmony_ci    ss << pa_parser_.GetHelpString() << std::endl;
77b1994897Sopenharmony_ci    error_msg_ = ss.str();
78b1994897Sopenharmony_ci}
79b1994897Sopenharmony_ci
80b1994897Sopenharmony_ciconst std::string &Abc2ProgramOptions::GetInputFilePath() const
81b1994897Sopenharmony_ci{
82b1994897Sopenharmony_ci    return input_file_path_;
83b1994897Sopenharmony_ci}
84b1994897Sopenharmony_ci
85b1994897Sopenharmony_ciconst std::string &Abc2ProgramOptions::GetOutputFilePath() const
86b1994897Sopenharmony_ci{
87b1994897Sopenharmony_ci    return output_file_path_;
88b1994897Sopenharmony_ci}
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_civoid Abc2ProgramOptions::PrintErrorMsg() const
91b1994897Sopenharmony_ci{
92b1994897Sopenharmony_ci    std::cerr << error_msg_;
93b1994897Sopenharmony_ci}
94b1994897Sopenharmony_ci
95b1994897Sopenharmony_ci}  // namespace panda::abc2program
96