Skip to content

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

Merged
henryiii merged 16 commits into
CLIUtils:masterfrom
metopa:suffixed_number_transform
May 18, 2019
Merged

henryiii merged 16 commits into
CLIUtils:masterfrom
metopa:suffixed_number_transform

Conversation

@metopa

@metopa metopa commented Mar 11, 2019 •

Copy link
Copy Markdown
Contributor

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

codecov Bot commented Mar 12, 2019

Copy link
Copy Markdown

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

codecov Bot commented Mar 12, 2019 •

Copy link
Copy Markdown

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

henryiii commented Mar 12, 2019 •

Copy link
Copy Markdown
Collaborator

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

metopa commented Mar 12, 2019

Copy link
Copy Markdown
Contributor Author

@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

Comment thread README.md Resolved
@henryiii

henryiii commented Mar 12, 2019 •

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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

metopa commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

@henryiii Sure thing, thank you a lot!

@henryiii henryiii mentioned this pull request Apr 28, 2019
@henryiii

Copy link
Copy Markdown
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
@henryiii
henryiii force-pushed the suffixed_number_transform branch from 4d7642a to beb99d8 Compare May 16, 2019 15:33
@metopa

metopa commented May 16, 2019

Copy link
Copy Markdown
Contributor Author

Tricky! Thank you for figuring this out.

@metopa

metopa commented May 16, 2019

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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

phlptp commented May 16, 2019

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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

metopa commented May 17, 2019

Copy link
Copy Markdown
Contributor Author

@henryiii I've just updated it

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

Copy link
Copy Markdown
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