VikR
VikR

Reputation: 5142

Django argparse: Unrecognized Arguments --settings?

I've looked at the other SO threads about unrecognized argparse arguments, but none of them seem to match up with my situation.

Here's my argparse code, located at apps/create_pdf/management/commands/create_pdf.py:


from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    help = 'html to pdf for specified website.'

    def add_arguments(self, parser):
        print('cp#1')

        parser = argparse.ArgumentParser(description='html to pdf for specified website')
        parser.add_argument('-w', '--website', nargs =1, action = 'store', choices = ['SITE_1', 'SITE_2'], default='', help='site to be scraped.')
        args = parser.parse_args()

        print(args)
        breakpoint()

    def handle(self, *args, **options):
        print('cp#2')

        # create_pdf_from_html()

I run this from the terminal like so:

python3 manage.py create_pdf --website=SITE_1 --settings=server_config.settings

...and I'm getting this error:

manage.py: error: unrecognized arguments: create_pdf --settings=server_config.settings

The settings path is correct and if I'm not using argparse, everything runs fine.

What am I missing?

Upvotes: 1

Views: 1213

Answers (1)

Oleksii Tambovtsev
Oleksii Tambovtsev

Reputation: 2834

The correct way to add arguments to your command:

from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    help = 'html to pdf for specified website.'

    def add_arguments(self, parser):
        parser.add_argument('-w', '--website', nargs =1, action = 'store', choices = ['SITE_1', 'SITE_2'], default='', help='site to be scraped.')


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

        # create_pdf_from_html()

Upvotes: 2

Related Questions