0

I have the following code:

class Command(BaseCommand):
    help = 'Build static site output.'

    def add_arguments(self, parser):
        parser.add_argument('args')

    def handle(self, *args, **options):
        """Request pages and build output."""
        if args:
            pages = args
            available = list(get_pages())
            invalid = []
            for page in pages:
                if page not in available:
                    invalid.append(page)
            if invalid:
                msg = 'Invalid pages: {}'.format(', '.join(invalid))
                raise CommandError(msg)
        else:
            ...

However when I run this command:

python prototypes.py build index

the command loops through each letter of the word index.

CommandError: Invalid pages: i, n, d, e, x

I want it to detect index as one argument and if I provide more arguments with spaces in between it should be looping through those.

If I don't add the add_arguments method it shows unrecognized argument in the console.

1
  • Django command use the standard argparse module (the parser you get is a subclass of ArgumentParser). The documentation is pretty extensive and has couple of example. It's a shame Django's documentation does not link to that of the argparse module. Commented Nov 16, 2016 at 8:30

1 Answer 1

1

This method fixed my problem.

def add_arguments(self, parser):
    parser.add_argument('args', nargs='+')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.