aboutsummaryrefslogtreecommitdiffstats
path: root/native/dicttoolkit/src/utils/arguments_parser.cpp
blob: 1451284f152e87dee96c9d7e1bfc32ffca26b26b (about) (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "utils/arguments_parser.h"

#include <unordered_set>

namespace latinime {
namespace dicttoolkit {

const size_t ArgumentSpec::UNLIMITED_COUNT = S_INT_MAX;

bool ArgumentsParser::validateSpecs() const {
    std::unordered_set<std::string> argumentNameSet;
    for (size_t i = 0; i < mArgumentSpecs.size() ; ++i) {
        if (mArgumentSpecs[i].getMinCount() == 0 && mArgumentSpecs[i].getMaxCount() == 0) {
            AKLOGE("minCount = maxCount = 0 for %s.", mArgumentSpecs[i].getName().c_str());
            return false;
        }
        if (mArgumentSpecs[i].getMinCount() != mArgumentSpecs[i].getMaxCount()
                && i != mArgumentSpecs.size() - 1) {
            AKLOGE("Variable length argument must be at the end.",
                    mArgumentSpecs[i].getName().c_str()v  );
            return false;
        }
        if (argumentNameSet.count(mArgumentSpecs[i].getName()) > 0) {
            AKLOGE("Multiple arguments have the same name \"%s\".",
                    mArgumentSpecs[i].getName().c_str());
            return false;
        }
        argumentNameSet.insert(mArgumentSpecs[i].getName());
    }
    return true;
}

void ArgumentsParser::printUsage(const std::string &commandName,
        const std::string &description) const {
    printf("Usage: %s", commandName.c_str());
    for (const auto &option : mOptionSpecs) {
        const std::string &optionName = option.first;
        const OptionSpec &spec = option.second;
        printf(" [-%s", optionName.c_str());
        if (spec.needsValue()) {
            printf(" <%s>", spec.getValueName().c_str());
        }
        printf("]");
    }
    for (const auto &argSpec : mArgumentSpecs) {
        if (argSpec.getMinCount() == 0 && argSpec.getMaxCount() == 1) {
            printf(" [<%s>]", argSpec.getName().c_str());
        } else if (argSpec.getMinCount() == 1 && argSpec.getMaxCount() == 1) {
            printf(" <%s>", argSpec.getName().c_str());
        } else if (argSpec.getMinCount() == 0) {
            printf(" [<%s>...]", argSpec.getName().c_str());
        } else if (argSpec.getMinCount() == 1) {
            printf(" <%s>...", argSpec.getName().c_str());
        }
    }
    printf("\n%s\n\n", description.c_str());
    for (const auto &option : mOptionSpecs) {
        const std::string &optionName = option.first;
        const OptionSpec &spec = option.second;
        printf(" -%s", optionName.c_str());
        if (spec.needsValue()) {
            printf(" <%s>", spec.getValueName().c_str());
        }
        printf("\t\t\t%s", spec.getDescription().c_str());
        if (spec.needsValue() && !spec.getDefaultValue().empty()) {
            printf("\tdefault: %s", spec.getDefaultValue().c_str());
        }
        printf("\n");
    }
    for (const auto &argSpec : mArgumentSpecs) {
        printf(" <%s>\t\t\t%s\n", argSpec.getName().c_str(), argSpec.getDescription().c_str());
    }
    printf("\n\n");
}

const ArgumentsAndOptions ArgumentsParser::parseArguments(const int argc, char **argv,
        const bool printErrorMessage) const {
    if (argc <= 0) {
        AKLOGE("Invalid argc (%d).", argc);
        ASSERT(false);
        return ArgumentsAndOptions();
    }
    std::unordered_map<std::string, std::string> options;
    for (const auto &entry : mOptionSpecs) {
        const std::string &optionName = entry.first;
        const OptionSpec &optionSpec = entry.second;
        if (optionSpec.needsValue() && !optionSpec.getDefaultValue().empty()) {
            // Set default value.
            options[optionName] = optionSpec.getDefaultValue();
        }
    }
    std::unordered_map<std::string, std::vector<std::string>> arguments;
    auto argumentSpecIt = mArgumentSpecs.cbegin();
    for (int i = 1; i < argc; ++i) {
        const std::string arg = argv[i];
        if (arg.length() > 1 && arg[0] == '-') {
            // option
            const std::string optionName = arg.substr(1);
            const auto it = mOptionSpecs.find(optionName);
            if (it == mOptionSpecs.end()) {
                if (printErrorMessage) {
                    fprintf(stderr, "Unknown option: '%s'\n", optionName.c_str());
                }
                return ArgumentsAndOptions();
            }
            std::string optionValue;
            if (it->second.needsValue()) {
                ++i;
                if (i >= argc) {
                    if (printErrorMessage) {
                        fprintf(stderr, "Missing argument for option '%s'\n", optionName.c_str());
                    }
                    return ArgumentsAndOptions();
                }
                optionValue = argv[i];
            }
            options[optionName] = optionValue;
        } else {
            // argument
            if (argumentSpecIt == mArgumentSpecs.end()) {
                if (printErrorMessage) {
                    fprintf(stderr, "Too many arguments.\n");
                }
                return ArgumentsAndOptions();
            }
            arguments[argumentSpecIt->getName()].push_back(arg);
            if (arguments[argumentSpecIt->getName()].size() >= argumentSpecIt->getMaxCount()) {
                ++argumentSpecIt;
            }
        }
    }

    if (argumentSpecIt != mArgumentSpecs.end()) {
        const auto &it = arguments.find(argumentSpecIt->getName());
        const size_t minCount = argumentSpecIt->getMinCount();
        const size_t actualcount = it == arguments.end() ? 0 : it->second.size();
        if (minCount > actualcount) {
            if (printErrorMessage) {
                fprintf(stderr, "Not enough arguments. %zd argumant(s) required for <%s>\n",
                        minCount, argumentSpecIt->getName().c_str());
            }
            return ArgumentsAndOptions();
        }
    }
    return ArgumentsAndOptions(std::move(options), std::move(arguments));
}

} // namespace dicttoolkit
} // namespace latinime