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 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 "command_parse.h"
17#include <getopt.h>
18#include <iostream>
19namespace {
20static struct option g_longOptions[] = {
21    {"width", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_WIDTH)},
22    {"height", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEIGHT)},
23    {"in", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_INPUT)},
24    {"out", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_OUTPUT)},
25    {"help", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HELP)},
26    {nullptr, 0, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_UNKONWN)}};
27}
28
29bool CommandParse::Parse(int argc, char *argv[], CommandOpt &opt)
30{
31    while (1) {
32        int optionIndex = 0;
33        int c = getopt_long(argc, argv, "i:o:w:h:", g_longOptions, &optionIndex);
34        if (c == -1) {
35            break;
36        }
37        MyOptIndex index = static_cast<MyOptIndex>(c);
38        switch (index) {
39            case MyOptIndex::OPT_INDEX_INPUT:
40                opt.fileInput = optarg;
41                break;
42            case MyOptIndex::OPT_INDEX_OUTPUT:
43                opt.fileOutput = optarg;
44                break;
45            case MyOptIndex::OPT_INDEX_WIDTH:
46                opt.width = std::stoi(optarg);
47                break;
48            case MyOptIndex::OPT_INDEX_HEIGHT:
49                opt.height = std::stoi(optarg);
50                break;
51            default:
52                ShowUsage();
53                break;
54        }
55    }
56    if (opt.fileInput == "" || opt.fileOutput == ""  || opt.width == 0 || opt.height == 0) {
57        return false;
58    }
59    return true;
60}
61
62void CommandParse::ShowUsage()
63{
64    std::cout << "Options:" << std::endl;
65    std::cout << " --help                     Print this help info." << std::endl;
66    std::cout << " -w, --width=width          The video width." << std::endl;
67    std::cout << " -h, --height=height        The video height." << std::endl;
68    std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
69    std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
70}
71