I want to implement import feature with required and optional parameters, to run this in this way:
python manage.py import --mode archive where --mode is required and archive also.
I'm using argparse library.
class Command(BaseCommand): help = 'Import' def add_arguments(self, parser): parser.add_argument('--mode', required=True, ) parser.add_argument('archive', required=True, default=False, help='Make import archive events' ) But I recived error:
TypeError: 'required' is an invalid argument for positionals 22 Answers
You created a positional argument (no -- option in front of the name). Positional arguments are always required. You can't use required=True for such options, just drop the required. Drop the default too; a required argument can't have a default value (it would never be used anyway):
parser.add_argument('archive', help='Make import archive events' ) If you meant for archive to be a command-line switch, use --archive instead.
I think that --mode archive is supposed to mean "mode is archive", in other words archive is the value of the --mode argument, not a separate argument. If it were, it would have to be --archive which is not what you want.
Just leave out the definition of archive.