Replace Mono.Options with System.CommandLine

This commit is contained in:
2020-01-02 04:09:39 +00:00
parent 4a74de5843
commit 758d4dd364
30 changed files with 2389 additions and 2147 deletions

View File

@@ -30,7 +30,8 @@
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Interfaces;
@@ -39,81 +40,82 @@ using DiscImageChef.Core;
using DiscImageChef.Decoders.ATA;
using DiscImageChef.Decoders.CD;
using DiscImageChef.Decoders.SCSI;
using Mono.Options;
namespace DiscImageChef.Commands
{
class DecodeCommand : Command
internal class DecodeCommand : Command
{
bool diskTags = true;
string inputFile;
string length = "all";
bool sectorTags = true;
bool showHelp;
ulong startSector;
public DecodeCommand() : base("decode", "Decodes and pretty prints disk and/or sector tags.")
{
Options = new OptionSet
Add(new Option(new[]
{
"--disk-tags", "-f"
}, "Decode disk tags.")
{
Argument = new Argument<bool>(() => true), Required = false
});
Add(new Option(new[]
{
"--length", "-l"
}, "How many sectors to decode, or \"all\".")
{
Argument = new Argument<string>(() => "all"), Required = false
});
Add(new Option(new[]
{
"--sector-tags", "-p"
}, "Decode sector tags.")
{
Argument = new Argument<bool>(() => true), Required = false
});
Add(new Option(new[]
{
"--start", "-s"
}, "Sector to start decoding from.")
{
Argument = new Argument<ulong>(() => 0), Required = false
});
AddArgument(new Argument<string>
{
$"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}",
$"{MainClass.AssemblyCopyright}",
"",
$"usage: DiscImageChef {Name} [OPTIONS] imagefile",
"",
Help,
{"disk-tags|f", "Decode disk tags.", b => diskTags = b != null},
{"length|l=", "How many sectors to decode, or \"all\".", s => length = s},
{"sector-tags|p", "Decode sector tags.", b => sectorTags = b != null},
{"start|s=", "Name of character encoding to use.", (ulong ul) => startSector = ul},
{"help|h|?", "Show this message and exit.", v => showHelp = v != null}
};
Arity = ArgumentArity.ExactlyOne, Description = "Media image path", Name = "image-path"
});
Handler = CommandHandler.Create(GetType().GetMethod(nameof(Invoke)));
}
public override int Invoke(IEnumerable<string> arguments)
static int Invoke(bool verbose, bool debug, bool diskTags, string imagePath, string length, bool sectorTags,
ulong startSector)
{
List<string> extra = Options.Parse(arguments);
if(showHelp)
{
Options.WriteOptionDescriptions(CommandSet.Out);
return (int)ErrorNumber.HelpRequested;
}
MainClass.PrintCopyright();
if(MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
if(MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
if(debug)
DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
if(verbose)
DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
Statistics.AddCommand("decode");
if(extra.Count > 1)
{
DicConsole.ErrorWriteLine("Too many arguments.");
return (int)ErrorNumber.UnexpectedArgumentCount;
}
if(extra.Count == 0)
{
DicConsole.ErrorWriteLine("Missing input image.");
return (int)ErrorNumber.MissingArgument;
}
inputFile = extra[0];
DicConsole.DebugWriteLine("Decode command", "--debug={0}", MainClass.Debug);
DicConsole.DebugWriteLine("Decode command", "--disk-tags={0}", diskTags);
DicConsole.DebugWriteLine("Decode command", "--input={0}", inputFile);
DicConsole.DebugWriteLine("Decode command", "--length={0}", length);
DicConsole.DebugWriteLine("Decode command", "--debug={0}", debug);
DicConsole.DebugWriteLine("Decode command", "--disk-tags={0}", diskTags);
DicConsole.DebugWriteLine("Decode command", "--input={0}", imagePath);
DicConsole.DebugWriteLine("Decode command", "--length={0}", length);
DicConsole.DebugWriteLine("Decode command", "--sector-tags={0}", sectorTags);
DicConsole.DebugWriteLine("Decode command", "--start={0}", startSector);
DicConsole.DebugWriteLine("Decode command", "--verbose={0}", MainClass.Verbose);
DicConsole.DebugWriteLine("Decode command", "--start={0}", startSector);
DicConsole.DebugWriteLine("Decode command", "--verbose={0}", verbose);
FiltersList filtersList = new FiltersList();
IFilter inputFilter = filtersList.GetFilter(inputFile);
var filtersList = new FiltersList();
IFilter inputFilter = filtersList.GetFilter(imagePath);
if(inputFilter == null)
{
DicConsole.ErrorWriteLine("Cannot open specified file.");
return (int)ErrorNumber.CannotOpenFile;
return(int)ErrorNumber.CannotOpenFile;
}
IMediaImage inputFormat = ImageFormat.Detect(inputFilter);
@@ -121,7 +123,8 @@ namespace DiscImageChef.Commands
if(inputFormat == null)
{
DicConsole.ErrorWriteLine("Unable to recognize image format, not decoding");
return (int)ErrorNumber.UnrecognizedFormat;
return(int)ErrorNumber.UnrecognizedFormat;
}
inputFormat.Open(inputFilter);
@@ -139,16 +142,20 @@ namespace DiscImageChef.Commands
case MediaTagType.SCSI_INQUIRY:
{
byte[] inquiry = inputFormat.ReadDiskTag(MediaTagType.SCSI_INQUIRY);
if(inquiry == null)
DicConsole.WriteLine("Error reading SCSI INQUIRY response from disc image");
else
{
DicConsole.WriteLine("SCSI INQUIRY command response:");
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(Inquiry.Prettify(inquiry));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
}
break;
@@ -156,16 +163,20 @@ namespace DiscImageChef.Commands
case MediaTagType.ATA_IDENTIFY:
{
byte[] identify = inputFormat.ReadDiskTag(MediaTagType.ATA_IDENTIFY);
if(identify == null)
DicConsole.WriteLine("Error reading ATA IDENTIFY DEVICE response from disc image");
else
{
DicConsole.WriteLine("ATA IDENTIFY DEVICE command response:");
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(Identify.Prettify(identify));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
}
break;
@@ -173,17 +184,21 @@ namespace DiscImageChef.Commands
case MediaTagType.ATAPI_IDENTIFY:
{
byte[] identify = inputFormat.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY);
if(identify == null)
DicConsole
.WriteLine("Error reading ATA IDENTIFY PACKET DEVICE response from disc image");
DicConsole.
WriteLine("Error reading ATA IDENTIFY PACKET DEVICE response from disc image");
else
{
DicConsole.WriteLine("ATA IDENTIFY PACKET DEVICE command response:");
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(Identify.Prettify(identify));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
}
break;
@@ -191,31 +206,41 @@ namespace DiscImageChef.Commands
case MediaTagType.CD_ATIP:
{
byte[] atip = inputFormat.ReadDiskTag(MediaTagType.CD_ATIP);
if(atip == null) DicConsole.WriteLine("Error reading CD ATIP from disc image");
if(atip == null)
DicConsole.WriteLine("Error reading CD ATIP from disc image");
else
{
DicConsole.WriteLine("CD ATIP:");
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(ATIP.Prettify(atip));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
}
break;
}
case MediaTagType.CD_FullTOC:
{
byte[] fulltoc = inputFormat.ReadDiskTag(MediaTagType.CD_FullTOC);
if(fulltoc == null) DicConsole.WriteLine("Error reading CD full TOC from disc image");
byte[] fullToc = inputFormat.ReadDiskTag(MediaTagType.CD_FullTOC);
if(fullToc == null)
DicConsole.WriteLine("Error reading CD full TOC from disc image");
else
{
DicConsole.WriteLine("CD full TOC:");
DicConsole
.WriteLine("================================================================================");
DicConsole.WriteLine(FullTOC.Prettify(fulltoc));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(FullTOC.Prettify(fullToc));
DicConsole.
WriteLine("================================================================================");
}
break;
@@ -223,48 +248,62 @@ namespace DiscImageChef.Commands
case MediaTagType.CD_PMA:
{
byte[] pma = inputFormat.ReadDiskTag(MediaTagType.CD_PMA);
if(pma == null) DicConsole.WriteLine("Error reading CD PMA from disc image");
if(pma == null)
DicConsole.WriteLine("Error reading CD PMA from disc image");
else
{
DicConsole.WriteLine("CD PMA:");
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(PMA.Prettify(pma));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
}
break;
}
case MediaTagType.CD_SessionInfo:
{
byte[] sessioninfo = inputFormat.ReadDiskTag(MediaTagType.CD_SessionInfo);
if(sessioninfo == null)
byte[] sessionInfo = inputFormat.ReadDiskTag(MediaTagType.CD_SessionInfo);
if(sessionInfo == null)
DicConsole.WriteLine("Error reading CD session information from disc image");
else
{
DicConsole.WriteLine("CD session information:");
DicConsole
.WriteLine("================================================================================");
DicConsole.WriteLine(Session.Prettify(sessioninfo));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(Session.Prettify(sessionInfo));
DicConsole.
WriteLine("================================================================================");
}
break;
}
case MediaTagType.CD_TEXT:
{
byte[] cdtext = inputFormat.ReadDiskTag(MediaTagType.CD_TEXT);
if(cdtext == null) DicConsole.WriteLine("Error reading CD-TEXT from disc image");
byte[] cdText = inputFormat.ReadDiskTag(MediaTagType.CD_TEXT);
if(cdText == null)
DicConsole.WriteLine("Error reading CD-TEXT from disc image");
else
{
DicConsole.WriteLine("CD-TEXT:");
DicConsole
.WriteLine("================================================================================");
DicConsole.WriteLine(CDTextOnLeadIn.Prettify(cdtext));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(CDTextOnLeadIn.Prettify(cdText));
DicConsole.
WriteLine("================================================================================");
}
break;
@@ -272,15 +311,20 @@ namespace DiscImageChef.Commands
case MediaTagType.CD_TOC:
{
byte[] toc = inputFormat.ReadDiskTag(MediaTagType.CD_TOC);
if(toc == null) DicConsole.WriteLine("Error reading CD TOC from disc image");
if(toc == null)
DicConsole.WriteLine("Error reading CD TOC from disc image");
else
{
DicConsole.WriteLine("CD TOC:");
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
DicConsole.WriteLine(TOC.Prettify(toc));
DicConsole
.WriteLine("================================================================================");
DicConsole.
WriteLine("================================================================================");
}
break;
@@ -288,6 +332,7 @@ namespace DiscImageChef.Commands
default:
DicConsole.WriteLine("Decoder for disk tag type \"{0}\" not yet implemented, sorry.",
tag);
break;
}
@@ -300,6 +345,7 @@ namespace DiscImageChef.Commands
{
DicConsole.WriteLine("Value \"{0}\" is not a valid number for length.", length);
DicConsole.WriteLine("Not decoding sectors tags");
return 3;
}
}
@@ -313,13 +359,14 @@ namespace DiscImageChef.Commands
default:
DicConsole.WriteLine("Decoder for disk tag type \"{0}\" not yet implemented, sorry.",
tag);
break;
}
// TODO: Not implemented
}
return (int)ErrorNumber.NoError;
return(int)ErrorNumber.NoError;
}
}
}