aboutsummaryrefslogtreecommitdiff
path: root/samples/ObjectDetection/src/CmdArgsParser.cpp
blob: b8c74bc10f34a6f54e45553bfc8d87f1b54a59b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "CmdArgsParser.hpp"
#include <iostream>
/*
 * Checks that a particular option was specified by the user
 */
bool CheckOptionSpecified(const std::map<std::string, std::string>& options, const std::string& option)
{
    auto it = options.find(option);
    return it!=options.end();
}

/*
 * Retrieves the user provided option
 */
std::string GetSpecifiedOption(const std::map<std::string, std::string>& options, const std::string& option)
{
    if (CheckOptionSpecified(options, option)){
        return options.at(option);
    }
    else
    {
        throw std::invalid_argument("Required option: " + option + " not defined.");
    }
}

/*
 * Parses all the command line options provided by the user and stores in a map.
 */
int ParseOptions(std::map<std::string, std::string>& options, std::map<std::string, std::string>& acceptedOptions,
                 char *argv[], int argc)
{
    for (int i = 1; i < argc; ++i)
    {
        std::string currentOption = std::string(argv[i]);
        auto it = acceptedOptions.find(currentOption);
        if (it != acceptedOptions.end())
        {
            if (i + 1 < argc && std::string(argv[i + 1]).rfind("--", 0) != 0)
            {
                std::string value = argv[++i];
                options.insert({it->first, value});
            }
            else if (std::string(argv[i]) == HELP)
            {
                std::cout << "Available options" << std::endl;
                for (auto & acceptedOption : acceptedOptions)
                {
                    std::cout << acceptedOption.first << " : " << acceptedOption.second << std::endl;
                }
                return 2;
            }
            else
            {
                std::cerr << std::string(argv[i]) << " option requires one argument." << std::endl;
                return 1;
            }
        }
        else
        {
            std::cerr << "Unrecognised option: " << std::string(argv[i]) << std::endl;
            return 1;
        }
    }
    return 0;
}