CSharp · Console
// A basic sample showing how to use subcommand with 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.Commands;
using System;
using System.Collections.Generic;
using System.ComponentModel;
// Used by the version command.
[assembly: ApplicationFriendlyName("Ookii.CommandLine Subcommand Sample")]
// 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[] { "add", "17.4", "188.9" };
//args = new[] { "multiply", "6", "7" };
//args = new[] { "sqrt", "25" };
//args = new[] { "abs", "-Help" };
//args = new[] { "version" };
// 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 CommandOptions()
{
CommandNameTransform = NameTransform.DashCase,
DefaultValueDescriptions = new Dictionary<Type, string>()
{
{ typeof(double), "Number" }
},
UseErrorColor = TriState.False,
UsageWriter = new UsageWriter(useColor: TriState.False),
// Uncomment the below if you want a more POSIX-like argument syntax for all commands:
//IsPosix = true
};
var manager = new CommandManager(options);
return manager.RunCommand(args) ?? 1;
// Base class for operations with two arguments.
abstract class BinaryCommand : ICommand
{
[CommandLineArgument(Position = 0, IsRequired = true)]
[Description("The first operand.")]
public double Left { get; set; }
[CommandLineArgument(Position = 1, IsRequired = true)]
[Description("The second operand.")]
public double Right { get; set; }
public abstract int Run();
}
// Base class for operations with one argument.
abstract class UnaryCommand : ICommand
{
[CommandLineArgument(Position = 0, IsRequired = true)]
[Description("The operand.")]
public double Value { get; set; }
public abstract int Run();
}
// The GeneratedParserAttribute is applied, but it doesn't do anything here
// because .Net Fiddle doesn't currently run source generators.
[GeneratedParser]
[Command]
[Description("Adds two numbers.")]
partial class AddCommand : BinaryCommand
{
public override int Run()
{
Console.WriteLine($"{Left} + {Right} = {Left + Right}");
return 0;
}
}
[GeneratedParser]
[Command]
[Description("Multiplies two numbers.")]
partial class MultiplyCommand : BinaryCommand
{
public override int Run()
{
Console.WriteLine($"{Left} * {Right} = {Left * Right}");
return 0;
}
}
[GeneratedParser]
[Command]
[Description("Takes the square root of a number.")]
[Alias("sqrt")]
partial class SquareRootCommand : UnaryCommand
{
public override int Run()
{
Console.WriteLine($"√{Value} = {Math.Sqrt(Value)}");
return 0;
}
}
[GeneratedParser]
[Command]
[Description("Gets the absolute value of a number.")]
[Alias("abs")]
partial class AbsoluteValueCommand : UnaryCommand
{
public override int Run()
{
Console.WriteLine($"|{Value}| = {Math.Abs(Value)}");
return 0;
}
}