chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using NUnit.Framework;
using OpenTap.Cli;
using System;
using System.ComponentModel;
using System.Threading;
 
namespace OpenTap.UnitTests
{
    [TestFixture]
    public class CliCommandTest
    {
        [Test]
        public void CliActionTreeTest()
        {
            var root = new CliActionTree();
            Assert.IsTrue(root.GetSubCommand(Array.Empty<string>()) == null);
            Assert.IsTrue(root.GetSubCommand("".Split(' ')) == null);
            Assert.IsTrue(root.GetSubCommand("test".Split(' ')).Name == "test");
            Assert.IsTrue(root.GetSubCommand("test action".Split(' ')).Name == "action");
            Assert.IsTrue(root.GetSubCommand("test action testaction".Split(' ')).Name == "testaction");
            Assert.IsTrue(root.GetSubCommand("test action testaction arg".Split(' ')).Name == "testaction");
        }
 
        [Test]
        public void TestHelpOnActionWithNoDisplayAttribute()
        {
            // Cli actions should be excuted inside a session.
            using (Session.Create())
            {
                Assert.That(CliActionExecutor.Execute(nameof(CliActionWithNoDisplayAttribute), "--help"), Is.EqualTo(0));
                Assert.That(CliActionExecutor.Execute(nameof(CliActionWithNoDisplayAttribute), "--123"), Is.EqualTo((int)ExitCodes.ArgumentParseError));
            }
        }
    }
 
    [Display("testaction", Groups: new[] { "test", "action" }, Description:"Runs TestAction")]
    public class TestAction : ICliAction
    {
        [UnnamedCommandLineArgument("notrequired", Required = false)]
        public string NotRequiredArgument { get; set; }
        public int Execute(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
    
    public class CliActionWithNoDisplayAttribute : ICliAction
    {
        public int Execute(CancellationToken cancellationToken) => 0;
    }
 
    [Display("action2", Groups: new[] { "test" }, Description:"Runs TestAction2")]
    public class TestAction2 : ICliAction
    {
        [CommandLineArgument("other", Description = "Some option which does not override a common option", ShortName = "o")]
        public bool Other { get; set; }
        [CommandLineArgument("quiet", Description = "Some option which overrides a common option", ShortName = "h")]
        public string Quiet { get; set; }
        public int Execute(CancellationToken cancellationToken)
        {
            Console.WriteLine($"Executed action 2 with 'Quiet = {Quiet}'.");
            return (int)ExitCodes.Success;
        }
    }
 
    [Display("action3", Groups: new[] { "test" }, Description:"Cannot create instance")]
    public class TestAction3 : ICliAction
    {
        public TestAction3()
        {
            throw new LicenseException(GetType(), null, "No license installed!");
        }
        public int Execute(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
    
    [Display("testargaction", Groups: new[] { "test", "action" }, Description:"Runs TestAction")]
    public class TestArgCliACtion  : ICliAction
    {
        [CommandLineArgument("a")] public uint a { get; set; } = 0;
        [CommandLineArgument("b")] public ulong b { get; set; } = 0;
        // c overridden by --color - does not work.
        [CommandLineArgument("c")] public float c { get; set; } = 0;
        [CommandLineArgument("d")] public float d { get; set; } = 0;
        [CommandLineArgument("e")] public double e { get; set; } = 0;
        [CommandLineArgument("f")] public long f { get; set; } = 0;
        // aliased by --help
        [CommandLineArgument("h")] public int[] h { get; set; } = Array.Empty<int>();
        [CommandLineArgument("i")] public int[] i { get; set; } = Array.Empty<int>();
 
        [CommandLineArgument("verdict")] public Verdict[] Verdict { get; set; } = Array.Empty<Verdict>();
        
        public int Execute(CancellationToken cancellationToken)
        {
            Console.WriteLine("{0} {1} {2} {3} {4} {5} [{6}]", a, b, c, d, e, f, string.Join(" ", i));
            Console.WriteLine("Verdicts: {0} ", string.Join(" ", Verdict));
            return 0;
        }
    }
    
}