using Ookii.CommandLine.Commands;
using System.Collections.Generic;
using System.ComponentModel;
[assembly: ApplicationFriendlyName("Ookii.CommandLine Subcommand Sample")]
var options = new CommandOptions()
CommandNameTransform = NameTransform.DashCase,
DefaultValueDescriptions = new Dictionary<Type, string>()
{ typeof(double), "Number" }
UseErrorColor = TriState.False,
UsageWriter = new UsageWriter(useColor: TriState.False),
var manager = new CommandManager(options);
return manager.RunCommand(args) ?? 1;
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();
abstract class UnaryCommand : ICommand
[CommandLineArgument(Position = 0, IsRequired = true)]
[Description("The operand.")]
public double Value { get; set; }
public abstract int Run();
[Description("Adds two numbers.")]
partial class AddCommand : BinaryCommand
public override int Run()
Console.WriteLine($"{Left} + {Right} = {Left + Right}");
[Description("Multiplies two numbers.")]
partial class MultiplyCommand : BinaryCommand
public override int Run()
Console.WriteLine($"{Left} * {Right} = {Left * Right}");
[Description("Takes the square root of a number.")]
partial class SquareRootCommand : UnaryCommand
public override int Run()
Console.WriteLine($"√{Value} = {Math.Sqrt(Value)}");
[Description("Gets the absolute value of a number.")]
partial class AbsoluteValueCommand : UnaryCommand
public override int Run()
Console.WriteLine($"|{Value}| = {Math.Abs(Value)}");