# 116. Command-Line Argument Parsing

Here are 10 Python snippets that demonstrate how to use the `argparse` module to parse command-line arguments and options effectively:

***

**1. Parsing a Simple Positional Argument**

```python
import argparse

parser = argparse.ArgumentParser(description="Process a single argument.")
parser.add_argument("name", type=str, help="Your name")
args = parser.parse_args()

print(f"Hello, {args.name}!")
```

***

**2. Adding an Optional Argument**

```python
import argparse

parser = argparse.ArgumentParser(description="Optional argument example.")
parser.add_argument("--age", type=int, help="Your age")
args = parser.parse_args()

if args.age:
    print(f"You are {args.age} years old.")
else:
    print("Age not provided.")
```

***

**3. Using Default Values for Optional Arguments**

```python
import argparse

parser = argparse.ArgumentParser(description="Optional argument with a default value.")
parser.add_argument("--city", type=str, default="Unknown", help="Your city")
args = parser.parse_args()

print(f"You live in {args.city}.")
```

***

**4. Using Boolean Flags**

```python
import argparse

parser = argparse.ArgumentParser(description="Boolean flag example.")
parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
args = parser.parse_args()

if args.verbose:
    print("Verbose mode enabled.")
else:
    print("Verbose mode disabled.")
```

***

**5. Parsing Multiple Positional Arguments**

```python
import argparse

parser = argparse.ArgumentParser(description="Multiple positional arguments.")
parser.add_argument("numbers", type=int, nargs=3, help="Provide three numbers")
args = parser.parse_args()

print(f"Numbers provided: {args.numbers}")
```

***

**6. Restricting Choices for an Argument**

```python
import argparse

parser = argparse.ArgumentParser(description="Restricted choices for arguments.")
parser.add_argument("color", choices=["red", "green", "blue"], help="Choose a color")
args = parser.parse_args()

print(f"You selected {args.color}.")
```

***

**7. Specifying Argument Types**

```python
import argparse

parser = argparse.ArgumentParser(description="Argument type specification.")
parser.add_argument("number", type=float, help="A floating-point number")
args = parser.parse_args()

print(f"You entered the number: {args.number}")
```

***

**8. Adding Help Text and Descriptions**

```python
import argparse

parser = argparse.ArgumentParser(
    description="A script demonstrating argparse with help and descriptions."
)
parser.add_argument("name", type=str, help="Your name")
parser.add_argument("--greeting", type=str, default="Hello", help="Custom greeting")
args = parser.parse_args()

print(f"{args.greeting}, {args.name}!")
```

***

**9. Parsing Multiple Optional Arguments**

```python
import argparse

parser = argparse.ArgumentParser(description="Multiple optional arguments.")
parser.add_argument("--start", type=int, help="Start value")
parser.add_argument("--end", type=int, help="End value")
args = parser.parse_args()

if args.start and args.end:
    print(f"Range: {args.start} to {args.end}")
else:
    print("Start or End not provided.")
```

***

**10. Grouping Arguments**

```python
import argparse

parser = argparse.ArgumentParser(description="Grouping related arguments.")
group = parser.add_argument_group("User Info")
group.add_argument("--username", type=str, required=True, help="Your username")
group.add_argument("--password", type=str, required=True, help="Your password")
args = parser.parse_args()

print(f"Username: {args.username}")
print("Password provided" if args.password else "No password provided.")
```

***

**How to Run These Scripts**

Save the Python script and run it from the command line. For example:

```bash
python script.py --age 25
```

**Summary**

The `argparse` module supports:

* Positional and optional arguments.
* Default values.
* Boolean flags.
* Type validation.
* Choices for arguments.
* Grouping arguments.

Let me know if you'd like more advanced examples or further explanation!
