【问题标题】:Using Argparse to create required argument with multiple options?使用 Argparse 创建具有多个选项的所需参数?
【发布时间】:2018-07-23 08:33:38
【问题描述】:

如果我正确理解 Argparse,位置参数是用户可以指定的必需参数。我需要使用 argparse 创建一个位置参数,用户可以在其中指定某种类型的参数,如果他/她提出 -h 选项则显示该参数。我试过使用 add_argument_group 但它只是在你调出 -h 选项时只显示一个带有其他参数描述的标题。

def Main():
    parser = argparse.ArgumentParser(description = __doc__, formatter_class = argparse.RawDescriptionHelpFormatter)
    parser.add_argument("input_directory",help = "The input directory where all of the files reside in")

    sub_parser = parser.add_argument_group('File Type')

    sub_parser.add_argument(".txt",help = "The input file is a .txt file")
    sub_parser.add_argument(".n12",help = "The input file is a .n12 file")
    sub_parser.add_argument(".csv",help = "The input file is a .csv file")

    parser.parse_args()

if __name__ == "__main__":
    Main()

所以当我运行脚本时,我应该指定以便运行脚本。如果我选择 .txt、.n12 或 .csv 作为我的参数,那么脚本应该运行。但是,如果我没有从列出的这 3 个选项中指定文件类型,那么脚本将不会运行。

我是否缺少可以为位置参数指定多个选项的 argparse 函数?

【问题讨论】:

  • 您在询问某种枚举?你能展示一些命令应该是什么样子的例子吗?
  • 您的代码定义了 4 个位置参数,因此需要来自用户的 4 个字符串。正如您所注意到的,argument_group 只会影响帮助显示;它在解析过程中什么都不做。我建议您尝试使用带有 choices=['txt', 'n12', 'csv'] 参数的标记参数。

标签: python file argparse


【解决方案1】:

使用choices= 参数强制用户从一组受限值中进行选择。

import argparse

def Main():
    parser = argparse.ArgumentParser()
    parser.add_argument("input_directory",help = "The input directory where all of the files reside in")
    parser.add_argument("file_type", help = "File Type", choices=['.txt', '.n12', '.csv'])

    ns = parser.parse_args()
    print(ns)


if __name__ == "__main__":
    Main()

【讨论】:

    【解决方案2】:

    我认为你把这弄得太复杂了。如果我正确理解您的问题,您希望用户输入两个参数:目录名称和文件类型。您的应用程序将只接受三个文件类型值。简单地这样做怎么样:

    import argparse
    
    def Main():
        parser = argparse.ArgumentParser(description = __doc__, formatter_class = argparse.RawDescriptionHelpFormatter)
        parser.add_argument("input_directory", help = "The input directory where all of the files reside in")
        parser.add_argument("file_type", help="One of: .txt, .n12, .csv")
        args = parser.parse_args()
        print(args)
    
    if __name__ == "__main__":
        Main()
    

    ...并添加应用程序逻辑以拒绝文件类型的无效值。

    您通过 parse_args() 返回的对象访问用户输入的值。

    【讨论】:

      【解决方案3】:

      使用选项分组功能使用add_mutually_exclusive_group()而不是add_argument_group()

      import argparse
      
      
      def Main():
          parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
          parser.add_argument("input_directory", help="The input directory where all of the files reside in")
      
          group = parser.add_mutually_exclusive_group(required=True)
          group.add_argument("-txt", action='store_true', help="The input file is a .txt file")
          group.add_argument("-n12", action='store_true', help="The input file is a .n12 file")
          group.add_argument("-csv", action='store_true', help="The input file is a .csv file")
      
          print parser.parse_args()
      
      if __name__ == "__main__":
          Main()
      

      【讨论】:

        猜你喜欢
        • 2018-07-21
        • 2020-03-16
        • 2015-12-11
        • 1970-01-01
        • 2016-05-04
        • 2020-11-03
        • 1970-01-01
        • 2020-10-23
        • 1970-01-01
        相关资源
        最近更新 更多