If you are referring to any arbitrary C program, no there is currently no way to reliably check if a program supports an option. You can try guessing with a mixture of ./program --help
, ./program --usage
, ./program -h
and ./program --this-option-is-a-lie-or-some-other-bogus-option-to-give-usage-information
. However, it all ultimately boils down to guesswork.
How to know if a C program supports an option
Question
I'm developing a script that checks the version of some installed C programs. The version check is performed with the --version option. However, this option may not be implemented in all the checked programs. When the option is implemented I use:
version=$(./$program_name --version)
But when it's not, the program just starts executing. If I just execute the program in the background and the stop it if continues running, I can never get the version number. Is there a way to check whether the option is implemented without letting the program run?
Solution 2
OTHER TIPS
Not completely waterproof but I nice start is using strings:
strings /usr/bin/git | grep -- --version
On RedHat and derivates using yum
and rpm
you can ask the rpm package manager:
$ rpm -q --whatprovides /bin/cat
coreutils-8.4-31.el6_5.1.i686
On Debian and derivated using apt-get
you can ask the APT package manager:
$ dpkg-query -S /bin/bash
bash: /bin/bash
Back in the day we always included a what
string with version info. Actually part of SCCS
, if the program contained strings starting with "@(#)", it is displayed by the what
command. C code would like like this:
static char prog_id[] = "@(#) my_program version 1.0 - 3/26/2014";
Anyway try doing a what
on the program and see what
you get. heh.