0

What's the best way to set a group argument as the default when no arguments are called.

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--a", action="store_true") #call when no arguments are provided
group.add_argument("--b", action="store_true")
group.add_argument("--c", action="store_true")

Let's call my program argparse_ex.py. I want argparse.py (with no arguments) and argparse.py --a to return the same output.

1 Answer 1

1

I would just add a simple test after parsing

if not any([args.a, args.b, args.c]):
   args.a=True

This is simpler than any attempt to make parse_args to do this. The parser will parse all arguments independently - and in any order. So you really can't tell until the parsing is all done whether any of the options has been selected or not.

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.