---
lang: ja-jp
breaks: true
---
# `dotnet cli` `dotnet-script` で C# をスクリプト実行する 2022-07-12
## filipw/dotnet-script
https://github.com/filipw/dotnet-script
## インストール
```shell=
dotnet tool install -g dotnet-script
```
## アップデート
```shell=
dotnet tool update -g dotnet-script
```
## アンインストール
```shell=
dotnet tool uninstall -g dotnet-script
```
## 使い方
```shell=
dotnet script --help
1.4.0
Usage: [command] [options] <script>
Arguments:
script Path to CSX script
Options:
-i|--interactive Execute a script and drop into the interactive mode afterwards.
-c|--configuration <configuration> Configuration to use for running the script [Release/Debug] Default is "Debug"
-s|--sources <SOURCE> Specifies a NuGet package source to use when resolving NuGet packages.
-d|--debug Enables debug output.
--verbosity Set the verbosity level of the command. Allowed values are t[trace], d[ebug],
i[nfo], w[arning], e[rror], and c[ritical].
--no-cache Disable caching (Restore and Dll cache)
--isolated-load-context Use isolated assembly load context
--info Displays environmental information
-?|-h|--help Show help information.
-v|--version Show version information.
Commands:
eval Execute CSX code.
exec Run a script from a DLL.
init Creates a sample script along with the launch.json file needed to launch and debug the script.
new Creates a new script file
publish Creates a self contained executable or DLL from a script
register Register .csx file handler to enable running scripts directly
Run ' [command] -?|-h|--help' for more information about a command.
Starting without a path to a CSX file or a command, starts the REPL (interactive) mode.
```
```shell=
dotnet script --help
1.3.1
Usage: [arguments] [options] [command]
Arguments:
script Path to CSX script
Options:
-i | --interactive Execute a script and drop into the interactive mode afterwards.
-c | --configuration <configuration> Configuration to use for running the script [Release/Debug] Default is "Debug"
-s | --sources <SOURCE> Specifies a NuGet package source to use when resolving NuGet packages.
-d | --debug Enables debug output.
--verbosity Set the verbosity level of the command. Allowed values are t[trace], d[ebug], i[nfo], w[arning], e[rror], and c[ritical].
--no-cache Disable caching (Restore and Dll cache)
--isolated-load-context Use isolated assembly load context
--info Displays environmental information
-? | -h | --help Show help information
-v | --version Show version information
Commands:
eval Execute CSX code.
exec Run a script from a DLL.
init Creates a sample script along with the launch.json file needed to launch and debug the script.
new Creates a new script file
publish Creates a self contained executable or DLL from a script
register Register .csx file handler to enable running scripts directly
Run ' [command] --help' for more information about a command.
Starting without a path to a CSX file or a command, starts the REPL (interactive) mode.
```
## Visual Studio Code 用のプロジェクトを作成
```shell=
dotnet script init
```
* lanch.json
```json=
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Script Debug",
"type": "coreclr",
"request": "launch",
"program": "dotnet",
"args": [
"exec",
"${env:HOME}/.dotnet/tools/.store/dotnet-script/1.4.0/dotnet-script/1.4.0/tools/net7.0/any/dotnet-script.dll",
"${file}"
],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"console": "integratedTerminal"
}
]
}
```
* omnisharp.json
```json=
{
"script": {
"enableScriptNuGetReferences": true,
"defaultTargetFramework": "net7.0-windows"
}
}
```
## 実行
```csharp=
Console.WriteLine("今日は世界!!");
```
```shell=
dotnet script main.csx
```
```shell=
dotnet script main.csx -c release
```
## Nullable に対応するには
> Nullable reference types
> https://github.com/dotnet-script/dotnet-script#nullable-reference-types
```csharp=
#nullable enable
```
## `csx`スクリプトファイルパスを取得
> https://github.com/filipw/dotnet-script#script-location
```csharp=
public static string GetScriptPath([System.Runtime.CompilerServices.CallerFilePath] string path = null) => path;
public static string GetScriptFolder([System.Runtime.CompilerServices.CallerFilePath] string path = null) => Path.GetDirectoryName(path);
Console.WriteLine(GetScriptPath());
Console.WriteLine(GetScriptFolder());
```
```csharp=
#nullable enable
public static string GetScriptPath([System.Runtime.CompilerServices.CallerFilePath] string? path = null) => path ?? "";
public static string GetScriptFolder([System.Runtime.CompilerServices.CallerFilePath] string? path = null) => Path.GetDirectoryName(path) ?? "";
private static string GetScriptName([System.Runtime.CompilerServices.CallerFilePath] string? path = null) => Path.GetFileNameWithoutExtension(path) ?? "";
```
## NuGet パッケージの参照方法
> NuGet Packages
> https://github.com/filipw/dotnet-script#nuget-packages-1
スクリプトの先頭に以下のように記述
```csharp=
#r "nuget: XXXXPackageName, 7.0.54"
```
:::warning
パッケージが正常に復元されない事象あり。
後日調査。
※`Visual Studio Code`で開くと復元される??
:::
## スクリプト用ディレクティブ
> https://ufcpp.net/study/csharp/cheatsheet/apscripting/
## 外部のスクリプトファイルを読み込む `#load`
> REPL commands
> https://github.com/filipw/dotnet-script#repl-commands
## プログラム引数にアクセスするには
```bat=
dotnet script XXXX\Tools.csx ^
"arg1" ^
"arg2" ^
"arg3" ^
-c release
```
```cs=
foreach (var arg in Args)
{
Console.WriteLine(arg);
}
```
## launch.json
> https://github.com/dotnet-script/dotnet-script/blob/master/src/Dotnet.Script.Core/Templates/globaltool.launch.json.template
```json=
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Script Debug",
"type": "coreclr",
"request": "launch",
"program": "${env:HOME}/.dotnet/tools/dotnet-script",
"args": ["${file}"],
"windows": {
"program": "${env:USERPROFILE}/.dotnet/tools/dotnet-script.exe",
},
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
}
]
}
```
## omnisharp.json
> https://github.com/dotnet-script/dotnet-script/blob/master/src/Dotnet.Script.Core/Templates/omnisharp.json.template
```json=
{
"script": {
"enableScriptNuGetReferences": true,
"defaultTargetFramework": "N/A"
}
}
```
```json=
{
"script": {
"enableScriptNuGetReferences": true,
"defaultTargetFramework": "net6.0"
}
}
```
###### tags: `dotnet cli` `C#` `dotnet-script`