Как закодировать комбинационные параметры argparse в python

У меня возникли проблемы с выполнением этого небольшого задания. Я провел небольшой эксперимент, но не смог может достичь результата.

Требование:

   test2.py [-c/-v] -f

Использование или правила:

  1. -c (compare) принимает 2 параметра.

    -v (verify) принимает 1 параметр.

    Либо из эти два должны присутствовать, , но не оба .

  2. -f - обязательный параметр (имя выходного файла).

Вывод: 1. В выходных данных не будет указано, что -c / -v является обязательным для любого из них, но не обоих . Он указывает, что все аргументы являются необязательными.
2. The output will indicate -f option under optional arguments which is incorrect. -f is mandatory argument, and I want to display outside - optional arguments.

How to change the script so that -h option output will be more user friendly (without any external validation)

usage: test.py <functional argument> <ouput target argument>

Package Compare/Verifier tool.

optional arguments:
  -h, --help            show this help message and exit
  -f outFileName, --file outFileName
                        File Name where result is stored.
  -c Package1 Package2, --compare Package1 Package2
                        Compare two packages.
  -v Package, --verify Package
                        Verify Content of package.
kiran@kiran-laptop:~/Study/scripts$ 

Code:

I am using the below code to achieve the output,

#!/usr/bin/python

import sys
import argparse

def main():
    usage='%(prog)s <functional argument> <ouput target argument>'
    description='Package Compare/Verifier tool.'
    parser = argparse.ArgumentParser(usage=usage,description=description)

    parser.add_argument('-f','--file',action='store',nargs=1,dest='outFileName',help='File Name where result is stored.',metavar="outFileName",required=True)


    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-c','--compare',action='store',nargs=2,dest='packageInfo',help='Compare two packages.',metavar=("Package1","Package2"))
    group.add_argument('-v','--verify',action='store',nargs=1,dest='packageName',help='Verify Content of package.',metavar='Package')
    args = parser.parse_args()

if __name__ == "__main__":
    main()
18
задан kumar_m_kiran 22 November 2013 в 10:56
поделиться