mkdir AddCommentToZip
cd AddCommentToZip
dotnet new console
dotnet add package System.CommandLine --prerelease
(prereleaseオプションをつけないと通りませんでした。)
ソースコード
using System.CommandLine;
using System.IO.Compression;
namespace AddCommentToZip;
class Program
{
static void AddCommentToZipFile(FileInfo file, string comment)
{
if (!File.Exists(file.FullName)) return;
if (!file.Extension.Equals(".ZIP", StringComparison.CurrentCultureIgnoreCase)) return;
using var fs = new FileStream(
file.FullName,
FileMode.Open,
FileAccess.ReadWrite);
using var zip = new ZipArchive(fs, ZipArchiveMode.Update);
zip.Comment = comment;
}
static async Task<int> Main(string[] args)
{
var fileOption = new Option<FileInfo?>(
name: "--file",
description: "コメントを追加するZIPファイル"
){ IsRequired = true };
var commentOption = new Option<string>(
name: "--comment",
description: "追加するコメント"
){ IsRequired = true };
var rootCommand = new RootCommand(
"ZIPファイルにコメントを追加するコマンド"
);
rootCommand.AddOption(fileOption);
rootCommand.AddOption(commentOption);
rootCommand.SetHandler((file, comment) =>
{
AddCommentToZipFile(file!, comment);
},
fileOption, commentOption);
return await rootCommand.InvokeAsync(args);
}
}
ソースコードのダウンロード
実行
コマンドライン引数をセットして実行
dotnet run -- --file test.zip --comment ABCDEFG
dotnet runでコマンドライン引数を試す場合–を付加するようです。
ヘルプ
dotnet run -- --help
Description:
ZIPファイルにコメントを追加するコマンド
Usage:
AddCommentToZip [options]
Options:
--file (REQUIRED) コメントを追加するZIPファイル
--comment (REQUIRED) 追加するコメント
--version Show version information
-?, -h, --help Show help and usage information
ヘルプにも対応しているようです。
引数なしで実行
dotnet run --
Option '--file' is required.
Option '--comment' is required.
Description:
ZIPファイルにコメントを追加するコマンド
Usage:
AddCommentToZip [options]
Options:
--file <file> (REQUIRED) コメントを追加するZIPファイル
--comment <comment> (REQUIRED) 追加するコメント
--version Show version information
-?, -h, --help Show help and usage information
コメント