6 using System.Text.RegularExpressions;
209 public GetOpt(
string[] appArgs,
string shortOpts, params
Option[] options) {
230 [GeneratedRegex(
ArgSplitRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled)]
288 }
else {
return -1; }
311 if (
Options.Length == 0) {
throw new ParseException(
"Cannot parse long option! No option list provided!"); }
336 if (optArg is
null) {
340 if (
bool.TryParse(optArg, out
bool outBool)) {
342 }
else if (
double.TryParse(optArg, out
double outDouble)) {
344 }
else if (
float.TryParse(optArg, out
float outFloat)) {
346 }
else if (
int.TryParse(optArg, out
int outInt)) {
371 if (nullableOpt is
null) {
381 var opt = (
Option)nullableOpt;
382 switch (opt.ArgumentType) {
391 }
else if (optArg !=
null) {
break; }
426 incrementCurrentIndex =
false;
430 incrementCurrentIndex =
true;
463 bool incrementCurrentIndex =
false;
465 if (argType is
null) {
488 else {
throw new ParseException(curOpt.ToString(),
"Missing argument for option!"); }
508 var posInStr =
ShortOpts.IndexOf(shortOpt);
509 if (posInStr == -1) {
522 switch (charToCheck) {
540 throw new ParseException(shortOpt.ToString(),
"Invalid option list!");
543 var nullableOpt =
Options.FindOptionOrDefault(shortOpt);
545 if (nullableOpt ==
null) {
548 }
else {
throw new ParseException(shortOpt.ToString(),
"Encountered unknown option!"); }
551 var opt = (
Option)nullableOpt;
563 var splitString =
default(
string[]);
569 if (indexOfSeparator != -1) {
570 curArg = $
"{ curArg.Substring(0, indexOfSeparator) }{ GnuArgSeparator }{ curArg.Substring(indexOfSeparator + 1) }";
576 if (splitString.Length == 1) {
582 optName = splitString[0];
584 argVal =
string.Join(
"", splitString[2..]);
586 argVal =
string.Join(
"", splitString.Skip(2));
604 return curArg.Substring(1);
612 if (isLongOpt && curArg.StartsWith(
DoubleDash)) {
613 return curArg.Substring(2);
614 }
else if (curArg.StartsWith(
SingleDash.ToString())) {
615 return curArg.Substring(1);
627 return !
string.IsNullOrEmpty(arg) &&
628 arg.Equals(
"--", StringComparison.CurrentCultureIgnoreCase);
637 if (
string.IsNullOrEmpty(arg)) {
return false; }
660 return arg.Length > 2 &&
671 if (
string.IsNullOrEmpty(arg)) {
return false; }
680 return arg.Length > 1 &&
698 if (
string.IsNullOrEmpty(arg) || !
AllowParamFiles || arg.Length < 2) {
return false; }
702 arg = arg.TrimStart(
'@');
704 if (File.Exists(arg)) {
716 if (paramFile ==
null || !paramFile.Exists) {
return; }
718 var lastIndex =
AppArgs.Length;
719 var lines = File.ReadAllLines(paramFile.FullName);
722 for (
int i = lastIndex, j = 0; i < m_appArgs.Length && j < lines.Length; i++, j++) {
723 if (
string.IsNullOrEmpty(lines[j].Trim())) {
continue; }
724 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.
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...
static bool ShallStopParsing(ref string arg)
Gets a value indicating whether or not the parser shall stop here.
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.