Is there a convenient way to test if a given flag is supported on a command?
1
vote
1
answer
1255
views
Okay, so I'm kinda stumped here.
I'm in the midst of a deep-dive into BASH, for the purposes of writing some automation scripts for my employer. Also, full disclosure here: I'm a web guy; hitherto I've known JUST enough CLI to operate git.
My issue is, **about half the staff are using MacOS, the other half are using GitBash**, and it seems there are different flags supported (or, more relevantly, *not* supported) on the two different BASH instantiations. Further conundrum: what with everyone working from home and the world ending and all, I cannot reliably demand every one of our staff "switch to distro X"/"upgrade to version Y".
Now, I know how to test if a given program is installed (though, I confess: I'm not crystal-clear why one is preferable to the other, and please: correct me if either is a terrible way of handling this), in the forms of:
sh
type foo >/dev/null 2>&1 || { echo >&2 "COMMAND NOT FOUND"; }
...and...
sh
[ -x "$(command -v foo)" ] || echo 'COMMAND NOT FOUND'
...but in my specific case, BOTH platforms HAVE foo
installed, but, as a for-instance, only MacOS has a -b
/--bar
modifier flag. **So, how do I test to see if a given FEATURE of a command is supported?** Every time I try to pass a flag to one of the tests, the program PERFORMING the test seems to believe it's directed at IT, ignores it entirely, or errors out.
**How can I test to see if foo -b
/foo --bar
is a valid/installed/accessible command?**
# Update/Answer
Since the general consensus presented in the well-reasoned and honestly excellent answers received below appears to be a hybrid of "no, one cannot", punctuated with "there's simply too many factors to be able to reliably glean anything resembling conclusive, useful data", I'm closing out the question.
My intention here was to ascertain what the correct approach to employ was to verify the presence of a potentially non-existent flag between two different platforms - *known* platforms, fortunately, in my case - and the appropriate tact in this case would seem to be the one I'm currently taking: **maintain two separate scripts, one for each platform**.
Thank you to all who took the time to respond! It's much appreciated.
Asked by NerdyDeeds
(123 rep)
Aug 12, 2020, 01:06 AM
Last activity: Aug 14, 2020, 11:06 AM
Last activity: Aug 14, 2020, 11:06 AM