//===- llvm/Support/CommandLine.h - Command line handler --------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This class implements a command line argument processor that is useful when // creating a tool. It provides a simple, minimalistic interface that is easily // extensible and supports nonlocal (library) command line options. // // Note that rather than trying to figure out what this code does, you should // read the library documentation located in docs/CommandLine.html or looks at // the many example usages in tools/*/*.cpp // //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_COMMANDLINE_H #define LLVM_SUPPORT_COMMANDLINE_H #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/raw_ostream.h" #include #include #include #include #include #include #include #include namespace llvm { class StringSaver; /// cl Namespace - This namespace contains all of the command line option /// processing machinery. It is intentionally a short name to make qualified /// usage concise. namespace cl { //===----------------------------------------------------------------------===// // ParseCommandLineOptions - Command line option processing entry point. // // Returns true on success. Otherwise, this will print the error message to // stderr and exit if \p Errs is not set (nullptr by default), or print the // error message to \p Errs and return false if \p Errs is provided. // // If EnvVar is not nullptr, command-line options are also parsed from the // environment variable named by EnvVar. Precedence is given to occurrences // from argv. This precedence is currently implemented by parsing argv after // the environment variable, so it is only implemented correctly for options // that give precedence to later occurrences. If your program supports options // that give precedence to earlier occurrences, you will need to extend this // function to support it correctly. bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview = "", raw_ostream *Errs = nullptr, const char *EnvVar = nullptr, bool LongOptionsUseDoubleDash = false); // Function pointer type for printing version information. using VersionPrinterTy = std::function; ///===---------------------------------------------------------------------===// /// SetVersionPrinter - Override the default (LLVM specific) version printer /// used to print out the version when --version is given /// on the command line. This allows other systems using the /// CommandLine utilities to print their own version string. void SetVersionPrinter(VersionPrinterTy func); ///===---------------------------------------------------------------------===// /// AddExtraVersionPrinter - Add an extra printer to use in addition to the /// default one. This can be called multiple times, /// and each time it adds a new function to the list /// which will be called after the basic LLVM version /// printing is complete. Each can then add additional /// information specific to the tool. void AddExtraVersionPrinter(VersionPrinterTy func); // PrintOptionValues - Print option values. // With -print-options print the difference between option values and defaults. // With -print-all-options print all option values. // (Currently not perfect, but best-effort.) void PrintOptionValues(); // Forward declaration - AddLiteralOption needs to be up here to make gcc happy. class Option; /// Adds a new option for parsing and provides the option it refers to. /// /// \param O pointer to the option /// \param Name the string name for the option to handle during parsing /// /// Literal options are used by some parsers to register special option values. /// This is how the PassNameParser registers pass names for opt. void AddLiteralOption(Option &O, StringRef Name); //===----------------------------------------------------------------------===// // Flags permitted to be passed to command line arguments // enum NumOccurrencesFlag { // Flags for the number of occurrences allowed Optional = 0x00, // Zero or One occurrence ZeroOrMore = 0x01, // Zero or more occurrences allowed Required = 0x02, // One occurrence required OneOrMore = 0x03, // One or more occurrences required // ConsumeAfter - Indicates that this option is fed anything that follows the // last positional argument required by the application (it is an error if // there are zero positional arguments, and a ConsumeAfter option is used). // Thus, for example, all arguments to LLI are processed until a filename is // found. Once a filename is found, all of the succeeding arguments are // passed, unprocessed, to the ConsumeAfter option. // ConsumeAfter = 0x04 }; enum ValueExpected { // Is a value required for the option? // zero reserved for the unspecified value ValueOptional = 0x01, // The value can appear... or not ValueRequired = 0x02, // The value is required to appear! ValueDisallowed = 0x03 // A value may not be specified (for flags) }; enum OptionHidden { // Control whether -help shows this option NotHidden = 0x00, // Option included in -help & -help-hidden Hidden = 0x01, // -help doesn't, but -help-hidden does ReallyHidden = 0x02 // Neither -help nor -help-hidden show this arg }; // Formatting flags - This controls special features that the option might have // that cause it to be parsed differently... // // Prefix - This option allows arguments that are otherwise unrecognized to be // matched by options that are a prefix of the actual value. This is useful for // cases like a linker, where options are typically of the form '-lfoo' or // '-L../../include' where -l or -L are the actual flags. When prefix is // enabled, and used, the value for the flag comes from the suffix of the // argument. // // AlwaysPrefix - Only allow the behavior enabled by the Prefix flag and reject // the Option=Value form. // enum FormattingFlags { NormalFormatting = 0x00, // Nothing special Positional = 0x01, // Is a positional argument, no '-' required Prefix = 0x02, // Can this option directly prefix its value? AlwaysPrefix = 0x03 // Can this option only directly prefix its value? }; enum MiscFlags { // Miscellaneous flags to adjust argument CommaSeparated = 0x01, // Should this cl::list split between commas? PositionalEatsArgs = 0x02, // Should this positional cl::list eat -args? Sink = 0x04, // Should this cl::list eat all unknown options? // Grouping - Can this option group with other options? // If this is enabled, multiple letter options are allowed to bunch together // with only a single hyphen for the whole group. This allows emulation // of the behavior that ls uses for example: ls -la === ls -l -a Grouping = 0x08, // Default option DefaultOption = 0x10 }; //===----------------------------------------------------------------------===// // Option Category class // class OptionCategory { private: StringRef const Name; StringRef const Description; void registerCategory(); public: OptionCategory(StringRef const Name, StringRef const Description = "") : Name(Name), Description(Description) { registerCategory(); } StringRef getName() const { return Name; } StringRef getDescription() const { return Description; } }; // The general Option Category (used as default category). extern OptionCategory GeneralCategory; //===----------------------------------------------------------------------===// // SubCommand class // class SubCommand { private: StringRef Name; StringRef Description; protected: void registerSubCommand(); void unregisterSubCommand(); public: SubCommand(StringRef Name, StringRef Description = "") : Name(Name), Description(Description) { registerSubCommand(); } SubCommand() = default; void reset(); explicit operator bool() const; StringRef getName() const { return Name; } StringRef getDescription() const { return Description; } SmallVector