1

I have a python parsing arguments like below:

Code:

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Args test')
    parser.add_argument('-myarg1', '--myarg1', type=str, dest='myarg1', required=True, help='Help yourself')
    parser.add_argument('-myarg2', '--myarg2', type=str, dest='myarg2', default=' ', help='Help yourself')

    args = parser.parse_args()
    print(args.myarg1)
    print(args.myarg2)

Above works if I call the script like below:

python myargs.py -myarg1 something -myarg2 somethingelse

But it does not work if I call it like below:

python myargs.py -myarg1 something -myarg2

And throws the below error obviously because it expects caller to pass value for the second argument.

usage: myargs.py [-h] -myarg1 MYARG1 [-myarg2 MYARG2]
myargs.py: error: argument -myarg2/--myarg2: expected one argument

Quesion:
I understand the reason for python complaining about it above. But, I want the user of my python script to be able to call the second argument with just saying -myarg2 or --myarg2 without specifying the type. Just like shell script style. Is it possible to do it with argparse?

I am using python 2.7 and above.

2
  • What is the expected value type of myarg2? Commented Nov 17, 2020 at 8:13
  • 1
    I updated the value type. Its str. Both arguments are string type Commented Nov 17, 2020 at 8:15

3 Answers 3

2

It is possible. You can use the action="store_true" attribute to turn an argument into a Boolean (flag).

parser.add_argument('-myarg2', '--myarg2', dest='myarg2', action="store_true", help='Help yourself')
args = parser.parse_args()
print(args.myarg2) # True if "python myargs.py -myarg2", False if "python myargs.py"

Edit

If you want the user to be able to pass an optional argument to the myarg2 flag, you need to use the nargs='?' attribute. You also need to define a default attribute which will be called if the flag isn't used, and a const attribute which will be called if the flag is used but without arguments.

parser.add_argument('-myarg2', '--myarg2', dest='myarg2', nargs='?', const="no value", default='no flag', help='Help yourself')
Sign up to request clarification or add additional context in comments.

2 Comments

But if she wants to pass a str argument this will result raise an error
Indeed, I've modified my answer to take care of this case
1

The problem is that you call the argument flag without a value. If you want the value to be an empty string do -

python myargs.py -myarg1 something -myarg2 ' '

1 Comment

I know that I can pass ' ' or ''. It works but I wanted to avoid it
1

if you want to use --myarg and interpret it as true (otherwise it is false) you need to use

parser.add_argument("-myarg2", "--myarg2", action="store_true")

5 Comments

But if she wants to pass a str argument this will result raise an error
@Lior Cohen good point. Strange that python does not give a way to pass string or not pass a string. But if this is the constraint in python, then I would have to live with it.
Ok. Just one question. action='store_false' makes it false?
How to check if user has passed an input or not?
@AdeleGoldberg Indeed, action="store_false"will make it false, and action="store_const" will make it the value of the const attribute. You can learn more here

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.