1

When I introduce to the command line "--debug" argument I need to set variable "debug", from my python script, to the value 1.

I've tried something, but I have to write "--debug=1" to the command line to set variable.

parser = argparse.ArgumentParser()
parser.add_argument("--debug", default=2)

When I run the command:

python script.py --rev1=1.2 --rev2=1.5 --debug

my variable "debug" should have value 1.

0

1 Answer 1

2

If you're interested to know whether a certain command line flag has been passed to your script, you'd set the action argument of Argument.add_argument to store_true.

parser.add_argument('--debug', action='store_true')

Then parser.parse_args().debug will have the value of True if --debug was present and False otherwise.

$ python script.py
parser.parse_args() returned Namespace(debug=False)
$ python script.py --debug
parser.parse_args() returned Namespace(debug=True)
Sign up to request clarification or add additional context in comments.

1 Comment

action='store_const' along with default=2 and const=1 could be used to set values of 1 or 2. store_true is a subclass of store_cont.

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.