made cssh more user friendly by using argparse to parse its arguments

fixes [https://bugzilla.ipr.univ-rennes.fr/show_bug.cgi?id=4366]
This commit is contained in:
Guillaume Raffy 2026-04-20 15:14:18 +02:00
parent 7f050ffba4
commit 9f4f301ba8
1 changed files with 16 additions and 7 deletions

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# this program generates a color value by hashing the input string # this program generates a color value by hashing the input string
import sys
from colorsys import hsv_to_rgb from colorsys import hsv_to_rgb
from hashlib import md5 from hashlib import md5
import argparse
def float_to_16b(component): def float_to_16b(component):
@ -28,17 +28,26 @@ def hsv_to_linux(hue, saturation, value):
if __name__ == '__main__': if __name__ == '__main__':
seed_string = sys.argv[1]
parser = argparse.ArgumentParser(description="Generate a rgb color by hashing the input string", epilog="Example usage: make_color.py cloud.ipr.univ-rennes.fr 0.7 0.5 linux")
parser.add_argument("seed_string", type=str, help="the string to be hashed to generate the pseudo random hue of the color")
parser.add_argument("color_value", type=float, help="the value (luminance) of the color between 0.0 and 1.0")
parser.add_argument("color_saturation", type=float, help="the saturation of the color between 0.0 and 1.0")
parser.add_argument("rgb_string_format", type=str, choices=['osx', 'linux'], help="the format of the output color string ('osx' for '{r:d16, g:d16, b:d16}' or 'linux' for '(r:d8, g:d8, b:d8)'")
args = parser.parse_args()
seed_string = args.seed_string
# print('seed_string = ', seed_string) # print('seed_string = ', seed_string)
color_value = float(sys.argv[2]) color_value = args.color_value
color_saturation = float(sys.argv[3]) color_saturation = args.color_saturation
string_format = sys.argv[4] # eg 'osx' 'linux' rgb_string_format = args.rgb_string_format # eg 'osx' 'linux'
# color_hue = int(md5(seed_string).hexdigest()[:8], 16) # taken from http://www.guguncube.com/3237/python-string-to-number-hash # color_hue = int(md5(seed_string).hexdigest()[:8], 16) # taken from http://www.guguncube.com/3237/python-string-to-number-hash
color_hue = float(int(md5(seed_string.encode('utf-8')).hexdigest(), 16) % 100000) / 100000.0 color_hue = float(int(md5(seed_string.encode('utf-8')).hexdigest(), 16) % 100000) / 100000.0
# print(color_hue) # print(color_hue)
if string_format == 'osx': if rgb_string_format == 'osx':
print(hsv_to_osx(color_hue, color_saturation, color_value)) print(hsv_to_osx(color_hue, color_saturation, color_value))
if string_format == 'linux': if rgb_string_format == 'linux':
print(hsv_to_linux(color_hue, color_saturation, color_value)) print(hsv_to_linux(color_hue, color_saturation, color_value))