How can I make it a required argument only if I don't select another option, so when I select version it shouldn't require -f, but the rest of the time it should be required?
parser = argparse.ArgumentParser(
description="This script will check the uri's from XXX")
parser.add_argument(
"-f", "--file", help="XXX export file to use", required=True)
parser.add_argument("-c", "--check", action="store_true",
help="Check the uri's")
parser.add_argument("-p", "--passwords", action="store_true",
help="Check the weak passwords")
parser.add_argument("-V", "--version", action="store_true",
help="Show version")
self.params = parser.parse_args(self.get_params())
quamrana
39.5k13 gold badges57 silver badges77 bronze badges
1 Answer 1
So, two things:
- In the general case, this is done with
add_mutually_exclusive_group(required=True)and adding the mutually exclusive arguments to that group (this isn't exactly what you want, since it won't allow you to pass both, but it's close enough). - In this specific case, you should be using the
action='version'action for displaying the version, which exists specifically for this purpose, and leave-fplainrequired=Trueas you've already written it.
answered Jan 23, 2022 at 20:20
ShadowRanger
158k12 gold badges222 silver badges317 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py