1 /*
2 * Copyright (c) 2023 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 "setresolution_utils.h"
17
18 #include <getopt.h>
19 #include <iostream>
20 #include <string>
21
22 namespace OHOS {
PrintUsage(const std::string & cmdLine)23 void SetResolutionUtils::PrintUsage(const std::string& cmdLine)
24 {
25 std::cout << "usage: " << cmdLine.c_str() <<
26 " [-w width] [-h height] [-d dpi] [-m]" << std::endl;
27 }
28
ProcessArgs(int argc,char * const argv[],CmdArgments & cmdArgments)29 bool SetResolutionUtils::ProcessArgs(int argc, char * const argv[], CmdArgments& cmdArgments)
30 {
31 int opt = 0;
32 const struct option longOption[] = {
33 { "width", required_argument, nullptr, 'w' },
34 { "height", required_argument, nullptr, 'h' },
35 { "dpi", required_argument, nullptr, 'd' },
36 { "help", required_argument, nullptr, 'm' },
37 { nullptr, 0, nullptr, 0 }
38 };
39 while ((opt = getopt_long(argc, argv, "w:h:d:m", longOption, nullptr)) != -1) {
40 switch (opt) {
41 case 'w': // output width
42 cmdArgments.width = static_cast<uint32_t>(atoi(optarg));
43 cmdArgments.isWidthSet = true;
44 break;
45 case 'h': // output height
46 cmdArgments.height = static_cast<uint32_t>(atoi(optarg));
47 cmdArgments.isHeightSet = true;
48 break;
49 case 'd': // output dpi
50 cmdArgments.dpi = static_cast<uint32_t>(atoi(optarg));
51 cmdArgments.isDpiSet = true;
52 break;
53 case 'm': // help
54 default:
55 SetResolutionUtils::PrintUsage(argv[0]);
56 return false;
57 }
58 }
59 return true;
60 }
61 }