getopt.net
A port of getopt in pure C#.
|
GetOpt-like class for handling getopt-like command-line arguments in .net. More...
Public Member Functions | |
GetOpt () | |
Default constructor; it is recommended to use this constructor and to use brace-initialiser-lists to instantiate/configure each instance of this object. | |
GetOpt (string[] appArgs, string shortOpts, params Option[] options) | |
Specialised constructor. | |
int | GetNextOpt (out string? outOptArg) |
Gets the next option in the list. | |
CommandOption | GetNextOpt () |
Gets the next option in the list, returning an object containing more detailled information about the option. | |
Static Public Attributes | |
const char | MissingArgChar = '?' |
The character that is returned when an option is missing a required argument. | |
const char | InvalidOptChar = '!' |
The character that is returned when an invalid option is returned. | |
const char | NonOptChar = (char)1 |
The character that is returned when a non-option value is encountered and it is not the argument to an option. | |
const string | DoubleDash = "--" |
This is the string getopt.net looks for when DoubleDashStopsParsing is enabled. | |
const char | SingleDash = '-' |
A single dash character. This is the character that is searched for, when parsing POSIX-/GNU-like options. | |
const char | SingleSlash = '/' |
A single slash. This is the char that is searched for when parsing arguments with the Windows convention. | |
const char | WinArgSeparator = ':' |
The argument separator used by Windows. | |
const char | GnuArgSeparator = '=' |
The argument separator used by POSIX / GNU getopt. | |
const string | ArgSplitRegex = @"([\s]|[=])" |
The regex used by ArgumentSplitter to split arguments into a key-value pair. | |
const char | SingleAtSymbol = '@' |
A single "at" character. This character is used when AllowParamFiles is enabled, to determine whether or not a param file has been passed to getopt.net. | |
Protected Member Functions | |
void | ResetOptPosition () |
Resets the option position to 1. | |
bool | MustReturnChar1 () |
Gets a value indicating whether or not non-options should be handled as if they were the argument of an option with the character code 1. | |
bool | MustStopParsing () |
If the first character of ShortOpts is '+' or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a nonoption argument is encountered. | |
int | ParseLongOption (out string? optArg) |
Parses long options. | |
bool | TryGetArgumentForShortOption (ref string? arg, out bool incrementCurrentIndex) |
Attempts to retrieve the argument for the current short option. | |
int | ParseShortOption (out string? optArg) |
Parses a single short option. | |
ArgumentType? | ShortOptRequiresArg (char shortOpt) |
Gets a value indicating whether or not a short option requires an argument. | |
bool | HasArgumentInOption (out string optName, out string? argVal) |
Determines whether or not the current option contains its argument with the string or not. | |
string | StripDashes (bool isLongOpt) |
Strips leading dashes from strings. | |
bool | IsLongOption (string arg) |
Gets a value indicating whether or not the current string is a long option. | |
bool | IsShortOption (string arg) |
Gets a value indicating whether or not the current string is/contains a (or more) short option. | |
bool | IsParamFileArg (string arg, out string? paramFile) |
Gets a value indicating whether or not a given option is a paramfile option. AllowParamFiles. | |
void | ReadParamFile (FileInfo paramFile) |
Reads the incoming param file and adds the contents to AppArgs | |
Static Protected Member Functions | |
static Regex | ArgumentSplitter () |
Compiled Regex. | |
static bool | ShallStopParsing (ref string arg) |
Gets a value indicating whether or not the parser shall stop here. | |
Protected Attributes | |
int | m_currentIndex = 0 |
The current index while traversing AppArgs | |
int | m_optPosition = 1 |
The current position in a multi-option string such as "-xvzRf" when parsing short options. | |
Properties | |
Option[] | Options = Array.Empty<Option>() [get, set] |
An optional list of long options to go with the short options. | |
string? | ShortOpts = null [get, set] |
The short opts to use. | |
bool | DoubleDashStopsParsing = true [get, set] |
Gets or sets a value indicating whether or not "--" stops parsing. Default: | |
string[] | AppArgs [get, set] |
Gets or sets the arguments to parse. | |
bool | OnlyShortOpts = false [get, set] |
Gets or sets a value indicating whether or not to only parse short options. Default: | |
bool | IgnoreEmptyOptions = true [get, set] |
Gets or sets a value indicating whether or not to ignore empty values. Default: | |
bool | IgnoreMissingArgument = false [get, set] |
Gets or sets a value indicating whether or not to ignore missing arguments. Default: | |
bool | IgnoreInvalidOptions = true [get, set] |
Gets or sets a value indicating whether or not invalid arguments should be ignored or not. Default: | |
bool | IgnoreEmptyAppArgs = true [get, set] |
Gets or sets a value indicating whether or not empty AppArgs are ignored or throw an exception. Default: | |
bool | StopParsingOptions = false [get, set] |
Gets or sets a value indicating whether or not option parsing shall stop or not. Default: | |
bool | AllowWindowsConventions = false [get, set] |
Gets or sets a value indicating whether or not Windows argument conventions are allowed. Default: | |
bool | AllowPowershellConventions = false [get, set] |
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. | |
bool | AllowParamFiles = false [get, set] |
Gets or sets a value indicating whether or not parameter files are accepted as a valid form of input. | |
bool | AllExceptionsDisabled [get, set] |
Either enables or disabled exceptions entirely. For more specific control over exceptions, see the other options provided by GetOpt. | |
int | CurrentIndex [get] |
Gets the current index of the app arguments being parsed. | |
Private Attributes | |
string[] | m_appArgs = Array.Empty<string>() |
GetOpt-like class for handling getopt-like command-line arguments in .net.
This class contains all the properties and logic required for parsing getopt-like command-line arguments.
A detailled description of the usage of this class can be found in the .
|
inline |
|
inline |
Specialised constructor.
appArgs | The arguments passed to the application. |
shortOpts | |
options |
Definition at line 209 of file GetOpt.cs.
References getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.Options, and getopt.net.GetOpt.ShortOpts.
|
staticprotected |
Compiled Regex.
Referenced by getopt.net.GetOpt.HasArgumentInOption().
|
inline |
Gets the next option in the list, returning an object containing more detailled information about the option.
Definition at line 333 of file GetOpt.cs.
References getopt.net.GetOpt.GetNextOpt().
Referenced by getopt.net.GetOpt.GetNextOpt(), and getopt.net.GetOpt.GetNextOpt().
|
inline |
Gets the next option in the list.
outOptArg | Out var; the argument for the option (if applicable). |
ParseException | If ignoring errors is disabled (default) and an error occurs. |
Definition at line 271 of file GetOpt.cs.
References getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.CurrentIndex, getopt.net.GetOpt.DoubleDash, getopt.net.GetOpt.DoubleDashStopsParsing, getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.IgnoreEmptyAppArgs, getopt.net.GetOpt.IgnoreEmptyOptions, getopt.net.GetOpt.IgnoreInvalidOptions, getopt.net.GetOpt.InvalidOptChar, getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.IsParamFileArg(), getopt.net.GetOpt.IsShortOption(), getopt.net.GetOpt.m_currentIndex, getopt.net.GetOpt.MustReturnChar1(), getopt.net.GetOpt.NonOptChar, getopt.net.GetOpt.Options, getopt.net.GetOpt.ParseLongOption(), getopt.net.GetOpt.ParseShortOption(), getopt.net.GetOpt.ReadParamFile(), getopt.net.GetOpt.ShortOpts, and getopt.net.GetOpt.StopParsingOptions.
|
inlineprotected |
Determines whether or not the current option contains its argument with the string or not.
optName | Out var; the name of the option |
argVal | Out var; the value of the argument |
Definition at line 561 of file GetOpt.cs.
References getopt.net.GetOpt.AllowWindowsConventions, getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.ArgumentSplitter(), getopt.net.GetOpt.CurrentIndex, getopt.net.GetOpt.StripDashes(), and getopt.net.GetOpt.WinArgSeparator.
Referenced by getopt.net.GetOpt.ParseLongOption().
|
inlineprotected |
Gets a value indicating whether or not the current string is a long option.
arg | The string to check. |
Definition at line 636 of file GetOpt.cs.
References getopt.net.GetOpt.AllowPowershellConventions, getopt.net.GetOpt.AllowWindowsConventions, getopt.net.GetOpt.GnuArgSeparator, getopt.net.GetOpt.Options, getopt.net.GetOpt.SingleDash, getopt.net.GetOpt.SingleSlash, and getopt.net.GetOpt.WinArgSeparator.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
inlineprotected |
Gets a value indicating whether or not a given option is a paramfile option. AllowParamFiles.
If arg is a valid paramfile option, but the file doesn't exist, paramFile will be set to
. Otherwise, paramFile will be set to the path to the file.
arg | The argument to check. |
paramFile | An out param containing either null or the path to the file. |
Definition at line 696 of file GetOpt.cs.
References getopt.net.GetOpt.AllowParamFiles, and getopt.net.GetOpt.SingleAtSymbol.
Referenced by getopt.net.GetOpt.GetNextOpt().
|
inlineprotected |
Gets a value indicating whether or not the current string is/contains a (or more) short option.
arg | The string to check. |
Definition at line 670 of file GetOpt.cs.
References getopt.net.GetOpt.AllowWindowsConventions, getopt.net.GetOpt.SingleDash, and getopt.net.GetOpt.SingleSlash.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
protected |
Gets a value indicating whether or not non-options should be handled as if they were the argument of an option with the character code 1.
From the getopt man page:
If the first character of optstring is '-', then each nonoption argv-element is handled as if it were the argument of an option with character code 1. (This is used by programs that were written to expect options and other argv-elements in any order and that care about the ordering of the two.)
Referenced by getopt.net.GetOpt.GetNextOpt().
|
protected |
If the first character of ShortOpts is '+' or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a nonoption argument is encountered.
Referenced by getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
inlineprotected |
Parses long options.
optArg | Out var; the option's argument (if applicable) |
ParseException | If ignoring of errors is disabled and a parsing error occurs. |
Definition at line 363 of file GetOpt.cs.
References getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.CurrentIndex, getopt.net.GetOpt.HasArgumentInOption(), getopt.net.GetOpt.IgnoreInvalidOptions, getopt.net.GetOpt.IgnoreMissingArgument, getopt.net.GetOpt.InvalidOptChar, getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.IsShortOption(), getopt.net.GetOpt.m_currentIndex, getopt.net.GetOpt.MissingArgChar, getopt.net.GetOpt.MustStopParsing(), getopt.net.GetOpt.Options, and getopt.net.GetOpt.StripDashes().
Referenced by getopt.net.GetOpt.GetNextOpt().
|
inlineprotected |
Parses a single short option.
optArg | Out var; the argument required for the option. |
ParseException | If ignoring of errors is disabled (default) will throw a ParseException if an error occurs. |
Definition at line 459 of file GetOpt.cs.
References getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.CurrentIndex, getopt.net.GetOpt.IgnoreMissingArgument, getopt.net.GetOpt.InvalidOptChar, getopt.net.GetOpt.m_currentIndex, getopt.net.GetOpt.m_optPosition, getopt.net.GetOpt.MissingArgChar, getopt.net.GetOpt.ResetOptPosition(), getopt.net.GetOpt.ShortOptRequiresArg(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
Referenced by getopt.net.GetOpt.GetNextOpt().
|
inlineprotected |
Reads the incoming param file and adds the contents to AppArgs
paramFile | The file to read. |
Definition at line 715 of file GetOpt.cs.
References getopt.net.GetOpt.AppArgs, and getopt.net.GetOpt.m_appArgs.
Referenced by getopt.net.GetOpt.GetNextOpt().
|
protected |
Resets the option position to 1.
Referenced by getopt.net.GetOpt.ParseShortOption().
|
inlinestaticprotected |
Gets a value indicating whether or not the parser shall stop here.
arg | The arg to check |
Definition at line 626 of file GetOpt.cs.
|
inlineprotected |
Gets a value indicating whether or not a short option requires an argument.
shortOpt | The opt to check for. |
ParseException | If ignoring errors is disabled (default) and an error occurs during parsing. |
Definition at line 506 of file GetOpt.cs.
References getopt.net.GetOpt.IgnoreInvalidOptions, getopt.net.GetOpt.Options, and getopt.net.GetOpt.ShortOpts.
Referenced by getopt.net.GetOpt.ParseShortOption().
|
inlineprotected |
Strips leading dashes from strings.
If AllowWindowsConventions is enabled, then this method will also strip leading slashes!
isLongOpt | Whether or not the current option is a long option. |
Definition at line 599 of file GetOpt.cs.
References getopt.net.GetOpt.AllowWindowsConventions, getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.DoubleDash, getopt.net.GetOpt.m_currentIndex, getopt.net.GetOpt.SingleDash, and getopt.net.GetOpt.SingleSlash.
Referenced by getopt.net.GetOpt.HasArgumentInOption(), and getopt.net.GetOpt.ParseLongOption().
|
inlineprotected |
Attempts to retrieve the argument for the current short option.
This is only a helper method to keep the code cleaner.
arg | A reference to the current optArg parameter in ParseShortOption(out string?) |
incrementCurrentIndex | Whether or not to increment m_currentIndex |
Definition at line 425 of file GetOpt.cs.
References getopt.net.GetOpt.AppArgs, getopt.net.GetOpt.CurrentIndex, getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.IsShortOption(), getopt.net.GetOpt.m_currentIndex, getopt.net.GetOpt.m_optPosition, and getopt.net.GetOpt.MustStopParsing().
Referenced by getopt.net.GetOpt.ParseShortOption().
|
static |
|
static |
This is the string getopt.net looks for when DoubleDashStopsParsing is enabled.
Definition at line 35 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), and getopt.net.GetOpt.StripDashes().
|
static |
The argument separator used by POSIX / GNU getopt.
Definition at line 57 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.IsLongOption().
|
static |
The character that is returned when an invalid option is returned.
Definition at line 25 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.ParseShortOption().
|
private |
Definition at line 86 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.ReadParamFile().
|
protected |
The current index while traversing AppArgs
Definition at line 218 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.ParseLongOption(), getopt.net.GetOpt.ParseShortOption(), getopt.net.GetOpt.StripDashes(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
protected |
The current position in a multi-option string such as "-xvzRf" when parsing short options.
Definition at line 223 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.ParseShortOption(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
static |
The character that is returned when an option is missing a required argument.
Definition at line 20 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.ParseShortOption().
|
static |
The character that is returned when a non-option value is encountered and it is not the argument to an option.
Definition at line 30 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt().
|
static |
A single "at" character. This character is used when AllowParamFiles is enabled, to determine whether or not a param file has been passed to getopt.net.
Definition at line 68 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.IsParamFileArg().
|
static |
A single dash character. This is the character that is searched for, when parsing POSIX-/GNU-like options.
Definition at line 41 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.IsShortOption(), and getopt.net.GetOpt.StripDashes().
|
static |
A single slash. This is the char that is searched for when parsing arguments with the Windows convention.
Definition at line 47 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.IsShortOption(), and getopt.net.GetOpt.StripDashes().
|
static |
The argument separator used by Windows.
Definition at line 52 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.HasArgumentInOption(), and getopt.net.GetOpt.IsLongOption().
|
getset |
Either enables or disabled exceptions entirely. For more specific control over exceptions, see the other options provided by GetOpt.
true
if exceptions are enabled, false
otherwise.
Definition at line 185 of file GetOpt.cs.
|
getset |
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.
@/path/to/file
) may be added in addition to any other arguments! Definition at line 178 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.IsParamFileArg().
|
getset |
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.
Powershell-style arguments are similar to GNU/POSIX long options, however they begin with a single dash (-).
Definition at line 160 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.IsLongOption().
|
getset |
Gets or sets a value indicating whether or not Windows argument conventions are allowed. Default:
false
By convention, Windows-like options begin with a slash (/). Options with arguments are separated by a colon ':'.
Definition at line 149 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.HasArgumentInOption(), getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.IsShortOption(), and getopt.net.GetOpt.StripDashes().
|
getset |
Gets or sets the arguments to parse.
Definition at line 91 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.GetOpt(), getopt.net.GetOpt.HasArgumentInOption(), getopt.net.GetOpt.ParseLongOption(), getopt.net.GetOpt.ParseShortOption(), getopt.net.GetOpt.ReadParamFile(), getopt.net.GetOpt.StripDashes(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
get |
Gets the current index of the app arguments being parsed.
Definition at line 195 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.HasArgumentInOption(), getopt.net.GetOpt.ParseLongOption(), getopt.net.GetOpt.ParseShortOption(), and getopt.net.GetOpt.TryGetArgumentForShortOption().
|
getset |
Gets or sets a value indicating whether or not "--" stops parsing. Default:
true
Definition at line 84 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt().
|
getset |
Gets or sets a value indicating whether or not empty AppArgs are ignored or throw an exception. Default:
true
Definition at line 130 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt().
|
getset |
Gets or sets a value indicating whether or not to ignore empty values. Default:
true
Definition at line 106 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt().
|
getset |
Gets or sets a value indicating whether or not invalid arguments should be ignored or not. Default:
true
If this is set to
and an invalid argument is found, then '!' will be returned.
Definition at line 124 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.ShortOptRequiresArg().
|
getset |
Gets or sets a value indicating whether or not to ignore missing arguments. Default:
false
If this is set to
and a required argument is missing, '?' will be returned.
Definition at line 115 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.ParseShortOption().
|
getset |
An optional list of long options to go with the short options.
Definition at line 73 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.GetOpt(), getopt.net.GetOpt.IsLongOption(), getopt.net.GetOpt.ParseLongOption(), and getopt.net.GetOpt.ShortOptRequiresArg().
|
getset |
The short opts to use.
Definition at line 78 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt(), getopt.net.GetOpt.GetOpt(), and getopt.net.GetOpt.ShortOptRequiresArg().
|
getset |
Gets or sets a value indicating whether or not option parsing shall stop or not. Default:
false
When this is set to
, all remaining arguments in AppArgs will be returned without being parsed.
Definition at line 139 of file GetOpt.cs.
Referenced by getopt.net.GetOpt.GetNextOpt().