Posts

Showing posts from May, 2022

Stackoverflow Answers Miss The Mark- CLI Input Validation

  I was testing out so_pip looking for re-usable code on SO and found https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response Someone posted a really specific "how do I do this specific thing: check if input is more or less than 18", the big picture answer is: Look, a CLI has to get input from people just like a browser or GUI does, but out-of-the-box there isn't an input field where you can set any properties to restrict user input. But you can get most of the way there by writing 6-10 lines of non-reusable code if all you want to do is write that little one-trick pony program. Summary of answers One interesting answer is linux only, I couldn’t test. Two related answers provide a function that force an input to be an integer in a range, or an arbitrary type in a range. The rest didn’t have re-usuable code, or if they did, it wasn’t for the main problem. One answer said use click.prompt and that is getting close to the corr...