// Copyright 2024 The Dawn & Tint Authors // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // 3. Neither the name of the copyright holder nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef SRC_DAWN_UTILS_COMMANDLINEPARSER_H_ #define SRC_DAWN_UTILS_COMMANDLINEPARSER_H_ #include <memory> #include <ostream> #include <string> #include <utility> #include <vector> #include "absl/types/span.h" // TODO(343500108): Use std::span when we have C++20. #include "dawn/common/Assert.h" #include "dawn/common/NonMovable.h" namespace dawn::utils { // A helper class to parse command line arguments. // // CommandLineParser parser; // auto& dryRun = parser.AddBool("dry-run", "fake operations").ShortName('d'); // auto& input = parser.AddString("input", "the input file to process").ShortName('i'); // auto& help = opts.AddHelp(); // // auto result = parser.Parse(argc, argv); // if (!result.success) { // std::cerr << result.errorMessage << "\n"; // return 1; // } // // if (help.GetValue()) { // std::cout << "Usage: " << argv[0] << " <options>\n\noptions\n"; // parser.PrintHelp(std::cout); // return 0; // } // // if (dryRun.GetValue() && input.IsSet()) { // doStuffWith(input.GetValue()); // } // // ... // // Command line options can use short-form for boolean options "(-f") and use both spaces or = to // separate the value for an option ("-f=foo" and "-f foo"). // TODO(42241992): Considering supporting more types of options and command line parsing niceties. // - Support "-" with a bunch of short names (like grep -rniI) // - Support returning the part that hasn't been parsed at the end so that users can do something // with it. // - Support "--" being used to separate remaining args. // - Support setting a default to show it in the help. class CommandLineParser { … }; // Option<Child> template <typename Child> Child& CommandLineParser::Option<Child>::ShortName(char shortName) { … } template <typename Child> Child& CommandLineParser::Option<Child>::Parameter(std::string parameter) { … } // EnumOption<E> template <typename E> CommandLineParser::EnumOption<E>::EnumOption( std::vector<std::pair<std::string_view, E>> conversions, std::string_view name, std::string_view desc) : … { … } template <typename E> CommandLineParser::EnumOption<E>::~EnumOption<E>() = default; template <typename E> E CommandLineParser::EnumOption<E>::GetValue() const { … } template <typename E> CommandLineParser::OptionBase::ParseResult CommandLineParser::EnumOption<E>::ParseImpl( absl::Span<const std::string_view> args) { … } template <typename E> std::string CommandLineParser::EnumOption<E>::GetParameter() const { … } template <typename E> CommandLineParser::EnumOption<E>& CommandLineParser::EnumOption<E>::Default(E value) { … } template <typename E> std::string CommandLineParser::EnumOption<E>::JoinNames(std::string_view separator) const { … } } // namespace dawn::utils #endif // SRC_DAWN_UTILS_COMMANDLINEPARSER_H_