I have a console application that needs to take in arguments. The app uses the Command Line Parser Library to parse the arguments.
The application needs to be able to take in hexadecimal arguments, and convert them to unsigned integers.
For example, if this is the Option class
public class CommandLineOptions
{
[Option('l', "crcLocation", Required = false, HelpText = "Address where the CRC will be inserted. Must be outside of the application image")]
public UInt32 CrcLocation { get; set; }
}
then the app should be able to launch with
app.exe -l 0x0000000F
thus setting CrcLocation
to 15
Is there a way to make the Command Line Parser Library convert from hexadecimal string to integer, or does the application need to do that manually?
From the library's source code, it internally uses the method Convert.ChangeType
to perform the conversion, which unfortunately does not support hexadecimal numbers. See: https://github.com/gsscoder/commandline/blob/master/src/CommandLine/Core/TypeConverter.cs#L66
Your best bet is to expose a string and use UInt32.TryParse
with the correct NumberStyles flags to perform the conversion yourself.
User contributions licensed under CC BY-SA 3.0