← 返回命令列表

Linux command

t4 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Process

t4 [template.tt] -o [output.cs]

Process

t4 [template.tt] -o [output.cs] -p:[Name]="[value]"

Process

echo '[<#= DateTime.Now #>]' | t4 -o -

Preprocess

t4 -c [ClassName] [template.tt] -o [Template.cs]

Process

echo '<#@ parameter name="[Date]" type="System.DateTime" #><#=$"{[Date]:dddd}"#>' | t4 -o - -p:[Date]="[2024/03/08]"

说明

t4 is a command-line tool for processing T4 (Text Template Transformation Toolkit) templates, a general-purpose way to use C# to generate any kind of text or code files. It is part of Mono.TextTemplating, a modern open-source reimplementation of the Visual Studio T4 text templating engine. The tool operates in two modes: direct template processing, which generates output files immediately, and preprocessing, which converts templates into runtime template classes that can be embedded in applications and executed at runtime. Templates use C# code blocks delimited by <# and #> tags to emit dynamic content. Parameter directives support automatic type conversion from CLI string values to the type specified in the template. The tool can read templates from standard input and write output to standard output, enabling pipeline integration.

参数

-o _file_
Output file path; use - for stdout
-p:_name_=_value_
Pass a parameter value to the template (repeatable)
-c _classname_
Preprocess template into a runtime template class
-r _assembly_
Reference an assembly
-u _namespace_
Import a namespace
-I _directory_
Add an include search directory
--dp _processor!class!assembly_
Add a custom directive processor
--help
Show help message
--version
Show version information

FAQ

What is the t4 command used for?

t4 is a command-line tool for processing T4 (Text Template Transformation Toolkit) templates, a general-purpose way to use C# to generate any kind of text or code files. It is part of Mono.TextTemplating, a modern open-source reimplementation of the Visual Studio T4 text templating engine. The tool operates in two modes: direct template processing, which generates output files immediately, and preprocessing, which converts templates into runtime template classes that can be embedded in applications and executed at runtime. Templates use C# code blocks delimited by <# and #> tags to emit dynamic content. Parameter directives support automatic type conversion from CLI string values to the type specified in the template. The tool can read templates from standard input and write output to standard output, enabling pipeline integration.

How do I run a basic t4 example?

Run `t4 [template.tt] -o [output.cs]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -o _file_ do in t4?

Output file path; use - for stdout