Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading