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,93 +30,89 @@
// 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;
using DiscImageChef.Console;
using DiscImageChef.Core;
using Mono.Options;
namespace DiscImageChef.Commands
{
class PrintHexCommand : Command
internal class PrintHexCommand : Command
{
string inputFile;
ulong length = 1;
bool longSectors;
bool showHelp;
ulong? startSector;
ushort widthBytes = 32;
public PrintHexCommand() : base("printhex", "Prints a sector, in hexadecimal values, to the console.")
{
Options = new OptionSet
Add(new Option(new[]
{
"--length", "-l"
}, "How many sectors to print.")
{
Argument = new Argument<ulong>(() => 1), Required = false
});
Add(new Option(new[]
{
"--long-sectors", "-r"
}, "Print sectors with tags included.")
{
Argument = new Argument<bool>(() => false), Required = false
});
Add(new Option(new[]
{
"--start", "-s"
}, "Starting sector.")
{
Argument = new Argument<ulong>(), Required = true
});
Add(new Option(new[]
{
"--width", "-w"
}, "How many bytes to print per line.")
{
Argument = new Argument<ushort>(() => 32), Required = false
});
AddArgument(new Argument<string>
{
$"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}",
$"{MainClass.AssemblyCopyright}",
"",
$"usage: DiscImageChef {Name} [OPTIONS] imagefile",
"",
Help,
{"length|l=", "How many sectors to print.", (ulong ul) => length = ul},
{"long-sectors|r", "Print sectors with tags included.", b => longSectors = b != null},
{"start|s=", "Name of character encoding to use.", (ulong ul) => startSector = ul},
{"width|w=", "How many bytes to print per line.", (ushort us) => widthBytes = us},
{"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 debug, bool verbose, string imagePath, ulong length, bool longSectors, ulong startSector,
ushort widthBytes)
{
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("print-hex");
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;
}
if(startSector is null)
{
DicConsole.ErrorWriteLine("Missing starting sector.");
return (int)ErrorNumber.MissingArgument;
}
inputFile = extra[0];
DicConsole.DebugWriteLine("PrintHex command", "--debug={0}", MainClass.Debug);
DicConsole.DebugWriteLine("PrintHex command", "--input={0}", inputFile);
DicConsole.DebugWriteLine("PrintHex command", "--length={0}", length);
DicConsole.DebugWriteLine("PrintHex command", "--debug={0}", debug);
DicConsole.DebugWriteLine("PrintHex command", "--input={0}", imagePath);
DicConsole.DebugWriteLine("PrintHex command", "--length={0}", length);
DicConsole.DebugWriteLine("PrintHex command", "--long-sectors={0}", longSectors);
DicConsole.DebugWriteLine("PrintHex command", "--start={0}", startSector);
DicConsole.DebugWriteLine("PrintHex command", "--verbose={0}", MainClass.Verbose);
DicConsole.DebugWriteLine("PrintHex command", "--WidthBytes={0}", widthBytes);
DicConsole.DebugWriteLine("PrintHex command", "--start={0}", startSector);
DicConsole.DebugWriteLine("PrintHex command", "--verbose={0}", verbose);
DicConsole.DebugWriteLine("PrintHex command", "--WidthBytes={0}", widthBytes);
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);
@@ -124,7 +120,8 @@ namespace DiscImageChef.Commands
if(inputFormat == null)
{
DicConsole.ErrorWriteLine("Unable to recognize image format, not verifying");
return (int)ErrorNumber.UnrecognizedFormat;
return(int)ErrorNumber.UnrecognizedFormat;
}
inputFormat.Open(inputFilter);
@@ -135,28 +132,29 @@ namespace DiscImageChef.Commands
if(inputFormat.Info.ReadableSectorTags == null)
{
DicConsole
.WriteLine("Requested sectors with tags, unsupported by underlying image format, printing only user data.");
DicConsole.
WriteLine("Requested sectors with tags, unsupported by underlying image format, printing only user data.");
longSectors = false;
}
else
{
if(inputFormat.Info.ReadableSectorTags.Count == 0)
{
DicConsole
.WriteLine("Requested sectors with tags, unsupported by underlying image format, printing only user data.");
DicConsole.
WriteLine("Requested sectors with tags, unsupported by underlying image format, printing only user data.");
longSectors = false;
}
}
byte[] sector = longSectors
? inputFormat.ReadSectorLong(startSector.Value + i)
: inputFormat.ReadSector(startSector.Value + i);
byte[] sector = longSectors ? inputFormat.ReadSectorLong(startSector + i)
: inputFormat.ReadSector(startSector + i);
PrintHex.PrintHexArray(sector, widthBytes);
}
return (int)ErrorNumber.NoError;
return(int)ErrorNumber.NoError;
}
}
}