From 19c0866b050a022406867aa650302f4efbf8e010 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Sat, 29 Apr 2023 09:24:26 -0400 Subject: CommandOptions (#2856) * WIP CommandOptions * Fix some output issues. * Simplify word wrapping. * Add file extensions. * Change how lookup takes place. Add appendSplit functions to StringUtil. Make Categories hold the index range of their options. * Small improvement. * Lookup with partial option names. * Associate user values. * Encoding flags in the name. * Refactor setting up of command options. * Use CommandOptions in slang-options. * Remove old help text. * Cache the CommandOptions on the Session. * Range checking. Fix bug in the Options handling. * Extra checks for validity. * Get categories directly. * Slight improvements over output. * Added NameValue types. * Fix typo. Remove some now unused diagnostics. Fix diagnostic in testing, as output has changed. * Add minimal usage message. * Remove platform executable extension from diagnostics output. * Some improvements around getting names from NameValue types. * Improve some option descriptions. * Small fixes. --- source/core/slang-command-options.h | 253 ++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 source/core/slang-command-options.h (limited to 'source/core/slang-command-options.h') diff --git a/source/core/slang-command-options.h b/source/core/slang-command-options.h new file mode 100644 index 000000000..a92d4d8f8 --- /dev/null +++ b/source/core/slang-command-options.h @@ -0,0 +1,253 @@ +#ifndef SLANG_CORE_COMMAND_OPTIONS_H +#define SLANG_CORE_COMMAND_OPTIONS_H + +#include "slang-basic.h" +#include "slang-string-slice-pool.h" +#include "slang-name-value.h" + +namespace Slang +{ + +/* For convenience we encode within "names" flags. +"-D..." means that -D *must* be followed by the value +"-D?..." means that -D *can* be a prefix, or it might be followed with the arg +*/ + +struct CommandOptions +{ + typedef uint32_t Flags; + + typedef int32_t UserIndex; + enum class UserValue : UserIndex; + static const UserValue kInvalidUserValue = UserValue(0x80000000); + + enum class LookupKind : int32_t + { + Category = -2, ///< Lookup a category name + Option = -1, ///< Lookup an option name (all options use the same lookup index even if in different categories) + Base = 0, ///< Lookup via category index + }; + + enum class CategoryKind + { + Option, ///< Command line option (like "-D") + Value, ///< One of a set of values (such as an enum or some other kind of list of values) + }; + + struct ValuePair + { + const char* name; + const char* description; + }; + + struct Category + { + UserValue userValue = kInvalidUserValue; + + CategoryKind kind; + UnownedStringSlice name; + UnownedStringSlice description; + + // Holds the span that defines all of the options associated with the category + Index optionStartIndex = 0; + Index optionEndIndex = 0; + }; + + struct Flag + { + enum Enum : Flags + { + CanPrefix = 0x1, /// Allows -Dfsggf or -D fdsfsd + IsPrefix = 0x2, /// Is an option that can only be a prefix + }; + }; + + struct Option + { + UnownedStringSlice names; ///< Comma delimited list of names, first name is the default + UnownedStringSlice usage; ///< Describes usage, can be empty + UnownedStringSlice description; ///< A description of usage + + UserValue userValue = kInvalidUserValue; + + Index categoryIndex = -1; ///< Category this option belongs to + Flags flags = 0; ///< Flags about this option + }; + + /// Add a category + Index addCategory(CategoryKind kind, const char* name, const char* description, UserValue userValue = kInvalidUserValue); + /// Use an already known category. It's an error if the category isn't found + void setCategory(const char* name); + + void add(const char* name, const char* usage, const char* description, UserValue userValue = kInvalidUserValue); + void add(const UnownedStringSlice* names, Count namesCount, const char* usage, const char* description, UserValue userValue = kInvalidUserValue, Flags flags = 0); + + void addValue(const UnownedStringSlice& name, UserValue userValue = kInvalidUserValue); + void addValue(const UnownedStringSlice& name, const UnownedStringSlice& description, UserValue userValue = kInvalidUserValue); + void addValue(const char* name, const char* description, UserValue userValue = kInvalidUserValue); + void addValue(const char* name, UserValue userValue = kInvalidUserValue); + void addValue(const UnownedStringSlice* names, Count namesCount, UserValue userValue = kInvalidUserValue); + + /// Add values (without UserValue association) + void addValues(const ValuePair* pairs, Count pairsCount); + + /// Add values + void addValues(const ConstArrayView& values); + void addValues(const ConstArrayView& values); + void addValues(const ConstArrayView& values); + + /// Sometimes values are listed with *names* per value. This method will take into account the aliases + void addValuesWithAliases(const ConstArrayView& values); + + /// Get the target index based off the name and the kind + Index findTargetIndexByName(LookupKind kind, const UnownedStringSlice& name) const; + /// Given a kind and a user value lookup the target index + Index findTargetIndexByUserValue(LookupKind kind, UserValue userValue) const; + + /// Finds the category by name or -1 if not found + Index findCategoryByName(const UnownedStringSlice& name) const { return findTargetIndexByName(LookupKind::Category, name); } + /// Finds the option index by name or -1 if not found + Index findOptionByName(const UnownedStringSlice& name) const; + /// Find the option index of a value, using it's category index and the name + Index findValueByName(Index categoryIndex, const UnownedStringSlice& name) const { return findTargetIndexByName(LookupKind(categoryIndex), name); } + + /// Get the category index from a user value + Index findCategoryByUserValue(UserValue userValue) const { return findTargetIndexByUserValue(LookupKind::Category, userValue); } + /// Can only get options + Index findOptionByUserValue(UserValue userValue) const { return findTargetIndexByUserValue(LookupKind::Option, userValue); } + /// Get a value associated with a category + Index findValueByUserValue(Index categoryIndex, UserValue userValue) const { return findTargetIndexByUserValue(LookupKind(categoryIndex), userValue); } + + /// Given a category user value, find the associated name + /// Returns -1 if not found + Index findOptionByCategoryUserValue(UserValue categoryUserValue, const UnownedStringSlice& name) const; + + /// Find a category by case insensitive name. Returns -1 if not found + Index findCategoryByCaseInsensitiveName(const UnownedStringSlice& slice) const; + + /// Given a category index returns all the options associated. + ConstArrayView