1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "command_parse.h"
17094332d3Sopenharmony_ci#include <getopt.h>
18094332d3Sopenharmony_ci#include <iostream>
19094332d3Sopenharmony_cinamespace {
20094332d3Sopenharmony_cistatic struct option g_longOptions[] = {
21094332d3Sopenharmony_ci    {"width", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_WIDTH)},
22094332d3Sopenharmony_ci    {"height", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEIGHT)},
23094332d3Sopenharmony_ci    {"in", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_INPUT)},
24094332d3Sopenharmony_ci    {"out", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_OUTPUT)},
25094332d3Sopenharmony_ci    {"help", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HELP)},
26094332d3Sopenharmony_ci    {nullptr, 0, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_UNKONWN)}};
27094332d3Sopenharmony_ci}
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_cibool CommandParse::Parse(int argc, char *argv[], CommandOpt &opt)
30094332d3Sopenharmony_ci{
31094332d3Sopenharmony_ci    while (1) {
32094332d3Sopenharmony_ci        int optionIndex = 0;
33094332d3Sopenharmony_ci        int c = getopt_long(argc, argv, "i:o:w:h:", g_longOptions, &optionIndex);
34094332d3Sopenharmony_ci        if (c == -1) {
35094332d3Sopenharmony_ci            break;
36094332d3Sopenharmony_ci        }
37094332d3Sopenharmony_ci        MyOptIndex index = static_cast<MyOptIndex>(c);
38094332d3Sopenharmony_ci        switch (index) {
39094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_INPUT:
40094332d3Sopenharmony_ci                opt.fileInput = optarg;
41094332d3Sopenharmony_ci                break;
42094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_OUTPUT:
43094332d3Sopenharmony_ci                opt.fileOutput = optarg;
44094332d3Sopenharmony_ci                break;
45094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_WIDTH:
46094332d3Sopenharmony_ci                opt.width = std::stoi(optarg);
47094332d3Sopenharmony_ci                break;
48094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_HEIGHT:
49094332d3Sopenharmony_ci                opt.height = std::stoi(optarg);
50094332d3Sopenharmony_ci                break;
51094332d3Sopenharmony_ci            default:
52094332d3Sopenharmony_ci                ShowUsage();
53094332d3Sopenharmony_ci                break;
54094332d3Sopenharmony_ci        }
55094332d3Sopenharmony_ci    }
56094332d3Sopenharmony_ci    if (opt.fileInput == "" || opt.fileOutput == ""  || opt.width == 0 || opt.height == 0) {
57094332d3Sopenharmony_ci        return false;
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci    return true;
60094332d3Sopenharmony_ci}
61094332d3Sopenharmony_ci
62094332d3Sopenharmony_civoid CommandParse::ShowUsage()
63094332d3Sopenharmony_ci{
64094332d3Sopenharmony_ci    std::cout << "Options:" << std::endl;
65094332d3Sopenharmony_ci    std::cout << " --help                     Print this help info." << std::endl;
66094332d3Sopenharmony_ci    std::cout << " -w, --width=width          The video width." << std::endl;
67094332d3Sopenharmony_ci    std::cout << " -h, --height=height        The video height." << std::endl;
68094332d3Sopenharmony_ci    std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
69094332d3Sopenharmony_ci    std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
70094332d3Sopenharmony_ci}
71