1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include "setresolution_utils.h"
17e0dac50fSopenharmony_ci
18e0dac50fSopenharmony_ci#include <getopt.h>
19e0dac50fSopenharmony_ci#include <iostream>
20e0dac50fSopenharmony_ci#include <string>
21e0dac50fSopenharmony_ci
22e0dac50fSopenharmony_cinamespace OHOS {
23e0dac50fSopenharmony_civoid SetResolutionUtils::PrintUsage(const std::string& cmdLine)
24e0dac50fSopenharmony_ci{
25e0dac50fSopenharmony_ci    std::cout << "usage: " << cmdLine.c_str() <<
26e0dac50fSopenharmony_ci        " [-w width] [-h height] [-d dpi] [-m]" << std::endl;
27e0dac50fSopenharmony_ci}
28e0dac50fSopenharmony_ci
29e0dac50fSopenharmony_cibool SetResolutionUtils::ProcessArgs(int argc, char * const argv[], CmdArgments& cmdArgments)
30e0dac50fSopenharmony_ci{
31e0dac50fSopenharmony_ci    int opt = 0;
32e0dac50fSopenharmony_ci    const struct option longOption[] = {
33e0dac50fSopenharmony_ci        { "width", required_argument, nullptr, 'w' },
34e0dac50fSopenharmony_ci        { "height", required_argument, nullptr, 'h' },
35e0dac50fSopenharmony_ci        { "dpi", required_argument, nullptr, 'd' },
36e0dac50fSopenharmony_ci        { "help", required_argument, nullptr, 'm' },
37e0dac50fSopenharmony_ci        { nullptr, 0, nullptr, 0 }
38e0dac50fSopenharmony_ci    };
39e0dac50fSopenharmony_ci    while ((opt = getopt_long(argc, argv, "w:h:d:m", longOption, nullptr)) != -1) {
40e0dac50fSopenharmony_ci        switch (opt) {
41e0dac50fSopenharmony_ci            case 'w': // output width
42e0dac50fSopenharmony_ci                cmdArgments.width = static_cast<uint32_t>(atoi(optarg));
43e0dac50fSopenharmony_ci                cmdArgments.isWidthSet = true;
44e0dac50fSopenharmony_ci                break;
45e0dac50fSopenharmony_ci            case 'h': // output height
46e0dac50fSopenharmony_ci                cmdArgments.height = static_cast<uint32_t>(atoi(optarg));
47e0dac50fSopenharmony_ci                cmdArgments.isHeightSet = true;
48e0dac50fSopenharmony_ci                break;
49e0dac50fSopenharmony_ci            case 'd': // output dpi
50e0dac50fSopenharmony_ci                cmdArgments.dpi = static_cast<uint32_t>(atoi(optarg));
51e0dac50fSopenharmony_ci                cmdArgments.isDpiSet = true;
52e0dac50fSopenharmony_ci                break;
53e0dac50fSopenharmony_ci            case 'm': // help
54e0dac50fSopenharmony_ci            default:
55e0dac50fSopenharmony_ci                SetResolutionUtils::PrintUsage(argv[0]);
56e0dac50fSopenharmony_ci                return false;
57e0dac50fSopenharmony_ci        }
58e0dac50fSopenharmony_ci    }
59e0dac50fSopenharmony_ci    return true;
60e0dac50fSopenharmony_ci}
61e0dac50fSopenharmony_ci}