RSCG – CommandLine
| name | CommandLine |
| nuget | https://www.nuget.org/packages/DotMake.CommandLine/ |
| link | https://github.com/dotmake-build/command-line |
| author | DotMake |
Generating easy command line applications.
This is how you can use CommandLine .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotMake.CommandLine" Version="1.8.0" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information
using DotMake.CommandLine;
Cli.Run(([CliArgument] string PersonName,int Age) =>
{
Console.WriteLine($@"Value for {nameof(PersonName)} parameter is '{PersonName}'");
Console.WriteLine($@"Value for {nameof(Age)} parameter is '{Age}'");
});
Cli.Run(([CliArgument] int idData) =>
{
Console.WriteLine($@"Value for {nameof(idData)} parameter is '{idData}'");
});
The code that is generated is
// <auto-generated />
// Generated by DotMake.CommandLine.SourceGeneration v1.8.0.0
// Roslyn (Microsoft.CodeAnalysis) v4.800.23.57201
// Generation: 1
namespace GeneratedCode
{
/// <inheritdoc />
[DotMake.CommandLine.CliCommandAttribute]
public class CliCommandAsDelegate_1ra93x8 : DotMake.CommandLine.CliCommandAsDelegateDefinition
{
/// <inheritdoc />
[DotMake.CommandLine.CliArgumentAttribute]
public int idData { get; set; }
/// <inheritdoc />
public void Run()
{
InvokeDelegate
(
"1ra93x8",
new object[]
{
idData,
}
);
}
[System.Runtime.CompilerServices.ModuleInitializerAttribute]
internal static void Initialize()
{
// Register this definition class so that it can be found by the command as delegate hash.
Register<GeneratedCode.CliCommandAsDelegate_1ra93x8>("1ra93x8");
}
}
}
// <auto-generated />
// Generated by DotMake.CommandLine.SourceGeneration v1.8.0.0
// Roslyn (Microsoft.CodeAnalysis) v4.800.23.57201
// Generation: 1
namespace GeneratedCode
{
/// <inheritdoc />
public class CliCommandAsDelegate_1ra93x8Builder : DotMake.CommandLine.CliCommandBuilder
{
/// <inheritdoc />
public CliCommandAsDelegate_1ra93x8Builder()
{
DefinitionType = typeof(GeneratedCode.CliCommandAsDelegate_1ra93x8);
ParentDefinitionType = null;
NameCasingConvention = DotMake.CommandLine.CliNameCasingConvention.KebabCase;
NamePrefixConvention = DotMake.CommandLine.CliNamePrefixConvention.DoubleHyphen;
ShortFormPrefixConvention = DotMake.CommandLine.CliNamePrefixConvention.SingleHyphen;
ShortFormAutoGenerate = true;
}
private GeneratedCode.CliCommandAsDelegate_1ra93x8 CreateInstance()
{
return new GeneratedCode.CliCommandAsDelegate_1ra93x8();
}
/// <inheritdoc />
public override System.CommandLine.CliCommand Build()
{
// Command for 'CliCommandAsDelegate_1ra93x8' class
var rootCommand = new System.CommandLine.CliRootCommand()
{
};
var defaultClass = CreateInstance();
// Argument for 'idData' property
var argument0 = new System.CommandLine.CliArgument<int>
(
"id-data"
)
{
};
argument0.CustomParser = GetParseArgument<int>
(
null
);
argument0.DefaultValueFactory = _ => defaultClass.idData;
rootCommand.Add(argument0);
// Add nested or external registered children
foreach (var child in Children)
{
rootCommand.Add(child.Build());
}
BindFunc = (parseResult) =>
{
var targetClass = CreateInstance();
// Set the parsed or default values for the options
// Set the parsed or default values for the arguments
targetClass.idData = GetValueForArgument(parseResult,argument0);
return targetClass;
};
rootCommand.SetAction(parseResult =>
{
var targetClass = (GeneratedCode.CliCommandAsDelegate_1ra93x8) BindFunc(parseResult);
// Call the command handler
var cliContext = new DotMake.CommandLine.CliContext(parseResult);
var exitCode = 0;
targetClass.Run();
return exitCode;
});
return rootCommand;
}
[System.Runtime.CompilerServices.ModuleInitializerAttribute]
internal static void Initialize()
{
var commandBuilder = new GeneratedCode.CliCommandAsDelegate_1ra93x8Builder();
// Register this command builder so that it can be found by the definition class
// and it can be found by the parent definition class if it's a nested/external child.
commandBuilder.Register();
}
}
}
// <auto-generated />
// Generated by DotMake.CommandLine.SourceGeneration v1.8.0.0
// Roslyn (Microsoft.CodeAnalysis) v4.800.23.57201
// Generation: 2
namespace GeneratedCode
{
/// <inheritdoc />
[DotMake.CommandLine.CliCommandAttribute]
public class CliCommandAsDelegate_34pdvjr : DotMake.CommandLine.CliCommandAsDelegateDefinition
{
/// <inheritdoc />
[DotMake.CommandLine.CliArgumentAttribute]
public string PersonName { get; set; }
/// <inheritdoc />
[DotMake.CommandLine.CliOptionAttribute]
public int Age { get; set; }
/// <inheritdoc />
public void Run()
{
InvokeDelegate
(
"34pdvjr",
new object[]
{
PersonName,
Age,
}
);
}
[System.Runtime.CompilerServices.ModuleInitializerAttribute]
internal static void Initialize()
{
// Register this definition class so that it can be found by the command as delegate hash.
Register<GeneratedCode.CliCommandAsDelegate_34pdvjr>("34pdvjr");
}
}
}
// <auto-generated />
// Generated by DotMake.CommandLine.SourceGeneration v1.8.0.0
// Roslyn (Microsoft.CodeAnalysis) v4.800.23.57201
// Generation: 2
namespace GeneratedCode
{
/// <inheritdoc />
public class CliCommandAsDelegate_34pdvjrBuilder : DotMake.CommandLine.CliCommandBuilder
{
/// <inheritdoc />
public CliCommandAsDelegate_34pdvjrBuilder()
{
DefinitionType = typeof(GeneratedCode.CliCommandAsDelegate_34pdvjr);
ParentDefinitionType = null;
NameCasingConvention = DotMake.CommandLine.CliNameCasingConvention.KebabCase;
NamePrefixConvention = DotMake.CommandLine.CliNamePrefixConvention.DoubleHyphen;
ShortFormPrefixConvention = DotMake.CommandLine.CliNamePrefixConvention.SingleHyphen;
ShortFormAutoGenerate = true;
}
private GeneratedCode.CliCommandAsDelegate_34pdvjr CreateInstance()
{
return new GeneratedCode.CliCommandAsDelegate_34pdvjr();
}
/// <inheritdoc />
public override System.CommandLine.CliCommand Build()
{
// Command for 'CliCommandAsDelegate_34pdvjr' class
var rootCommand = new System.CommandLine.CliRootCommand()
{
};
var defaultClass = CreateInstance();
// Option for 'Age' property
var option0 = new System.CommandLine.CliOption<int>
(
"--age"
)
{
Required = false,
};
option0.CustomParser = GetParseArgument<int>
(
null
);
option0.DefaultValueFactory = _ => defaultClass.Age;
option0.Aliases.Add("-a");
rootCommand.Add(option0);
// Argument for 'PersonName' property
var argument0 = new System.CommandLine.CliArgument<string>
(
"person-name"
)
{
};
argument0.CustomParser = GetParseArgument<string>
(
null
);
rootCommand.Add(argument0);
// Add nested or external registered children
foreach (var child in Children)
{
rootCommand.Add(child.Build());
}
BindFunc = (parseResult) =>
{
var targetClass = CreateInstance();
// Set the parsed or default values for the options
targetClass.Age = GetValueForOption(parseResult,option0);
// Set the parsed or default values for the arguments
targetClass.PersonName = GetValueForArgument(parseResult,argument0);
return targetClass;
};
rootCommand.SetAction(parseResult =>
{
var targetClass = (GeneratedCode.CliCommandAsDelegate_34pdvjr) BindFunc(parseResult);
// Call the command handler
var cliContext = new DotMake.CommandLine.CliContext(parseResult);
var exitCode = 0;
targetClass.Run();
return exitCode;
});
return rootCommand;
}
[System.Runtime.CompilerServices.ModuleInitializerAttribute]
internal static void Initialize()
{
var commandBuilder = new GeneratedCode.CliCommandAsDelegate_34pdvjrBuilder();
// Register this command builder so that it can be found by the definition class
// and it can be found by the parent definition class if it's a nested/external child.
commandBuilder.Register();
}
}
}
// <auto-generated />
// Generated by DotMake.CommandLine.SourceGeneration v1.8.0.0
// Roslyn (Microsoft.CodeAnalysis) v4.800.23.57201
// Generation: 2
namespace GeneratedCode
{
/// <inheritdoc />
[DotMake.CommandLine.CliCommandAttribute]
public class CliCommandAsDelegate_3r0zsnc : DotMake.CommandLine.CliCommandAsDelegateDefinition
{
/// <inheritdoc />
[DotMake.CommandLine.CliArgumentAttribute]
public string arg1 { get; set; }
/// <inheritdoc />
[DotMake.CommandLine.CliOptionAttribute]
public bool opt1 { get; set; }
/// <inheritdoc />
public void Run()
{
InvokeDelegate
(
"3r0zsnc",
new object[]
{
arg1,
opt1,
}
);
}
[System.Runtime.CompilerServices.ModuleInitializerAttribute]
internal static void Initialize()
{
// Register this definition class so that it can be found by the command as delegate hash.
Register<GeneratedCode.CliCommandAsDelegate_3r0zsnc>("3r0zsnc");
}
}
}
// <auto-generated />
// Generated by DotMake.CommandLine.SourceGeneration v1.8.0.0
// Roslyn (Microsoft.CodeAnalysis) v4.800.23.57201
// Generation: 2
namespace GeneratedCode
{
/// <inheritdoc />
public class CliCommandAsDelegate_3r0zsncBuilder : DotMake.CommandLine.CliCommandBuilder
{
/// <inheritdoc />
public CliCommandAsDelegate_3r0zsncBuilder()
{
DefinitionType = typeof(GeneratedCode.CliCommandAsDelegate_3r0zsnc);
ParentDefinitionType = null;
NameCasingConvention = DotMake.CommandLine.CliNameCasingConvention.KebabCase;
NamePrefixConvention = DotMake.CommandLine.CliNamePrefixConvention.DoubleHyphen;
ShortFormPrefixConvention = DotMake.CommandLine.CliNamePrefixConvention.SingleHyphen;
ShortFormAutoGenerate = true;
}
private GeneratedCode.CliCommandAsDelegate_3r0zsnc CreateInstance()
{
return new GeneratedCode.CliCommandAsDelegate_3r0zsnc();
}
/// <inheritdoc />
public override System.CommandLine.CliCommand Build()
{
// Command for 'CliCommandAsDelegate_3r0zsnc' class
var rootCommand = new System.CommandLine.CliRootCommand()
{
};
var defaultClass = CreateInstance();
// Option for 'opt1' property
var option0 = new System.CommandLine.CliOption<bool>
(
"--opt-1"
)
{
Required = false,
};
option0.CustomParser = GetParseArgument<bool>
(
null
);
option0.DefaultValueFactory = _ => defaultClass.opt1;
option0.Aliases.Add("-o");
rootCommand.Add(option0);
// Argument for 'arg1' property
var argument0 = new System.CommandLine.CliArgument<string>
(
"arg-1"
)
{
};
argument0.CustomParser = GetParseArgument<string>
(
null
);
rootCommand.Add(argument0);
// Add nested or external registered children
foreach (var child in Children)
{
rootCommand.Add(child.Build());
}
BindFunc = (parseResult) =>
{
var targetClass = CreateInstance();
// Set the parsed or default values for the options
targetClass.opt1 = GetValueForOption(parseResult,option0);
// Set the parsed or default values for the arguments
targetClass.arg1 = GetValueForArgument(parseResult,argument0);
return targetClass;
};
rootCommand.SetAction(parseResult =>
{
var targetClass = (GeneratedCode.CliCommandAsDelegate_3r0zsnc) BindFunc(parseResult);
// Call the command handler
var cliContext = new DotMake.CommandLine.CliContext(parseResult);
var exitCode = 0;
targetClass.Run();
return exitCode;
});
return rootCommand;
}
[System.Runtime.CompilerServices.ModuleInitializerAttribute]
internal static void Initialize()
{
var commandBuilder = new GeneratedCode.CliCommandAsDelegate_3r0zsncBuilder();
// Register this command builder so that it can be found by the definition class
// and it can be found by the parent definition class if it's a nested/external child.
commandBuilder.Register();
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/CommandLine
Leave a Reply