1

I am having little difficulty with argparse, and I would appreciate if somebody could advise on following two problems:

1) my script takes several commnad line arguments, one of them is --color. I would like argparse to enforce the following syntax: --color=True and not tolerate syntax such as: --color True.

parser = argparse.ArgumentParser()
parser.add_argument('--color', nargs=1, default=True)

2) Another option I am using is -i

parser.add_argument('-i','--ignorecase', action='store_true')
args = parser.parse_args()

depending whether True or False, re.compile will use flags=re.IGNORECASE or flags=0

if (args.ignorecase == True):

   CASE_SENSITIVITY=re.IGNORECASE
else:

   CASE_SENSITIVITY=0

pattern = re.compile('my_pattern', flags=CASE_SENSITIVITY)

is there any simpler way to assign the value to CASE_SENSITIVITY ? Perhaps inside the parser.add_argument, so that I can avoid the multiple steps, if else, and so on ?

1 Answer 1

1

You could always use a ternary operator such as:

CASE_SENSITIVITY = 0 if not args.ignorecase else re.IGNORECASE
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.