> For the complete documentation index, see [llms.txt](https://soundarya.gitbook.io/mylearnings/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://soundarya.gitbook.io/mylearnings/argsparser-in-python.md).

# ArgsParser in Python

The `argparse` module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and `argparse` figures out how to parse those out of `sys.argv`. The module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

#### Key Components of `argparse`

1. **ArgumentParser Object**
   * This is the main entry point for the argument parsing system. It contains methods for adding arguments and parsing them.
2. **add\_argument() Method**
   * This method is used to specify which command-line options the program is expecting.
3. **parse\_args() Method**
   * This method is called to parse the arguments provided by the user at the command line.

#### Basic Example

Here’s a step-by-step example of how to use `argparse`:

**1. Import the Module**

First, you need to import the `argparse` module:

```python
import argparse
```

**2. Create a Parser Object**

You create an `ArgumentParser` object which will hold all the information necessary to parse the command line into Python data types:

```python
parser = argparse.ArgumentParser(description="Example Argument Parser")
```

* `description` is an optional argument that provides a brief description of what the program does.

**3. Add Arguments**

You use the `add_argument` method to specify what command-line options the program is expecting:

```python
parser.add_argument('filename', help='Name of the file to process')
parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity')
parser.add_argument('-c', '--count', type=int, help='Number of times to repeat the message')
```

* `'filename'` is a positional argument (it is required).
* `-v` or `--verbose` is an optional argument that, if provided, will set the `verbose` attribute to `True`.
* `-c` or `--count` is an optional argument that expects an integer value.

**4. Parse the Arguments**

You parse the arguments provided by the user using the `parse_args` method:

```python
args = parser.parse_args()
```

**5. Access the Arguments**

You can access the values of the arguments using the attributes of the `args` object:

```python
print(f"Filename: {args.filename}")
if args.verbose:
    print("Verbosity turned on")
if args.count:
    print(f"Count: {args.count}")
```

#### Complete Example

Here’s a complete script putting it all together:

```python
import argparse

# Create the parser
parser = argparse.ArgumentParser(description="Example Argument Parser")

# Add arguments
parser.add_argument('filename', help='Name of the file to process')
parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity')
parser.add_argument('-c', '--count', type=int, help='Number of times to repeat the message')

# Parse the arguments
args = parser.parse_args()

# Access the arguments
print(f"Filename: {args.filename}")
if args.verbose:
    print("Verbosity turned on")
if args.count:
    print(f"Count: {args.count}")
```

#### Running the Script

Assuming the script is saved as `example.py`, you can run it from the command line like this:

```sh
python example.py test.txt -v -c 3
```

#### Output

```vbnet
Filename: test.txt
Verbosity turned on
Count: 3
```
