# Python Random Joke Generator

This utility generates random jokes, specifically tailored for programmers, using the `pyjokes` library. It allows you to filter jokes by category and language.

**Modules Used:**
*   `pyjokes`: To fetch one-liner jokes.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments for category and language.

## Installation

```bash
pip install pyjokes
```

## The Code

Save this as `jokegen.py`.

```python
import pyjokes
import argparse

def tell_joke(category='neutral', language='en'):
    try:
        # Fetch a random joke
        joke = pyjokes.get_joke(language=language, category=category)
        print(joke)
    except Exception as e:
        print(f"Error: {e}")
        print("Ensure the language and category combination is valid.")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Random Programmer Joke Generator")
    
    parser.add_argument(
        "-c", "--category", 
        default="neutral", 
        choices=['neutral', 'adult', 'all'],
        help="Category of the joke (default: neutral)"
    )
    
    parser.add_argument(
        "-l", "--language", 
        default="en", 
        choices=['en', 'de', 'es'],
        help="Language of the joke (default: en)"
    )
    
    args = parser.parse_args()
    
    tell_joke(args.category, args.language)
```

## Usage

```bash
# Default (Neutral, English)
python jokegen.py

# Get an 'adult' (profane) joke
python jokegen.py --category adult

# Get a joke in Spanish
python jokegen.py --language es
```

[[programming/python/python]]