I am trying to write a parsing for my python script where the command parameters should be in the following order,
OUTPUT :
cli unmount [-h] -f FS_NAME [-n NODE_SPEC] [--evict [--force]]]
I am able to write the code for rest of the commands except for the last one. [--evict [--force]]. This means that --force argument will only apply if --evict is given.
parser = argparse.ArgumentParser('CLI demo')
sub_parser = parser.add_subparsers()
unmount = sub_parser.add_parser('unmount')
unmount.add_argument("-f", "--fs", dest="fs_name", required=True, help="filesystem name.")
unmount.add_argument("-n", "--nodes", dest="nodes", metavar='NODE_SPEC', help="pdsh style nodes hostnames (If this parameters ")
These are the two approaches I have taken for adding the optional child argument, --force to the optional parent argument, --evict,
Approach 1:
evict_parser = unmount.add_subparsers()
evict = evict_parser.add_parser("--evict", help="evict lustre clients before unmount.")
evict.add_argument("--force", dest="force", action="store_true", default=False, help="force mode for evict lustre clients.")
parser.parse_args()
and Approach 2:
parent_cmd_parser = argparse.ArgumentParser(add_help=F)
parent_cmd_parser.add_argument("--force", dest="force", action="store_true", default=False, help="force mode for evict lustre clients.")
evict_parser = unmount.add_subparsers()
evict = evict_parser.add_parser("--evict", help="evict lustre clients before unmount.", parents=[parent_cmd_parser])
Unfortunately none is working. In first case I am not getting the desired help output/usage help and in the second the --force argument is hidden.
--forceis used without--evict? What if it is given first?