Two more modifiers for return codes #202
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was writing a little tutorial for plumbum and converting one of my more complex bash scripts to plumbum, and I noticed one common use that
subprocess.call
does well that plumbum works a little harder for. It was in checking and working with return codes. While the exception method is excellent, if you are working with, for example, git, and you want to toggle behavior based on the return code of a command (likegit rev-list HEAD..origin
), then you have to use.run(retcode=None)[0]
or try/except blocks (which then require accessing the stdin/stderr differently in the except block).I added two modifiers alongside FG and BG, namely TF and RETCODE, that make working with return codes easy and natural in plumbum. They work a lot like BG, for example:
They support foreground execution with
FG=True
in the call, and TF can take retcode just like FG. Note that the bash syntax for using true/false return codes is &&, so this is a little like it, like BG.I've added the two new objects, a test, documentation, and docstrings. Also fixed one minor typo in the docstring for FG.
PS: Should I add to the "what's new" documentation, too? I'm assuming you do that, but I'm happy to help if you need me to. Also, let me know if you have recommendations about names, etc, I'm happy to change them and resubmit if you have better ideas.) Thanks for such a brilliant library! I'll send a link to my blog post tutorial when I'm done with it if you want to see it. It should have a plumbum vs. bash example.