jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.16k stars 582 forks source link

Allow "G", "M" and 'k' when parsing arguments #397

Open UweBonnes opened 1 year ago

UweBonnes commented 1 year ago

openFPGALoader uses cxxopts for argument parsing. To dump the content from some flash chip, a size argument must be given. At resent I must use "--file-size 1000000" to read 1MByte, What would it take to understand "--file-size 1M"? At the moment leaving "M" and "Mi" asinde ;-)

jarro2783 commented 1 year ago

I think this should be up to the application to deal with. Although you can implement this yourself using a custom type.

You can overload cxxopts::parse_value to add a parser for a custom type.

nigels-com commented 1 year ago

That would be nice to have out of the box. Both SI powers of 10 and computer powers of 2.

ldeng-ustc commented 1 month ago

I also think it would be better if the feature could be used out of the box. When using such units, the program ultimately still hopes to parse it into numbers, rather than custom types. But I don't think this should be the default behavior. Perhaps interfaces could be added to Value to add different parsing options. For example, use methods like the following to parse formats:

options.add_options()
    ("s,si_size", "A size with SI prefix", cxxopts::value<size_t>()->default_value("1k")->enable_si_prefix(), "SIZE")
    ("b,binary_size", "A size with binary prefix", cxxopts::value<size_t>()->default_value("1K")->enable_binary_prefix())
    ;

But unit parsing is undoubtedly a dirty work, introducing many difficult-to-handle issues (such as case sensitivity, floating point number parsing, etc.), it is hard to implement error-free in a lightweight command line parsing. Perhaps, introducing limited basic support under clear definitions might be a good idea.

Maybe I will attempt to implement it in my spare time, but for now, I still prefer to enter numbers directly or use calculations in the shell (for example, $((1024*1024))).