Skip to content

Instantly share code, notes, and snippets.

@YounesCheikh
Last active February 22, 2022 19:40
Show Gist options
  • Save YounesCheikh/c000e4a03ba7b545df1838b03e41474c to your computer and use it in GitHub Desktop.
Save YounesCheikh/c000e4a03ba7b545df1838b03e41474c to your computer and use it in GitHub Desktop.
Very old school way to parse Command Line arguments to compare with the Cliargs.NET package
var userInputArgs = Environment.GetCommandLineArgs().Skip(1).ToArray();
if(userInputArgs.Length == 0)
{
Console.WriteLine("Missing mandatory arguments: --name");
return;
}
string name = string.Empty;
uint? age = null;
var currentKey = string.Empty;
foreach(var arg in userInputArgs)
{
if(currentKey == "--name" || currentKey == "-n")
{
name = arg;
currentKey = string.Empty;
continue;
}
if (currentKey == "--age" || currentKey == "-a")
{
age = Convert.ToUInt32(arg);
currentKey = string.Empty;
continue;
}
if (arg == "--name" || arg == "-n")
{
currentKey = arg;
continue;
}
if (arg == "--age" || arg == "-a")
{
currentKey = arg;
continue;
}
Console.WriteLine("Error: Unkown argument {0}", arg);
return;
}
if(string.IsNullOrWhiteSpace(name))
{
Console.WriteLine("Wrong value for name");
return;
}
if(age.HasValue)
{
Console.WriteLine($"Dear {name}, you're {age.Value} years old.");
}
else
{
Console.WriteLine($"Dear {name}, we don't know your age!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment