mamot.fr is one of the many independent Mastodon servers you can use to participate in the fediverse.
Mamot.fr est un serveur Mastodon francophone, géré par La Quadrature du Net.

Server stats:

3.5K
active users

ploum

Dear and hackers,

I’m trying to find out, in Python, if "grep" has the option "--color=auto" or not.

(to make compatible with grep )

Any suggestion? (the one on the mailing-list doesn’t work for me)

I could simply scan "grep --help" for the option but, intuitively, I don’t like it.

lists.sr.ht/~lioploum/offpunk-

@ploum run the command with the option and check whether the exit code is not 0?

@ploum the first line of grep -V reads "grep (GNU grep) 3.11" for me (on Termux) so probably you could just parse the first line of the version and make your own inferences?

@fdavies93 @ploum that's not going to work if the grep supports color auto, but isn't gnu's

@SRAZKVT @ploum presumably, you just bake some logic about that into your application, which would let you be more intelligent than just running with the flag and checking the exit code, but could require annoying version checking logic (there might be a versioned index of capabilities of UNIX utilities or something out there tho, idk)

@ploum i would either look through --help, or leave it to package maintainers to set some value at build time that disables color, though im not sure how to do that in python, that's more if a c thing

@ploum I have another idea, but I think it's much worse: uncompress /usr/share/man/man1/grep.1.gz and kind of search / parse the relevant bit.

Advantage: does not start the grep process.

Inconvenients: probably lots, at least it looks like more work. And the man page location looks the same in Linux and FreeBSD, but I have no idea if that can be generalized.

@ploum
Utiliser une implémentation en python comme « greplica » ? 😁

b'color' in open('/bin/grep','rb').read() ?

@tnt : that’s quite error prone (need to check the path).

I managed to do it by checking for "x" in the binary stream b'x' with the color option. If it fails, fallback to grep without color. (the challenge was to ensure that the grep test would succeed)

git.sr.ht/~lioploum/offpunk/co