0

I am trying to add a command line argument depending on some values returned by the functions. When I am not giving that argument it says:

main.py: error: argument -opp/--operator is required

When I am giving the argument it says:

main.py: error: unrecognized arguments: -opp +

Following is the piece of EDITED code (as told in one of the answers):

parser.add_argument('-z', help='Help msg', required=True)
args, unknown = parser.parse_known_args()
value = some_functions(args.z)
if value == some_particular_value:
    parser.add_argument('-opp','--operator',help='Some help msg',required=True)
args = parser.parse_args()

Please help me in adding this argument. Thanks!

3
  • 1
    The argument given with one - has to be one character only, e.g. -o. Commented Mar 9, 2015 at 6:27
  • @KlausD. even then its not working. Commented Mar 9, 2015 at 6:34
  • 1
    You have a couple of code glitches: 1. in line 4 it should be == instead of =. In line 5 it should be parser.add_argument(...) instead of parser = add_argument(...). Does that fix your problem? Commented Mar 9, 2015 at 7:07

1 Answer 1

2

There are, though, a couple of mistakes in your code. Here's the corrected version:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-z', help = 'Help msg', required = True)

args, _ = parser.parse_known_args()

# value = some_functions(args.z)

if value == some_particular_value:
    parser.add_argument('-opp', '--operator', help = 'Some help msg', required = True)

    # args2, _ = parser.parse_known_args()
    # some_function2(args2.operator)

So, let's analyse your mistakes:

Assigning instead of comparing

That's typical newbie mistake. Within a conditional operator (if, case...) you set the value instead of checking it. The difference is in amount of the = sign.

If you assign value, the condition in the operator will be always True and test will always succeed (in most of programming languages and cases).

Check this out:

a = 1

if a = 2:
  print a

This may print 2 in some languages (like C or Java; using the correct syntax). Why? You've just set it! Yet, Python is smart enough to tell you about your mistake:

File "<stdin>", line 1
    if a = 2:
         ^
SyntaxError: invalid syntax

And compare it to this:

a = 1

if a == 2:
  print a

This will not print anything. Because the if test did not pass.

Assigning value instead of calling method

You want to be using the method add_argument instead of re-defining the parser variable, right?

parser = add_argument(...)

That's something like I've described above. You should be calling a method of a parser variable, not defining its new value:

parser.add_argument(...)

Re-parsing arguments is missing?

You did not show the part of the code where you check for the operator argument. Note: you should parse your arguments again, when defined a new argument:

parser.add_argument(...)
args, _ = parser.parse_known_arguments()

Then you will get a new argument in the args variable.

Using the wrong name of argument?

Again, you are missing part of code, where you check for the operator argument' value. If you are trying to access it with

args.opp # whoops...

Then you'd just get an error saying There's no argument 'opp'!, because it has its full name and should be accessed with it:

args.operator # aaah, here it is!
Sign up to request clarification or add additional context in comments.

2 Comments

I made all the changes you told, but it is still having the same problem and also I am using the correct name of the operator.
@Vipul just show the exact code line where the error appears. I suppose you are missing something with the argument name while trying to access it...

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.