Question: I have a script which is meant to be used like this:
`usage: installer.py dir [-h] [-v]`

`dir` is a positional argument which is defined like this:

```
parser.add_argument('dir', default=os.getcwd())

```

I want the `dir` to be optional: when it's not specified it should just be `cwd`.

Unfortunately when I don't specify the `dir` argument, I get `Error: Too few arguments`.

Answer: 
Use [`nargs='?'`](https://docs.python.org/3/library/argparse.html#nargs) (or `nargs='*'` if you need more than one dir)

```
parser.add_argument('dir', nargs='?', default=os.getcwd())

```

extended example:

```
>>> import os, argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-v', action='store_true')
_StoreTrueAction(option_strings=['-v'], dest='v', nargs=0, const=True, default=False, type=None, choices=None, help=None, metavar=None)
>>> parser.add_argument('dir', nargs='?', default=os.getcwd())
_StoreAction(option_strings=[], dest='dir', nargs='?', const=None, default='/home/vinay', type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args('somedir -v'.split())
Namespace(dir='somedir', v=True)
>>> parser.parse_args('-v'.split())
Namespace(dir='/home/vinay', v=True)
>>> parser.parse_args(''.split())
Namespace(dir='/home/vinay', v=False)
>>> parser.parse_args(['somedir'])
Namespace(dir='somedir', v=False)
>>> parser.parse_args('somedir -h -v'.split())
usage: [-h] [-v] [dir]

positional arguments:
  dir

optional arguments:
  -h, --help  show this help message and exit
  -v

```
Short Answer
------------

As already shown in the previous two answers, you can accept an optional positional argument with `nargs='?'`. You could also turn the argument directly into a `Path` type and/or shorten the cwd to `.` if you wanted to:

**myfile.py**

```py
import argparse
import pathlib

parser = argparse.ArgumentParser()
parser.add_argument("dir", nargs="?", default=".", type=pathlib.Path)
parsed_args = parser.parse_args()

print("Installing to", parsed_args.dir.resolve())

```

```bash
$ python myfile.py
Installing to /users/myname/myfolder

$ python myfile.py /usr/bin/
Installing to /usr/bin

```

Longer answer
-------------

Since you also mention the flag-style True/False options `-h` and `-v` in your question, these examples may be of use:

### Flags (e.g. `-v`)

We might refer to optional options that take no arguments as "flags". With flags, we only care about whether they are given or not. `-h` is a flag that argparse adds automatically (along with the longer version `--help`) so we shouldn't really override that. If we consider `-v` then,

**myfile.py**

```
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
        "-v",
        "--version",
        action="store_true")
parsed_args = parser.parse_args()

if parsed_args.version:
    print("version flag given")
else:
    print("version flag not given")

```

Note that the second argument to `add_argument()` is a longer name for the option. It is not mandatory but it does make your subsequent code more readable (`parsed_args.version` vs `parsed_args.v`) and makes calls to your installer more explicit.

```bash
$ python myfile.py -v
version flag given

$ python myfile.py --version
version flag given

$ python myfile.py
version flag not given

```

### Optional arguments (e.g. `--installdir /usr/bin/`)

One could argue that, in your case, you would be better off with an optional argument rather than a positional one.

**myfile.py**

```py
import argparse
import pathlib

parser = argparse.ArgumentParser()
parser.add_argument(
        "-i",
        "--installdir",  # Optional (but recommended) long version
        type=pathlib.Path,
        default="/bin"
        )
parsed_args = parser.parse_args()

print("Installing to", parsed_args.installdir)

```

```bash
$ python myfile.py -i /usr/bin/
Installing to /usr/bin

$ python myfile.py --installdir /usr/bin/
Installing to /usr/bin

$ python myfile.py
Installing to /bin

```