Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for size values (640 KB) and numbers with unit in general #253

Merged
merged 16 commits into from
May 18, 2019

Conversation

metopa
Copy link
Contributor

@metopa metopa commented Mar 11, 2019

This PR adds infrastructure for accepting inputs in form <number> <unit>. This covers things like size inputs, durations, and many more. <suffix> may or may not be separated from the number with space, must contain only letters and optionally can be matched case-insensitively.

CLI::AsNumberWithUnit takes a mapping from unit to factor. Transformation for durations may look like this:

map<string, double> m = {{"ns", 1e-9}, {"us", 1e-6}, {"ms", 1e-3}, {"s", 1}, {"m", 60}};
app.add_option("-d", duration)->transform(CLI::AsNumberWithUnit(mapping));

Transformation for size is already provided:

app.add_option("-s", size)->transform(CLI::AsSizeValue(true));

@codecov
Copy link

codecov bot commented Mar 12, 2019

Codecov Report

Merging #253 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #253   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          12     12           
  Lines        2566   2665   +99     
=====================================
+ Hits         2566   2665   +99
Impacted Files Coverage Δ
include/CLI/StringTools.hpp 100% <100%> (ø) ⬆️
include/CLI/Validators.hpp 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update be8a08f...f6db0a7. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 12, 2019

Codecov Report

Merging #253 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #253   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          12     12           
  Lines        2782   2879   +97     
=====================================
+ Hits         2782   2879   +97
Impacted Files Coverage Δ
include/CLI/StringTools.hpp 100% <100%> (ø) ⬆️
include/CLI/Validators.hpp 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 10e4b07...6c57111. Read the comment docs.

@henryiii
Copy link
Collaborator

henryiii commented Mar 12, 2019

I like the general version and adding an explicit one for common uses. I will probably break out the more specialized transforms (at least the size one here, IP4, etc.) into a separate utilities file so that single header file users won't have a growing file each release. It would be available for either normal or single header file use, but a separate file. Just a heads up for my plans, does not affect this PR. Will hopefully help with #194.

@henryiii henryiii added this to the v1.8 milestone Mar 12, 2019
@metopa
Copy link
Contributor Author

metopa commented Mar 12, 2019

@henryiii Could you please take a look at the building error on GCC 4.7? I don't have it on my machine and the error is a bit of WTF

README.md Show resolved Hide resolved
@henryiii
Copy link
Collaborator

henryiii commented Mar 12, 2019

I'll try it out in a 4.7 docker container later today and see if I can see what it's complaining about. GCC 4.7 is a pain, I'll probably be dropping it in a release or two. This looks fixable without too much work, I think.

@henryiii
Copy link
Collaborator

Note: This is on my todo list but I'm traveling a bit unexpectedly to cover for someone at a conference, so eventually will get to this!

@metopa
Copy link
Contributor Author

metopa commented Mar 17, 2019

@henryiii Sure thing, thank you a lot!

@henryiii henryiii mentioned this pull request Apr 28, 2019
@henryiii
Copy link
Collaborator

I believe you are seeing this bug in GCC: https://stackoverflow.com/a/12086873/2402816. I believe you are only putting these methods in the class for organization? There are no members. We can either move them to put this in 1.8 or wait till 1.9 and drop GCC 4.7 support.

@henryiii henryiii force-pushed the suffixed_number_transform branch 2 times, most recently from fd231d5 to 4d7642a Compare May 16, 2019 14:19
@metopa
Copy link
Contributor Author

metopa commented May 16, 2019

Tricky! Thank you for figuring this out.

@metopa
Copy link
Contributor Author

metopa commented May 16, 2019

After returning back to this after some time I got an idea. Should I rename SuffixedNumber to smth like NumberWithUnit and generally use "unit" term in docs? Right now it makes more sense to me.

@henryiii
Copy link
Collaborator

@phlptp , what do you think on the naming question? I like both. My physics backround leans toward Unit, but Suffix is a little more general

@phlptp
Copy link
Collaborator

phlptp commented May 16, 2019

I think I would have a preference toward "Unit". Given a significant chunk of my time recently has been on a runtime units library I may be bit biased though.

@henryiii
Copy link
Collaborator

@metopa Do you want to wait and target 1.9 instead, or try to get this in to 1.8?

@metopa metopa changed the title Support for size values (640 KB) and suffixed numbers in general Support for size values (640 KB) and numbers with unit in general May 17, 2019
@metopa
Copy link
Contributor Author

metopa commented May 17, 2019

@henryiii I've just updated it

@henryiii henryiii merged commit 59a3656 into CLIUtils:master May 18, 2019
@henryiii
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants