Command Line Parser Library: Parse a hex string into UInt32

0

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?

c#
parsing
command-line-arguments
command-line-parser
asked on Stack Overflow Aug 25, 2015 by CurtisHx

1 Answer

1

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.

answered on Stack Overflow Aug 25, 2015 by jgg99

User contributions licensed under CC BY-SA 3.0