Stackoverflow Answers Miss The Mark- CLI Input Validation
I was testing out so_pip looking for re-usable code on SO and found
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 correct answer.
Stackoverflow discourages duplicate and near duplicate questions, so I can't ask, "how do I do this for professional, bespoke, for-pay, "enterprise" code?" In my opinion, as soon as you need more than a number, you should use something comparable to web or GUI controls.
Libraries on Pypi for “cli input controls” for lack of a better name:
https://click.palletsprojects.com/en/8.1.x/prompts/ - click
https://github.com/sfischer13/python-prompt - prompt
https://github.com/ddorn/superprompt - superprompt
I'm sure there are others.
The next level above this is a full-screen user interface, e.g.
Finding re-usable code on StackOverflow is hard and what superficially looks re-usable might be better solved with an actual library.
Comments
Post a Comment