6 using System.Text.RegularExpressions;
210 public GetOpt(
string[] appArgs,
string shortOpts, params
Option[] options) {
231 [GeneratedRegex(
ArgSplitRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled)]
312 if (
Options.Length == 0) {
throw new ParseException(
"Cannot parse long option! No option list provided!"); }
337 if (optArg is
null) {
341 if (
bool.TryParse(optArg, out
bool outBool)) {
343 }
else if (
double.TryParse(optArg, out
double outDouble)) {
345 }
else if (
float.TryParse(optArg, out
float outFloat)) {
347 }
else if (
int.TryParse(optArg, out
int outInt)) {
372 if (nullableOpt is
null) {
381 var opt = (
Option)nullableOpt;
382 switch (opt.ArgumentType) {
391 }
else if (optArg !=
null) {
break; }
427 incrementCurrentIndex =
false;
431 incrementCurrentIndex =
true;
464 bool incrementCurrentIndex =
false;
466 if (argType is
null) {
490 else {
throw new ParseException(curOpt.ToString(),
"Missing argument for option!"); }
510 var posInStr =
ShortOpts.IndexOf(shortOpt);
511 if (posInStr == -1) {
524 switch (charToCheck) {
542 throw new ParseException(shortOpt.ToString(),
"Invalid option list!");
545 var nullableOpt =
Options.FindOptionOrDefault(shortOpt);
547 if (nullableOpt ==
null) {
551 }
else {
throw new ParseException(shortOpt.ToString(),
"Encountered unknown option!"); }
554 var opt = (
Option)nullableOpt;
566 var splitString = Array.Empty<
string>();
572 if (indexOfSeparator != -1) {
573 curArg = $
"{ curArg.Substring(0, indexOfSeparator) }{ GnuArgSeparator }{ curArg.Substring(indexOfSeparator + 1) }";
579 if (splitString.Length == 1) {
585 optName = splitString[0];
587 argVal =
string.Join(
"", splitString[2..]);
589 argVal =
string.Join(
"", splitString.Skip(2));
607 return curArg.Substring(1);
615 if (isLongOpt && curArg.StartsWith(
DoubleDash)) {
616 return curArg.Substring(2);
617 }
else if (curArg.StartsWith(
SingleDash.ToString())) {
618 return curArg.Substring(1);
630 return !
string.IsNullOrEmpty(arg) &&
631 arg.Equals(
"--", StringComparison.CurrentCultureIgnoreCase);
640 if (
string.IsNullOrEmpty(arg)) {
return false; }
663 return arg.Length > 2 &&
674 if (
string.IsNullOrEmpty(arg)) {
return false; }
683 return arg.Length > 1 &&
701 if (
string.IsNullOrEmpty(arg) || !
AllowParamFiles || arg.Length < 2) {
return false; }
705 arg = arg.TrimStart(
'@');
707 if (File.Exists(arg)) {
719 if (paramFile ==
null || !paramFile.Exists) {
return; }
721 var lastIndex =
AppArgs.Length;
722 var lines = File.ReadAllLines(paramFile.FullName);
725 for (
int i = lastIndex, j = 0; i < m_appArgs.Length && j < lines.Length; i++, j++) {
726 if (
string.IsNullOrEmpty(lines[j].Trim())) {
continue; }
727 if (lines[j].Trim()[0] ==
'#') {
continue; }
Represents a single argument received via command-line options.
GetOpt-like class for handling getopt-like command-line arguments in .net.
const char GnuArgSeparator
The argument separator used by POSIX / GNU getopt.
bool TryGetArgumentForShortOption(ref string? arg, out bool incrementCurrentIndex)
Attempts to retrieve the argument for the current short option.
bool ShallStopParsing(ref string arg)
Gets a value indicating whether or not the parser shall stop here.
static Regex ArgumentSplitter()
Compiled Regex.
bool IsParamFileArg(string arg, out string? paramFile)
Gets a value indicating whether or not a given option is a paramfile option. AllowParamFiles.
bool AllExceptionsDisabled
Either enables or disabled exceptions entirely. For more specific control over exceptions,...
bool HasArgumentInOption(out string optName, out string? argVal)
Determines whether or not the current option contains its argument with the string or not.
bool OnlyShortOpts
Gets or sets a value indicating whether or not to only parse short options. Default:
bool MustStopParsing()
If the first character of ShortOpts is '+' or the environment variable POSIXLY_CORRECT is set,...
bool IsShortOption(string arg)
Gets a value indicating whether or not the current string is/contains a (or more) short option
string StripDashes(bool isLongOpt)
Strips leading dashes from strings.
const char SingleSlash
A single slash. This is the char that is searched for when parsing arguments with the Windows convent...
GetOpt()
Default constructor; it is recommended to use this constructor and to use brace-initialiser-lists to ...
bool DoubleDashStopsParsing
Gets or sets a value indicating whether or not "--" stops parsing. Default:
CommandOption GetNextOpt()
Gets the next option in the list, returning an object containing more detailled information about the...
Option[] Options
An optional list of long options to go with the short options.
const char WinArgSeparator
The argument separator used by Windows.
ArgumentType? ShortOptRequiresArg(char shortOpt)
Gets a value indicating whether or not a short option requires an argument.
bool AllowParamFiles
Gets or sets a value indicating whether or not parameter files are accepted as a valid form of input.
void ReadParamFile(FileInfo paramFile)
Reads the incoming param file and adds the contents to AppArgs
bool IgnoreEmptyOptions
Gets or sets a value indicating whether or not to ignore empty values. Default:
bool IgnoreInvalidOptions
Gets or sets a value indicating whether or not invalid arguments should be ignored or not....
const char NonOptChar
The character that is returned when a non-option value is encountered and it is not the argument to a...
int ParseLongOption(out string? optArg)
Parses long options
int ParseShortOption(out string? optArg)
Parses a single short option.
int m_currentIndex
The current index while traversing AppArgs
const string ArgSplitRegex
The regex used by ArgumentSplitter to split arguments into a key-value pair.
bool IgnoreMissingArgument
Gets or sets a value indicating whether or not to ignore missing arguments. Default:
string[] AppArgs
Gets or sets the arguments to parse.
bool StopParsingOptions
Gets or sets a value indicating whether or not option parsing shall stop or not. Default:
bool AllowWindowsConventions
Gets or sets a value indicating whether or not Windows argument conventions are allowed....
bool AllowPowershellConventions
Gets or sets a value indicating whether or not Powershell-style arguments are allowed....
const char SingleAtSymbol
A single "at" character. This character is used when AllowParamFiles is enabled, to determine whether...
const char MissingArgChar
The character that is returned when an option is missing a required argument.
bool IgnoreEmptyAppArgs
Gets or sets a value indicating whether or not empty AppArgs are ignored or throw an exception....
const char SingleDash
A single dash character. This is the character that is searched for, when parsing POSIX-/GNU-like opt...
const char InvalidOptChar
The character that is returned when an invalid option is returned.
void ResetOptPosition()
Resets the option position to 1.
string? ShortOpts
The short opts to use.
int CurrentIndex
Gets the current index of the app arguments being parsed.
const string DoubleDash
This is the string getopt.net looks for when DoubleDashStopsParsing is enabled.
bool IsLongOption(string arg)
Gets a value indicating whether or not the current string is a long option.
bool MustReturnChar1()
Gets a value indicating whether or not non-options should be handled as if they were the argument of ...
int m_optPosition
The current position in a multi-option string such as "-xvzRf" when parsing short options.
GetOpt(string[] appArgs, string shortOpts, params Option[] options)
Specialised constructor.
int GetNextOpt(out string? outOptArg)
Gets the next option in the list.
Generic exception class that is thrown when the parser is not configured to ignore errors.
ArgumentType
Enumeration containing the argument types possible for getopt.
Represents a single long option for getopt.