1

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
asked Jan 23, 2022 at 20:14

1 Answer 1

3

So, two things:

  1. 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).
  2. In this specific case, you should be using the action='version' action for displaying the version, which exists specifically for this purpose, and leave -f plain required=True as you've already written it.
answered Jan 23, 2022 at 20:20
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.