davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

must_match does not check supplied value #300

Closed logicminds closed 3 years ago

logicminds commented 3 years ago

Example of GLI command

cb coins roi --hashrate=54mh --power=130 --usdcost=$428.00

Example of associated flag

roi.flag :usdcost, desc: 'The USD Cost of the equipment ie. 428.0', type: Float, must_match: /\$?\d*\.?\d{0,2}/

Expectation

GLI would throw and exception or error that the provided value does not match the pattern. Without this $428 is a string when it should be a float.

Even changing the type to a String does not yield anything different.

davetron5000 commented 3 years ago

I think this might be a problem with your regexp. That regexp matches any string because every element of it is optional. If you put ^ at the start and $ at the end, it should work:

roi.flag :usdcost, desc: 'The USD Cost of the equipment ie. 428.0', type: Float, must_match: /^\$?\d*\.?\d{0,2}$/
#                                                                                             ^                ^
#                                                                                             |                |
#                                                                                             |                |
#                                                                                             |                |
logicminds commented 3 years ago

The updated regex did the trick. The error message is not helpful to the user though. Is there a way to override the default error message?

error: invalid argument: --usdcost=$

davetron5000 commented 3 years ago

Agreed it's not a great error message. It's tricky to improve because GLI defers this part of the parsing to Ruby's OptionParser which is what is generating that error message.

davetron5000 commented 3 years ago

Closing as original issue is not an issue. Feel free to open a new one about the not-very-good error message