# 177. CLI Tools with Click

**Snippet 1: Basic CLI with Click**

```python
import click

@click.command()
def hello():
    """Simple Hello World CLI"""
    click.echo("Hello, World!")

if __name__ == '__main__':
    hello()
```

***

**Snippet 2: CLI with Arguments**

```python
import click

@click.command()
@click.argument('name')
def greet(name):
    """CLI to greet a user by name"""
    click.echo(f"Hello, {name}!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 3: CLI with Multiple Arguments**

```python
import click

@click.command()
@click.argument('first_name')
@click.argument('last_name')
def greet(first_name, last_name):
    """CLI to greet a user by first and last name"""
    click.echo(f"Hello, {first_name} {last_name}!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 4: CLI with Options**

```python
import click

@click.command()
@click.option('--name', prompt='Your name', help='The person to greet')
def greet(name):
    """CLI to greet a user with an option"""
    click.echo(f"Hello, {name}!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 5: CLI with Default Value for Options**

```python
import click

@click.command()
@click.option('--name', default='World', help='The person to greet')
def greet(name):
    """CLI to greet with a default value"""
    click.echo(f"Hello, {name}!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 6: CLI with Multiple Options**

```python
import click

@click.command()
@click.option('--name', prompt='Your name', help='The person to greet')
@click.option('--greeting', default='Hello', help='The greeting to use')
def greet(name, greeting):
    """CLI to greet a user with a custom greeting"""
    click.echo(f"{greeting}, {name}!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 7: CLI with Confirmation Prompt**

```python
import click

@click.command()
@click.option('--name', prompt='Your name', help='The person to greet')
@click.option('--greeting', default='Hello', help='The greeting to use')
@click.confirmation_option(prompt="Are you sure you want to continue?")
def greet(name, greeting):
    """CLI to greet a user with a confirmation prompt"""
    click.echo(f"{greeting}, {name}!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 8: CLI with Argument Validation**

```python
import click

@click.command()
@click.argument('age', type=int)
def check_age(age):
    """CLI to check if a user is an adult"""
    if age < 18:
        click.echo("You are a minor.")
    else:
        click.echo("You are an adult.")

if __name__ == '__main__':
    check_age()
```

***

**Snippet 9: CLI with Dynamic Options**

```python
import click

@click.command()
@click.option('--num', type=int, default=1, help='Number of greetings')
def greet(num):
    """CLI to greet multiple times"""
    for _ in range(num):
        click.echo("Hello!")

if __name__ == '__main__':
    greet()
```

***

**Snippet 10: CLI with Subcommands**

```python
import click

@click.group()
def cli():
    """Main CLI group"""
    pass

@click.command()
def greet():
    """Subcommand to greet"""
    click.echo("Hello!")

@click.command()
def farewell():
    """Subcommand to say farewell"""
    click.echo("Goodbye!")

cli.add_command(greet)
cli.add_command(farewell)

if __name__ == '__main__':
    cli()
```

***
