Add MkvEditCommand

This commit is contained in:
2022-03-18 09:32:47 +01:00
parent df86aee35f
commit d2391b95b3
2 changed files with 129 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
using System.Text;
using MkvPropEditWrapper.Shared;
namespace MkvPropEditWrapper;
public class MkvEditCommand
{
public const string CommandName = "mkvpropedit";
public class SubCommand
{
private int? _trackNumber;
private string? _property;
private string? _value;
public SubCommand()
{
}
public SubCommand(int trackNumber, string property, string value)
{
_trackNumber = trackNumber;
_property = property;
_value = value;
}
public SubCommand WithNumber(int number)
{
_trackNumber = number;
return this;
}
public SubCommand WithProperty(string property)
{
_property = property;
return this;
}
public SubCommand WithValue(string value)
{
_value = value;
return this;
}
public void AppendSelf(StringBuilder builder)
{
builder.Append($"track:{_trackNumber} --set {_property}:{_value}");
}
public override string ToString()
{
return $"track:{_trackNumber} --set {_property}:{_value}";
}
}
private string? _filePath;
private readonly IList<SubCommand> _subCommands;
public MkvEditCommand()
{
_subCommands = new List<SubCommand>();
}
public MkvEditCommand(string filePath)
{
_filePath = filePath;
_subCommands = new List<SubCommand>();
}
public MkvEditCommand WithFilePath(string filePath)
{
_filePath = filePath;
return this;
}
public MkvEditCommand AddSubCommand(SubCommand command)
{
_subCommands.Add(command);
return this;
}
public override string ToString()
{
var builder = new StringBuilder();
builder.Append($"{CommandName} {_filePath}");
foreach (var subCommand in _subCommands)
{
builder.Append(" --edit ");
subCommand.AppendSelf(builder);
}
return builder.ToString();
}
}
public static class SubCommandExtensions
{
public static MkvEditCommand.SubCommand WithFlagDefault(this MkvEditCommand.SubCommand subCommand, bool flagDefault)
{
return subCommand.WithProperty(Flags.Default).WithValue(flagDefault.ToFlagBoolean());
}
public static MkvEditCommand.SubCommand WithFlagForced(this MkvEditCommand.SubCommand subCommand, bool flagForced)
{
return subCommand.WithProperty(Flags.Forced).WithValue(flagForced.ToFlagBoolean());
}
}

View File

@@ -0,0 +1,13 @@
namespace MkvPropEditWrapper.Shared;
public static class MkvExtensions
{
public static bool FromFlagBoolean(this string value) => value switch
{
"1" => true,
"0" => false,
_ => throw new ArgumentException("Value must be '0' or '1'", nameof(value))
};
public static string ToFlagBoolean(this bool value) => value ? "1" : "0";
}