Skip to content

Commit 0ea2511

Browse files
committed
added a demo project
1 parent 78ad9e7 commit 0ea2511

File tree

10 files changed

+238
-2
lines changed

10 files changed

+238
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
It is a presentation layer framework using MVVM that helps to implement a CLI (command line interface).
44

5+
## How to use (with Autofac)
6+
7+
1. Include `ConsoleTools.Commando.Autofac.DependencyInjection` nuget package.
8+
9+
2. Register `Commando` into `Autofac`.
10+
11+
```csharp
12+
Assembly presentationAssembly = typeof(SomeCommand).Assembly;
13+
containerBuilder.RegisterCommando(presentationAssembly);
14+
```
15+
16+
3. Instantiate the `CommandRouter`
17+
```csharp
18+
CommandRouter commandRouter = context.Resolve<CommandRouter>();
19+
```
20+
21+
4. Parse the arguments
22+
```csharp
23+
Arguments arguments = new(args);
24+
```
25+
26+
5. Execute
27+
```csharp
28+
await commandRouter.Execute(arguments);
29+
```
30+
531
## Discussions and Suggestions
632

733
https://github.com/lastunicorn/Commando/discussions

sources/ConsoleTools.Commando/ConsoleTools.Commando.Autofac.DependencyInjection/CommandoSetup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public static void RegisterCommando(this ContainerBuilder containerBuilder, para
3939

4040
foreach (Type type in availableCommands.GetViewTypes())
4141
containerBuilder.RegisterType(type).AsSelf();
42+
43+
containerBuilder.RegisterType<Application>().AsSelf();
4244
}
4345
}
4446
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ConsoleTools.Commando
2+
// Copyright (C) 2022 Dust in the Wind
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
using System.Threading.Tasks;
18+
19+
namespace DustInTheWind.ConsoleTools.Commando.Demo.Commands
20+
{
21+
[Command("dummy", ShortDescription = "A dummy command that shows how to use Commando.")]
22+
public class DummyCommand : ICommand
23+
{
24+
[CommandParameter(Name = "text", ShortName = 't', IsOptional = true)]
25+
public string DummyText { get; set; }
26+
27+
public Task Execute()
28+
{
29+
DummyText += " - text was updated";
30+
31+
return Task.CompletedTask;
32+
}
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// ConsoleTools.Commando
2+
// Copyright (C) 2022 Dust in the Wind
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
using System;
18+
19+
namespace DustInTheWind.ConsoleTools.Commando.Demo.Commands
20+
{
21+
public class DummyView : IView<DummyCommand>
22+
{
23+
public void Display(DummyCommand command)
24+
{
25+
Console.WriteLine(command.DummyText);
26+
}
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!--
2+
ConsoleTools.Commando
3+
Copyright (C) 2022 Dust in the Wind
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
-->
18+
19+
<Project Sdk="Microsoft.NET.Sdk">
20+
21+
<PropertyGroup>
22+
<OutputType>Exe</OutputType>
23+
<TargetFramework>net5.0</TargetFramework>
24+
<AssemblyName>DustInTheWind.ConsoleTools.Commando.Demo</AssemblyName>
25+
<RootNamespace>DustInTheWind.ConsoleTools.Commando.Demo</RootNamespace>
26+
</PropertyGroup>
27+
28+
<ItemGroup>
29+
<PackageReference Include="Autofac" Version="6.4.0" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<ProjectReference Include="..\ConsoleTools.Commando.Autofac.DependencyInjection\ConsoleTools.Commando.Autofac.DependencyInjection.csproj" />
34+
</ItemGroup>
35+
36+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ConsoleTools.Commando
2+
// Copyright (C) 2022 Dust in the Wind
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
using System.Threading.Tasks;
18+
using Autofac;
19+
20+
namespace DustInTheWind.ConsoleTools.Commando.Demo
21+
{
22+
internal class Program
23+
{
24+
private static async Task Main(string[] args)
25+
{
26+
IContainer container = Setup.ConfigureServices();
27+
28+
await using ILifetimeScope lifetimeScope = container.BeginLifetimeScope();
29+
30+
Application application = lifetimeScope.Resolve<Application>();
31+
await application.Run(args);
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ConsoleTools.Commando
2+
// Copyright (C) 2022 Dust in the Wind
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
using System.Reflection;
18+
using Autofac;
19+
using DustInTheWind.ConsoleTools.Commando.Autofac.DependencyInjection;
20+
21+
namespace DustInTheWind.ConsoleTools.Commando.Demo
22+
{
23+
internal class Setup
24+
{
25+
public static IContainer ConfigureServices()
26+
{
27+
ContainerBuilder containerBuilder = new();
28+
29+
Assembly presentationAssembly = Assembly.GetExecutingAssembly();
30+
containerBuilder.RegisterCommando(presentationAssembly);
31+
32+
return containerBuilder.Build();
33+
}
34+
}
35+
}

sources/ConsoleTools.Commando/ConsoleTools.Commando.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{933C5D
1010
Directory.Build.props = Directory.Build.props
1111
EndProjectSection
1212
EndProject
13-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTools.Commando.Autofac.DependencyInjection", "ConsoleTools.Commando.Autofac.DependencyInjection\ConsoleTools.Commando.Autofac.DependencyInjection.csproj", "{EEFC180D-6001-4EE8-BAB1-EBE17BD8E341}"
13+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTools.Commando.Autofac.DependencyInjection", "ConsoleTools.Commando.Autofac.DependencyInjection\ConsoleTools.Commando.Autofac.DependencyInjection.csproj", "{EEFC180D-6001-4EE8-BAB1-EBE17BD8E341}"
14+
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTools.Commando.Demo", "ConsoleTools.Commando.Demo\ConsoleTools.Commando.Demo.csproj", "{8EE16070-FB87-44FA-9BEF-5A22D4EC38D1}"
1416
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -26,6 +28,10 @@ Global
2628
{EEFC180D-6001-4EE8-BAB1-EBE17BD8E341}.Debug|Any CPU.Build.0 = Debug|Any CPU
2729
{EEFC180D-6001-4EE8-BAB1-EBE17BD8E341}.Release|Any CPU.ActiveCfg = Release|Any CPU
2830
{EEFC180D-6001-4EE8-BAB1-EBE17BD8E341}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{8EE16070-FB87-44FA-9BEF-5A22D4EC38D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{8EE16070-FB87-44FA-9BEF-5A22D4EC38D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{8EE16070-FB87-44FA-9BEF-5A22D4EC38D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{8EE16070-FB87-44FA-9BEF-5A22D4EC38D1}.Release|Any CPU.Build.0 = Release|Any CPU
2935
EndGlobalSection
3036
GlobalSection(SolutionProperties) = preSolution
3137
HideSolutionNode = FALSE
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace DustInTheWind.ConsoleTools.Commando
7+
{
8+
public class Application
9+
{
10+
private readonly CommandRouter commandRouter;
11+
12+
public Application(CommandRouter commandRouter)
13+
{
14+
this.commandRouter = commandRouter ?? throw new ArgumentNullException(nameof(commandRouter));
15+
commandRouter.CommandCreated += HandleCommandCreated;
16+
}
17+
18+
private static void HandleCommandCreated(object? sender, CommandCreatedEventArgs e)
19+
{
20+
if (e.UnusedArguments.Count <= 0)
21+
return;
22+
23+
IEnumerable<string> unusedArguments = e.UnusedArguments
24+
.Select(x => x.Name ?? x.Value);
25+
26+
foreach (string unusedArgument in unusedArguments)
27+
CustomConsole.WriteLine(ConsoleColor.DarkYellow, $"Unknown argument: {unusedArgument}");
28+
}
29+
30+
public async Task Run(string[] args)
31+
{
32+
Arguments arguments = new(args);
33+
await commandRouter.Execute(arguments);
34+
}
35+
}
36+
}

sources/ConsoleTools.Commando/Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
<Product>ConsoleTools Commando</Product>
2424
<Company>Dust in the Wind</Company>
2525
<Copyright>Copyright © Dust in the Wind 2022</Copyright>
26-
<Authors />
2726
</PropertyGroup>
2827

2928
</Project>

0 commit comments

Comments
 (0)