1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef IDMAP2_INCLUDE_IDMAP2_COMMANDLINEOPTIONS_H_ 18 #define IDMAP2_INCLUDE_IDMAP2_COMMANDLINEOPTIONS_H_ 19 20 #include <functional> 21 #include <memory> 22 #include <ostream> 23 #include <string> 24 #include <vector> 25 26 #include "idmap2/Result.h" 27 28 namespace android::idmap2 { 29 30 /* 31 * Utility class to convert a command line, including options (--path foo.txt), 32 * into data structures (options.path = "foo.txt"). 33 */ 34 class CommandLineOptions { 35 public: 36 static std::unique_ptr<std::vector<std::string>> ConvertArgvToVector(int argc, const char** argv); 37 CommandLineOptions(const std::string & name)38 explicit CommandLineOptions(const std::string& name) : name_(name) { 39 } 40 41 CommandLineOptions& OptionalFlag(const std::string& name, const std::string& description, 42 bool* value); 43 CommandLineOptions& MandatoryOption(const std::string& name, const std::string& description, 44 std::string* value); 45 CommandLineOptions& MandatoryOption(const std::string& name, const std::string& description, 46 std::vector<std::string>* value); 47 CommandLineOptions& OptionalOption(const std::string& name, const std::string& description, 48 std::string* value); 49 CommandLineOptions& OptionalOption(const std::string& name, const std::string& description, 50 std::vector<std::string>* value); 51 Result<Unit> Parse(const std::vector<std::string>& argv) const; 52 void Usage(std::ostream& out) const; 53 54 private: 55 struct Option { 56 std::string name; 57 std::string description; 58 std::function<void(const std::string& value)> action; 59 enum { 60 COUNT_OPTIONAL, 61 COUNT_EXACTLY_ONCE, 62 COUNT_ONCE_OR_MORE, 63 COUNT_OPTIONAL_ONCE_OR_MORE, 64 } count; 65 bool argument; 66 }; 67 68 mutable std::vector<Option> options_; 69 std::string name_; 70 }; 71 72 } // namespace android::idmap2 73 74 #endif // IDMAP2_INCLUDE_IDMAP2_COMMANDLINEOPTIONS_H_ 75