NullLib.CommandLine
1.0.5
Prefix Reserved
See the version list below for details.
dotnet add package NullLib.CommandLine --version 1.0.5
NuGet\Install-Package NullLib.CommandLine -Version 1.0.5
<PackageReference Include="NullLib.CommandLine" Version="1.0.5" />
paket add NullLib.CommandLine --version 1.0.5
#r "nuget: NullLib.CommandLine, 1.0.5"
// Install NullLib.CommandLine as a Cake Addin #addin nuget:?package=NullLib.CommandLine&version=1.0.5 // Install NullLib.CommandLine as a Cake Tool #tool nuget:?package=NullLib.CommandLine&version=1.0.5
NullLib.CommandLine
Easily calling methods defined in C# with a command string.
To read Chinese document, scroll down. 向下滚动屏幕以查看中文文档
Usage
First of all, the basic type in NullLib.CommandLine for calling methods is CommandObject
, it contains methods' information, such as MethodInfo
, ParameterInfo
, and attributes.
Then, a class which contains methods for being called should be defined, in this class, each method which will be called must has a Command
attribute, and then, we will initialize a CommandObject
instance with this type.
An example class:
public class AppCommands
{
[Command]
public void HelloWorld()
{
Console.WriteLine("Hello world!");
}
}
Initialize a CommandObject
instance, and loop execute command.
using System;
using NullLib.CommandLine;
class Program
{
static CommandObject<AppCommands> AppCommandObject = new CommandObject<AppCommands>(); // new CommandObject instance
static void Main(string[] args)
{
Console.WriteLine("Now input commands.");
while (true)
{
Console.Write(">>> "); // prompt
string cmdline = Console.ReadLine();
if (!AppCommandObject.TryExecuteCommand(cmdline, out var result))
{
if (result != null) // if a method has no return value, then result is null.
Console.WriteLine(result);
}
else
{
Console.WriteLine("Command execute failed.");
}
}
}
}
Run application, and input command:
Now input commands.
>>> HelloWorld
Hello world!
To pass parameters to method, specify ArgumentConverter
for each parameter.
Let's add these methods to AppCommands
[Command(typeof(FloatArguConverter), typeof(FloatArguConverter))] // the build-in ArgumentConverter in NullLib.CommandLine
public float Plus(float a, float b)
{
return a + b;
}
[Command(typeof(FloatArguConverter))] // if the following converters is same as the last one, you can ignore these
public float Mul(float a, float b)
{
return a * b;
}
[Command(typeof(DoubleArguConverter))]
public double Log(double n, double newBase = Math.E) // you can also use optional parameter
{
return Math.Log(n, newBase);
}
[Command(typeof(ForeachArguConverter<FloatArguConverter>))] // each string of array will be converted by FloatConverter
public float Sum(params float[] nums) // variable length parameter method is supported
{
float result = 0;
foreach (var i in nums)
result += i;
return result;
}
[Command(typeof(ArgutConverter))] // if don't need to do any convertion, specify an 'ArguConverter'
public void Print(string txt)
{
Console.WriteLine(txt);
}
[Command] // the defualt converter is 'ArgumentConverter', you can ignore these
public bool StringEquals(string txt1, string txt2) // or specify 'null' to use the last converter (here is ArgumentConverter)
{
return txt1.Equals(txt2);
}
[Command(typeof(EnumArguConverter<ConsoleColor>))] // EnumConverter helps convert string to Enum type automatically.
public void SetBackground(ConsoleColor color)
{
Console.BackgroundColor = color;
}
Run and input:
Now input commands.
>>> Plus 1 1
2
>>> Mul 2 4
8
>>> Log 8 2
3
>>> Log 8
2.07944154167984
>>> Sum 1 2 3 4
10
>>> Print "some text\tescaped char is also supported"
some text escaped char is also supported
>>> StringEquals qwq awa
False
>>> SetBackground White
>>> SetBackground 0
>>> Print "you can convert to a enum type by it's name or integer value"
you can convert to a enum type by it's name or integer value
More
[https://github.com/SlimeNull/NullLib.CommandLine](For more information, Click here to go to Github)
NullLib.CommandLine
通过命令行字符串来方便快捷的调用 C# 中定义的方法
使用方式
首先, 在 NullLib.CommandLine 中用于调用方法的最基本类型是 CommandObject
, 它包含了方法的各种信息, 例如 MethodInfo
, ParameterInfo
, 以及属性.
然后, 你需要定义一个包含要调用方法的类, 在这个类中, 每一个将被调用的方法都应该有一个 Command
属性, 之后我们将用这个类型实例化一个 CommandObject
实例.
一个示例类型:
public class AppCommands
{
[Command]
public void HelloWorld()
{
Console.WriteLine("Hello world!");
}
}
实例化一个 CommandObject
对象, 然后循环执行指令.
using System;
using NullLib.CommandLine;
class Program
{
static CommandObject<AppCommands> AppCommandObject = new CommandObject<AppCommands>(); // 实例化一个 CommandObject 对象
static void Main(string[] args)
{
Console.WriteLine("Now input commands.");
while (true)
{
Console.Write(">>> "); // 提示符
string cmdline = Console.ReadLine();
if (!AppCommandObject.TryExecuteCommand(cmdline, out var result))
{
if (result != null) // 如果一个方法没有返回值, 则结果是 null.
Console.WriteLine(result);
}
else
{
Console.WriteLine("Command execute failed.");
}
}
}
}
运行程序, 并输入指令:
Now input commands.
>>> HelloWorld
Hello world!
为每一个参数指定 ArgumentConverter
来以传递参数到方法.
那么, 我们再试试将这些方法添加到 AppCommands
中
[Command(typeof(FloatArguConverter), typeof(FloatArguConverter))] // NullLib.CommandLine 中的内置 ArgumentConverter
public float Plus(float a, float b)
{
return a + b;
}
[Command(typeof(FloatArguConverter))] // 如果跟着的转换器与上一个是一样的, 那么你可以忽略它们
public float Mul(float a, float b)
{
return a * b;
}
[Command(typeof(DoubleArguConverter))]
public double Log(double n, double newBase = Math.E) // 你也可以使用可选参数
{
return Math.Log(n, newBase);
}
[Command(typeof(ForeachArguConverter<FloatArguConverter>))] // 数组中的每一个字符串都将被 FloatArguConverter 转换
public float Sum(params float[] nums) // 可变参数的方法也是受支持的
{
float result = 0;
foreach (var i in nums)
result += i;
return result;
}
[Command(typeof(ArgutConverter))] // 如果不需要做任何转换, 则可以指定一个 'ArguConverter'
public void Print(string txt)
{
Console.WriteLine(txt);
}
[Command] // 默认的转换器是 'ArguConverter', 在这种情况下你可以忽略它们
public bool StringEquals(string txt1, string txt2) // 或者指定 'null' 来使用上一个转换器(在这里指 ArguConverter)
{
return txt1.Equals(txt2);
}
[Command(typeof(EnumArguConverter<ConsoleColor>))] // EnumConverter 可以用来将字符串自动转换为枚举类型
public void SetBackground(ConsoleColor color)
{
Console.BackgroundColor = color;
}
Run and input:
Now input commands.
>>> Plus 1 1
2
>>> Mul 2 4
8
>>> Log 8 2
3
>>> Log 8
2.07944154167984
>>> Sum 1 2 3 4
10
>>> Print "一些文本\t转义字符也是受支持的"
一些文本 转义字符也是受支持的
>>> StringEquals qwq awa
False
>>> SetBackground White
>>> SetBackground 0
>>> Print "你可以通过一个枚举类型的名字或整数值来进行转换"
你可以通过一个枚举类型的名字或整数值来进行转换
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.3.0.3 | 585 | 3/12/2022 | |
1.3.0.2 | 449 | 3/2/2022 | |
1.3.0.1 | 462 | 3/2/2022 | |
1.2.0.1 | 441 | 2/23/2022 | |
1.1.0.2 | 406 | 10/27/2021 | |
1.1.0.1 | 628 | 10/27/2021 | |
1.1.0 | 431 | 10/27/2021 | |
1.0.9.1 | 339 | 8/30/2021 | |
1.0.9 | 321 | 8/28/2021 | |
1.0.8 | 329 | 8/26/2021 | |
1.0.7 | 382 | 7/3/2021 | |
1.0.6.2 | 392 | 6/7/2021 | |
1.0.6.1 | 387 | 6/3/2021 | |
1.0.5 | 1,377 | 6/2/2021 | |
1.0.4 | 448 | 6/2/2021 | |
1.0.3 | 410 | 5/31/2021 | |
1.0.2 | 367 | 5/31/2021 | |
1.0.1 | 358 | 3/18/2021 |