0

Below is the example code for argparse in python script.

group = parser.add_mutually_exclusive_group()
group.add_argument('-a','--example-a ',  action='store_true', help='--example-a ')
group.add_argument('-b','--example-b' action='store_true', help= '--example-b')
group.add_argument('-c', '--example-c', action='store_true', help= '--example-c')
group.add_argument('-d', '--directory', action='store_true', help= 'to input directory')

Please help me how to add argument if they specify '-d' '--directory' they must input directory path

and if they specify any option like -a, -b, -c with option -d they must specify directory path as input.

abc
  • 165
  • 2
  • 10
  • If `-d` is a default `store`, it will take the path as an argument. – hpaulj Mar 05 '18 at 17:10
  • -d is not a default. Directory path i have specified internally. In case they chosen -d to specify custom directory path – abc Mar 05 '18 at 17:18
  • I mean: `group.add_argument('-d', '--directory', help= ' directory')`. Also, `-d` can't be used with one of the others; they are all mutually exclusive. – hpaulj Mar 05 '18 at 17:30
  • How to define to use -d with other options example -a -d or -ad and specify directory – abc Mar 05 '18 at 17:33
  • Define the `-d` outside of the group. Let the user specify a directory regardless of what the other flags say. – hpaulj Mar 05 '18 at 22:05
  • -d is optional, in case -d is specified in command line then any one flags must specif in this case (-a, -b or -c) – abc Mar 06 '18 at 04:06
  • Test for that kind of coordination after parsing. – hpaulj Mar 06 '18 at 04:18
  • I give it up, I will go for if else – abc Mar 06 '18 at 04:43
  • [Cross-argument validation in argparse](https://stackoverflow.com/questions/49000217) – hpaulj Mar 06 '18 at 05:17
  • Thanks hpaulj, finally I have achieved with argparse. Last thing i need help is .i need to add one argument which should not apper in --help or -h output – abc Mar 07 '18 at 02:00
  • Check the docs for `help=argparse.SUPPRESS`. – hpaulj Mar 07 '18 at 02:04
  • Great, It works Thanks a lot – abc Mar 07 '18 at 02:12

0 Answers0