1 /*
2 * Copyright (c) 2022 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 #include "command_adapter_parse.h"
16 #include <getopt.h>
17
18
19 namespace {
20 enum class MyOptIndex {
21 OPT_INDEX_UNKONWN = 0,
22 OPT_INDEX_BUFFER_HANDLE,
23 OPT_INDEX_HEVC,
24 OPT_INDEX_HELP,
25 OPT_INDEX_HEIGHT = 'h',
26 OPT_INDEX_INPUT = 'i',
27 OPT_INDEX_OUTPUT = 'o',
28 OPT_INDEX_WIDTH = 'w'
29 };
30 }
31
Parse(int argc,char * argv[],CommandOpt & opt)32 bool CommandAdapterParse::Parse(int argc, char *argv[], CommandOpt &opt)
33 {
34 while (1) {
35 int optionIndex = 0;
36 static struct option long_options[] = {
37 {"width", required_argument, 0, (int)MyOptIndex::OPT_INDEX_WIDTH },
38 {"height", required_argument, 0, (int)MyOptIndex::OPT_INDEX_HEIGHT },
39 {"in", required_argument, 0, (int)MyOptIndex::OPT_INDEX_INPUT },
40 {"out", required_argument, 0, (int)MyOptIndex::OPT_INDEX_OUTPUT },
41 {"help", no_argument, NULL, (int)MyOptIndex::OPT_INDEX_HELP },
42 {0, 0, 0, (int)MyOptIndex::OPT_INDEX_UNKONWN}
43 };
44 int c = getopt_long(argc, argv, "i:o:w:h:", long_options, &optionIndex);
45 if (c == -1) {
46 break;
47 }
48 MyOptIndex index = (MyOptIndex)c;
49 switch (index) {
50 case MyOptIndex::OPT_INDEX_BUFFER_HANDLE:
51 opt.useBuffer = true;
52 break;
53 case MyOptIndex::OPT_INDEX_HEVC:
54 opt.codec = codecMime::HEVC;
55 break;
56 case MyOptIndex::OPT_INDEX_HELP:
57 ShowUsage();
58 break;
59 case MyOptIndex::OPT_INDEX_INPUT:
60 opt.fileInput = optarg;
61 break;
62 case MyOptIndex::OPT_INDEX_OUTPUT:
63 opt.fileOutput = optarg;
64 break;
65 case MyOptIndex::OPT_INDEX_WIDTH:
66 opt.width = std::stoi(optarg);
67 break;
68 case MyOptIndex::OPT_INDEX_HEIGHT:
69 opt.height = std::stoi(optarg);
70 break;
71 default:
72 ShowUsage();
73 break;
74 }
75 }
76 if (opt.fileInput.empty() || opt.fileOutput.empty() || opt.width == 0 || opt.height == 0) {
77 return false;
78 }
79 return true;
80 }
81
ShowUsage()82 void CommandAdapterParse::ShowUsage()
83 {
84 std::cout << "Options:" << std::endl;
85 std::cout << " -w, --width=width The video width." << std::endl;
86 std::cout << " -h, --height=height The video height." << std::endl;
87 std::cout << " -o, --out=FILE The file name for output file." << std::endl;
88 std::cout << " -i, --in=FILE The file name for input file." << std::endl;
89 std::cout << " --help The help info." << std::endl;
90 }
91