| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | Argument.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Declaration of the Argument class for command-line argument handling. |
| 12 | |
| 13 | --*/ |
| 14 | #pragma once |
| 15 | #include "ArgMap.h" |
| 16 | |
| 17 | #include <optional> |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 | |
| 21 | #define WSLC_CLI_ARG_ID_CHAR L'-' |
| 22 | #define WSLC_CLI_ARG_ID_STRING L"-" |
| 23 | #define WSLC_CLI_ARG_SPLIT_CHAR L'=' |
| 24 | #define WSLC_CLI_HELP_ARG L"?" |
| 25 | #define WSLC_CLI_HELP_ARG_STRING WSLC_CLI_ARG_ID_STRING WSLC_CLI_HELP_ARG |
| 26 | #define NO_ALIAS L"" |
| 27 | |
| 28 | using namespace wsl::windows::wslc::argument; |
| 29 | |
| 30 | namespace wsl::windows::wslc { |
| 31 | struct ArgumentOverrides |
| 32 | { |
| 33 | std::optional<std::wstring> Name; |
| 34 | std::optional<std::wstring> Alias; |
| 35 | std::optional<bool> Required; |
| 36 | std::optional<argument::Limit> Limit; |
| 37 | std::optional<std::wstring> Desc; |
| 38 | }; |
| 39 | |
| 40 | // An argument to a command. |
| 41 | struct Argument |
| 42 | { |
| 43 | // Default argument configuration constants |
| 44 | static constexpr Kind DefaultKind = Kind::Flag; |
| 45 | static constexpr bool DefaultRequired = false; |
| 46 | static constexpr argument::Limit DefaultLimit = argument::Limit::Single; |
| 47 | |
| 48 | // Full constructor with all parameters |
| 49 | Argument( |
| 50 | ArgType argType, |
| 51 | std::wstring name, |
| 52 | std::wstring alias, |
| 53 | std::wstring desc, |
| 54 | argument::Kind kind = DefaultKind, |
| 55 | bool required = DefaultRequired, |
| 56 | argument::Limit limit = DefaultLimit) : |
| 57 | m_argType(argType), m_name(std::move(name)), m_desc(std::move(desc)), m_alias(std::move(alias)), m_required(required), m_type(kind), m_limit(limit) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | Argument(const Argument&) = default; |
| 62 | Argument& operator=(const Argument&) = default; |
| 63 | |
| 64 | Argument(Argument&&) = default; |
| 65 | Argument& operator=(Argument&&) = default; |
| 66 | |
| 67 | // Creates an argument using its table defaults and any command-specific overrides. |
| 68 | static Argument Create(ArgType type, ArgumentOverrides overrides = {}); |
| 69 | |
| 70 | // Gets the argument usage string in the format of "-alias,--name" or just "--name" if no alias. |
| 71 | std::wstring GetUsageString() const; |
| 72 | |
| 73 | // Arguments are not localized, but the description is. |
| 74 | const std::wstring& Name() const |
| 75 | { |
| 76 | return m_name; |
| 77 | } |
| 78 | const std::wstring& Alias() const |
| 79 | { |
| 80 | return m_alias; |
| 81 | } |
| 82 | const std::wstring& Description() const |
| 83 | { |
| 84 | return m_desc; |
| 85 | } |
| 86 | bool Required() const |
| 87 | { |
| 88 | return m_required; |
| 89 | } |
| 90 | ArgType Type() const |
| 91 | { |
| 92 | return m_argType; |
| 93 | } |
| 94 | Kind Kind() const |
| 95 | { |
| 96 | return m_type; |
| 97 | } |
| 98 | bool IsOption() const |
| 99 | { |
| 100 | return m_type == argument::Kind::Flag || m_type == argument::Kind::Value; |
| 101 | } |
| 102 | Limit Limit() const |
| 103 | { |
| 104 | return m_limit; |
| 105 | } |
| 106 | |
| 107 | // A single-value argument accepts one value (last-wins on repeats). |
| 108 | bool IsSingle() const |
| 109 | { |
| 110 | return m_limit == argument::Limit::Single; |
| 111 | } |
| 112 | |
| 113 | // An unlimited argument accumulates every value supplied. |
| 114 | bool IsUnlimited() const |
| 115 | { |
| 116 | return m_limit == argument::Limit::Unlimited; |
| 117 | } |
| 118 | |
| 119 | // Validates this argument's current values, caching the converted result (converted arguments |
| 120 | // only) on `execArgs` so reads reuse it without re-parsing until the raw values change. |
| 121 | void Validate(ArgMap& execArgs) const; |
| 122 | |
| 123 | private: |
| 124 | ArgType m_argType; |
| 125 | std::wstring m_name; |
| 126 | std::wstring m_desc; |
| 127 | std::wstring m_alias; |
| 128 | bool m_required = DefaultRequired; |
| 129 | argument::Kind m_type = DefaultKind; |
| 130 | argument::Limit m_limit = DefaultLimit; |
| 131 | }; |
| 132 | } // namespace wsl::windows::wslc |