Some scripts just print that they're done, even when they skip records or find nothing to process. What should a command-line script tell the person running it?
How can I make my Python scripts' output more useful?
Answers
Give the user a short summary of what actually happened. How many items were found, how many changed, and how many were skipped? If things were skipped, say why, at least in categories such as invalid input or missing fields.
Zero results deserve a little thought. Finding nothing might be perfectly normal for one script and a sign of a problem for another. Make the message reflect what the script expects rather than treating every zero as either a success or a failure.
Reply to this answer
Use logging when you need different levels of detail. A concise normal run can show the outcome, while a more verbose mode can explain individual decisions and help diagnose failures. Send errors somewhere people will actually notice them.
4j~ferrulewhisk_369:
Depending on how the tool is used, quiet by default may be better. A script used in automation might only need output when something goes wrong.
Reply to this answer
Don't forget the help text. A useful --help should say what the script does, what inputs it expects, and what its options change. Even good run output won't help much if someone can't figure out how to invoke the tool.
Reply to this answer
Make errors readable without hiding the actual failure. For example, if an API request fails, report which operation failed and enough context to investigate. Catch exceptions where you can add useful information or recover, not just to keep the script running silently.
Reply to this answer
Match the effort to the audience. A small internal script may only need a few clear messages, while a frequently used command might benefit from a command-line library such as Click. You don't need a fancy interface just to make the result understandable.
Reply to this answer
Add your answer
Answers are reviewed before publication.
6f!cobblemarble_821:
That distinction helps. A count alone doesn't tell me whether I should be concerned.