Random Color Generator with CLI Args

  • Share this:

Code introduction


This function uses the Fire library to accept command line arguments and generates a random color based on the arguments or returns the specified color name and value.


Technology Stack : Fire, random, json

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random
import json
from fire import Fire

def generate_random_color():
    return f'#{random.randint(0, 0xFFFFFF):06x}'

def random_color_arg(color_name, color_value):
    if color_name == 'name':
        return color_value
    elif color_name == 'value':
        return generate_random_color()
    else:
        raise ValueError("Invalid color argument")

if __name__ == "__main__":
    Fire(random_color_arg, color_name="name", color_value="red")                
              
Tags: