bonzi
bonzi

Reputation: 359

Required commandline options in Python using argparse

I have the following scenario for command line argument. If there is a particular option then there should be some other required options. For example if there is -- create then there should be --name. Also if there is --remove then there should be --id. Is it possible to implement this scenario with argparse? or someother thing?

Upvotes: 5

Views: 8509

Answers (2)

intuited
intuited

Reputation: 24034

This can be done with subcommands as long as you don't mind create and remove not being preceded with hyphens. This may make sense anyway, since those verbs are often used as actions rather than options.

Upvotes: 8

AJ.
AJ.

Reputation: 28174

Optional is implicit, required must be specified:

http://docs.python.org/library/argparse.html#required

That said, there doesn't appear to be a built-in mechanism for argument "dependencies", as I think you would like to implement. This would be a requirement for your application.

Upvotes: 1

Related Questions