3

If c is not know to the user and a = 3 and b = 2 I want to call the program like this:

$ python my_program 3 2

if c is know to the user and c = 9 I want to call the program like this:

$ python my_program -c 6

Since the calculation represented by c = results.a + results.b takes about 30 minutes complete I would like the ability to skip over it if c is known.

Would this be possible? I have two different Code examples below but this is not exactly what I want.

Code example 1

import argparse

def my_function():

    if results.c == 0:
        c = results.a + results.b
        d = c + 5

    else:
        d = results.c + 5


if __name__=='__main__':

    parser = argparse.ArgumentParser()

    parser.add_argument('a', action="store", type=int)
    parser.add_argument('b', action="store", type=int)
    parser.add_argument('-c', action="store", default=0, type=int)

    results = parser.parse_args()

    my_function()

My code example 1 required calls would be:

$ python my_program 3 2

or

$ python my_program 3 2 -c 6

Code Example 2

import argparse

def my_function():

    if results.c == 0:
        c = results.a + results.b
        d = c + 5

    else:
        d = results.c + 5


if __name__=='__main__':

    parser = argparse.ArgumentParser()

    parser.add_argument('-a', action="store", default=0, type=int)
    parser.add_argument('-b', action="store", default=0, type=int)
    parser.add_argument('-c', action="store", default=0, type=int)

    results = parser.parse_args()

    my_function()

My code example 2 required calls would be:

$ python my_program -a 3 -b 2

or

$ python my_program -c 6

Thank you for any help. Have a good day.

1
  • 2
    In your first example, set required=False for all arguments? Also, I'd use a single add_argument('ab', nargs=2) for a and b. Commented Jun 8, 2013 at 14:15

1 Answer 1

3

You'll have to make all arguments optional and validate them yourself:

ap=argparse.ArgumentParser()

ap.add_argument('-c', required=False)
ap.add_argument('a', nargs='?')
ap.add_argument('b', nargs='?')

args = ap.parse_args()

if (args.c is not None):
    # process c
elif (args.a is not None and args.b is not None):
    # process a and b
else:
    # validation errors

If you were willing to make a and b a single named parameter instead of positionals, you could use mutual exclusion to do this for you. Mutually exclusive groups require all parameters in them to be optional, and named parameters can only be required.

ap=argparse.ArgumentParser()

g = ap.add_mutually_exclusive_group(required=True)
g.add_argument('-ab', nargs=2, required=False)
g.add_argument('-c', required=False)

args = ap.parse_args()

print('args={}'.format(repr(args)))

if (args.c is not None):
    # process c
elif (args.ab is not None):
    a, b = args.ab
    # process a and b
else:
    raise Exception("should never get here")
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.