From d7930e4a4692aac1912cfe0c6715d62e5b702a1d Mon Sep 17 00:00:00 2001 From: Simon Cahill Date: Sun, 20 Aug 2023 21:46:59 +0200 Subject: [PATCH] Began implementing automatic help text generation --- getopt.net.tests/getopt.net.tests.csproj | 8 +- getopt.net/GetOpt.cs | 93 +++++++++++++----------- getopt.net/Option.cs | 22 ++++-- getopt.net/{ => enums}/ArgumentType.cs | 0 getopt.net/enums/OptionConvention.cs | 25 +++++++ getopt.net/enums/TextAlignment.cs | 20 +++++ getopt.net/getopt.net.csproj | 6 ++ 7 files changed, 122 insertions(+), 52 deletions(-) rename getopt.net/{ => enums}/ArgumentType.cs (100%) create mode 100644 getopt.net/enums/OptionConvention.cs create mode 100644 getopt.net/enums/TextAlignment.cs diff --git a/getopt.net.tests/getopt.net.tests.csproj b/getopt.net.tests/getopt.net.tests.csproj index 5c938a7..dc59e1a 100644 --- a/getopt.net.tests/getopt.net.tests.csproj +++ b/getopt.net.tests/getopt.net.tests.csproj @@ -9,10 +9,10 @@ - - - - runtime; build; native; contentfiles; analyzers; buildtransitive + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/getopt.net/GetOpt.cs b/getopt.net/GetOpt.cs index bc1ef81..e2d97ac 100644 --- a/getopt.net/GetOpt.cs +++ b/getopt.net/GetOpt.cs @@ -4,7 +4,7 @@ namespace getopt.net { using System.IO; using System.Text.RegularExpressions; - + /// /// GetOpt-like class for handling getopt-like command-line arguments in .net. /// @@ -61,9 +61,9 @@ public partial class GetOpt { /// public const string ArgSplitRegex = @"([\s]|[=])"; - /// - /// A single "at" character. - /// This character is used when is enabled, to determine whether or not a param file has been passed to getopt.net. + /// + /// A single "at" character. + /// This character is used when is enabled, to determine whether or not a param file has been passed to getopt.net. /// public const char SingleAtSymbol = '@'; @@ -148,9 +148,9 @@ public string[] AppArgs { /// public bool AllowWindowsConventions { get; set; } = false; - /// - /// Gets or sets a value indicating whether or not Powershell-style arguments are allowed. - /// This option doesn't conflict with the GNU/POSIX or Windows-style argument parsing and is simply an addition. + /// + /// Gets or sets a value indicating whether or not Powershell-style arguments are allowed. + /// This option doesn't conflict with the GNU/POSIX or Windows-style argument parsing and is simply an addition. /// /// /// This option is disabled by default. @@ -159,11 +159,11 @@ public string[] AppArgs { /// public bool AllowPowershellConventions { get; set; } = false; - /// - /// Gets or sets a value indicating whether or not parameter files are accepted as a valid form of input. - /// - /// Parameter files are known from some software, such as GCC. - /// A param file can be passed as @/path/to/file. + /// + /// Gets or sets a value indicating whether or not parameter files are accepted as a valid form of input. + /// + /// Parameter files are known from some software, such as GCC. + /// A param file can be passed as @/path/to/file. /// /// /// Param files must follow certain rules, in order to be accepted by getopt.net. @@ -661,47 +661,54 @@ protected bool IsShortOption(string arg) { arg[1] != SingleDash; } - /// - /// Gets a value indicating whether or not a given option is a paramfile option. - /// . - /// - /// - /// If is a valid paramfile option, but the file doesn't exist, will be set to null. - /// Otherwise, will be set to the path to the file. - /// - /// The argument to check. - /// An out param containing either null or the path to the file. + /// + /// Gets a value indicating whether or not a given option is a paramfile option. + /// . + /// + /// + /// If is a valid paramfile option, but the file doesn't exist, will be set to null. + /// Otherwise, will be set to the path to the file. + /// + /// The argument to check. + /// An out param containing either null or the path to the file. /// true if the passed argument is a valid paramfile option. false otherwise. - protected bool IsParamFileArg(string arg, out string? paramFile) { - paramFile = null; // pre-set this so we don't have to do it everywhere - if (string.IsNullOrEmpty(arg) || !AllowParamFiles || arg.Length < 2) { return false; } - - if (arg[0] != SingleAtSymbol) { return false; } - - arg = arg.TrimStart('@'); - - if (File.Exists(arg)) { - paramFile = arg; - } - - return true; + protected bool IsParamFileArg(string arg, out string? paramFile) { + paramFile = null; // pre-set this so we don't have to do it everywhere + if (string.IsNullOrEmpty(arg) || !AllowParamFiles || arg.Length < 2) { return false; } + + if (arg[0] != SingleAtSymbol) { return false; } + + arg = arg.TrimStart('@'); + + if (File.Exists(arg)) { + paramFile = arg; + } + + return true; } /// /// Reads the incoming param file and adds the contents to /// /// The file to read. - protected void ReadParamFile(FileInfo paramFile) { - if (paramFile == null || !paramFile.Exists) { return; } - - var lastIndex = AppArgs.Length; - var lines = File.ReadAllLines(paramFile.FullName); - Array.Resize(ref m_appArgs, lines.Length + AppArgs.Length); - + protected void ReadParamFile(FileInfo paramFile) { + if (paramFile == null || !paramFile.Exists) { return; } + + var lastIndex = AppArgs.Length; + var lines = File.ReadAllLines(paramFile.FullName); + Array.Resize(ref m_appArgs, lines.Length + AppArgs.Length); + for (int i = lastIndex, j = 0; i < m_appArgs.Length && j < lines.Length; i++, j++) { m_appArgs[i] = lines[j]; - } + } } + + /// + /// Gets a object with all currently configured + /// + /// A new instance. + public DescriptionGenerator GetDescriptionGenerator() => new DescriptionGenerator(Options); } + } diff --git a/getopt.net/Option.cs b/getopt.net/Option.cs index 12c4d03..ef37b42 100644 --- a/getopt.net/Option.cs +++ b/getopt.net/Option.cs @@ -18,18 +18,21 @@ public Option() { } /// The name of the argument. /// The argument type. /// The value of the option. - public Option(string name, ArgumentType argType, char value): this(name, argType, (int)value) { } + /// A brief description of the parameter. + public Option(string name, ArgumentType argType, char value, string? description = null): this(name, argType, (int)value, description) { } /// /// Constructs a new instance of this struct with an as the value type. /// - /// - /// - /// - public Option(string name, ArgumentType argType, int value) { + /// The name of the argument. + /// The argument type. + /// The value of the option. + /// A brief description of the parameter. + public Option(string name, ArgumentType argType, int value, string? description = null) { Name = name; ArgumentType = argType; Value = value; + Description = description; } /// @@ -53,6 +56,15 @@ public Option(string name, ArgumentType argType, int value) { /// public int Value { get; set; } + /// + /// Gets or sets the description for this option. + /// + /// This property can be used by the to generate automatic descriptions for your + /// application. + /// + /// The description for this option. + public string? Description { get; set; } + } } diff --git a/getopt.net/ArgumentType.cs b/getopt.net/enums/ArgumentType.cs similarity index 100% rename from getopt.net/ArgumentType.cs rename to getopt.net/enums/ArgumentType.cs diff --git a/getopt.net/enums/OptionConvention.cs b/getopt.net/enums/OptionConvention.cs new file mode 100644 index 0000000..578851b --- /dev/null +++ b/getopt.net/enums/OptionConvention.cs @@ -0,0 +1,25 @@ +using System; + +namespace getopt.net { + + /// + /// An enumeration of all supported option conventions supported by the class. + /// + public enum OptionConvention { + /// + /// Print the help text using options formatted with POSIX/GNU conventions. + /// + PosixGnuConvention = 0, + + /// + /// Print the help text using options formatted with Windows conventions. + /// + WindowsConvention = 1, + + /// + /// Print the help text using options formatted with Powershell conventions. + /// + PowershellConvention = 2 + } +} + diff --git a/getopt.net/enums/TextAlignment.cs b/getopt.net/enums/TextAlignment.cs new file mode 100644 index 0000000..11521b0 --- /dev/null +++ b/getopt.net/enums/TextAlignment.cs @@ -0,0 +1,20 @@ +using System; + +namespace getopt.net { + + /// + /// A simple enumeration for text alignment strategies for generating description texts. + /// + public enum TextAlignment { + + LeftAligned = 0, + + RightAligned = 1, + + Centred = 2, + + Centered = 2, + + } +} + diff --git a/getopt.net/getopt.net.csproj b/getopt.net/getopt.net.csproj index 3ab5ec9..544cda6 100644 --- a/getopt.net/getopt.net.csproj +++ b/getopt.net/getopt.net.csproj @@ -47,6 +47,9 @@ Changes 4 true + + + @@ -68,4 +71,7 @@ Changes + + +