I
I
Ilya Fateev2014-03-27 21:03:51
linux
Ilya Fateev, 2014-03-27 21:03:51

How can python 2.x argparser get either one required argument or one optional?

How can python 2.x argparser get EITHER one required argument OR one optional?
That is, EITHER python @[email protected] is a required parameter
OR python @[email protected] is an optional parameter

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxaon, 2014-03-28
@fat32nov

What's the problem?

from argparse import ArgumentParser

parser = ArgumentParser(usage="Here is usagage")
parser.add_argument("positional",nargs="*")
parser.add_argument("--flag")

if __name__ == "__main__":
    args = parser.parse_args()
    if args.positional and not args.flag:
        print("i'm positional")
    elif args.flag and not args.flag:
        print ("i'm flag")
    else:
        raise Exception("RTFM")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question