CSharp · Console
// A basic sample using Ookii.CommandLine.
// See https://github.com/SvenGroot/Ookii.CommandLine for documentation, a tutorial, and samples.
// Note that this sample DOES NOT use source generation, because that is not supported on .Net Fiddle.
#nullable enable
using Ookii.CommandLine;
using Ookii.CommandLine.Validation;
using System;
using System.ComponentModel;
// I don't think it's possible to specify arguments to .Net fiddle, so just specify
// them in code. Uncomment one of the below to see the result (or write your own).
args = new[] { "-Help" };
//args = new[] { "John" };
//args = new[] { "John", "42", "-Birthday" };
//args = new[] { "John", "-FavoriteAnimal", "cat" };
//args = new[] { "John", "-animal", "goose" }; // This uses the explicit alias for -FavoriteAnimal
//args = new[] { "John", "-fav", "dog" }; // This uses an automatic prefix alias for -FavoriteAnimal
//args = new[] { "-Version" };
// Argument names are case insensitive
//args = new[] { "Sven", "-favoriteanimal", "hamster" };
// This one can be used if you uncomment the alternate parsing mode below.
//args = new[] { "Sven", "-b", "--favorite-animal", "dog" };
// Color is explicitly disabled because .Net Fiddle's standard output is not redirected,
// so the UsageWriter thinks it's supported, but it's not (you'll see weird characters
// if you turn it on or leave it to auto).
var options = new ParseOptions()
{
UseErrorColor = TriState.False,
UsageWriter = new UsageWriter(useColor: TriState.False)
};
// If source generation was supported by .Net Fiddle, we'd write this instead:
// var arguments = ProgramArguments.Parse(options);
var arguments = CommandLineParser.Parse<ProgramArguments>(args, options);
if (arguments == null)
{
return 1;
}
if (arguments.Birthday)
{
Console.WriteLine($"Happy birthday, {arguments.Name}!");
}
else
{
Console.WriteLine($"Hello {arguments.Name}.");
}
if (arguments.Age != null)
{
Console.WriteLine($"You are {arguments.Age} years old.");
}
if (arguments.FavoriteAnimal != Animal.Unspecified)
{
Console.WriteLine($"Your favorite animal is a {arguments.FavoriteAnimal}.");
}
return 0;
enum Animal
{
Unspecified,
Cat,
Dog
}
// The GeneratedParserAttribute is applied, but it doesn't do anything here
// because .Net Fiddle doesn't currently run source generators.
//
// Uncomment the ParseOptionsAttribute to enable a more POSIX-like parsing experience,
// with long and short argument names with different prefixes ('--' and '-'), lower
// case names with words separated by dashes, and case-sensitive argument names.
// If you don't uncomment this, the "IsShort" properties below are ignored.
//[ParseOptions(IsPosix = true)]
[GeneratedParser]
[ApplicationFriendlyName("OokiiCommandLine Sample")]
[Description("Displays a message based on the command line arguments.")]
partial class ProgramArguments
{
[CommandLineArgument(Position = 0)]
[Description("Specifies your name.")]
public required string Name { get; set; }
[CommandLineArgument(Position = 1)]
[Description("Specifies your age.")]
[ValueDescription("Number")]
public int? Age { get; set; }
[CommandLineArgument(IsShort = true)]
[Description("Indicates if it's your birthday.")]
public bool Birthday { get; set; }
[CommandLineArgument(DefaultValue = Animal.Unspecified, IsShort = true)]
[Description("Indicates your favorite animal.")]
[ValidateEnumValue]
[Alias("Animal")]
public Animal FavoriteAnimal { get; set; }
}