Parameters received as arrays in management command on Windows 10

I am using a management command:

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('foo', nargs=1, type=str)

    def handle(self, *args, **options):
        print(type(options['foo']))

And calling from the command prompt:

python manage.py my_command bar

But this outputs:

<class 'list'>

Why not a string? Is this different on Linux?

The docs for nargs state:

N (an integer). N arguments from the command line will be gathered together into a list.

And later:

If the nargs keyword argument is not provided, the number of arguments consumed is determined by the action. Generally this means a single command-line argument will be consumed and a single item (not a list) will be produced.

So, stop parsing nargs and you will get just the string.

2 Likes