【问题标题】:Python Add CMD Call to ArgeParsePython 将 CMD 调用添加到 ArgParse
【发布时间】:2018-07-12 07:29:07
【问题描述】:

我正在尝试在我的 python 代码中的帮助部分添加一个选项。当用户使用 -h 运行 cmd 调用时,他们将收到以下提示。

如果他们不知道什么节目可用,我想选择 -sl 选项,它将运行 cmd 并将列表作为结果输出到同一终端。由于我缺乏经验和涉及 awk 的 cmd,引用也被证明是一个问题。

未触及的命令行调用如下: 工作信息--allshow | awk -F "[, ]+" '/jobs/{print $(NF-1)}'

showlist = "jobsinfo --allshow | awk -F " + "[, ]+" + "'/jobs/{print     $(NF-1)}'"

# Help section
parser = argparse.ArgumentParser(description="Check the render queue for a time period")
parser.add_argument("-l", "--location", default=site, help="The location you want to check. ""#Examples: london, denver, montreal...")
parser.add_argument("-p", "--project", default="New", help="The project you want to check. The Default is set to new. "
                    "#Examples: Please see SHOW list here: http://dnet.dneg.com/display/COMMS/SHOW+INFORMATION")
# parser.add_argument("-sl", "--showlist", action=showlist, help="List the Shows currently on Servers.")
parser.add_argument("-d", "--days", type=int,  default=14, help="The number of days you wish to go back. The default is set to 14 days. "
                    "#Examples: -d 3, -d 9, -d 14")
args = parser.parse_args()

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: python linux bash awk cmd


    【解决方案1】:

    这里只是给你一些想法。由于我没有你的jobsinfo 程序,所以我使用以下命令作为运行命令

    ls -l | awk 'NR < 10 { print " quoting no problem " $NF }' 
    

    注意,这不是真的推荐,但很容易做到。见http://mywiki.wooledge.org/ParsingLs

    无论如何,对于您给出的代码,我只需要进行一些更改。 showlist 选项变为

    parser.add_argument("-s", "--showlist",
                        action='store_const', const=True, 
                        help="List the Shows currently on Servers.")
    

    这意味着如果您使用-s 选项 (./argparse_help.py -s),那么您可以执行以下操作:

    args = parser.parse_args()
    
    if args.showlist:
        print "showlist is True"
    else:
        print "showlist is False"
    

    如果需要,请参阅 Python.org 的 API referencetutorial

    要实际运行该命令,您可能需要subprocess 模块。例如,我使用 Python 的原始多行字符串 (r""" triple quoted strings""") 来避免引用问题(当然,除了 shell 问题):

    import subprocess
    test_run = r"""   ls -l | 
                awk 'BEGIN { 
                        print "'\'' quotes are like they would be in the shell '\''"
                     }
                     NR < 10 { 
                        print  $NF 
                     }'   
    
               """
    subprocess.call(test_run, shell=True)
    

    运行时,这给出了

    ~$> ./argparse_help.py -s
    ' quotes are like they would be in the shell '
    117984
    anvil_group.sh
    a.out
    argparse_help.py
    a.text
    awk
    bin
    Library
    confused_out
    

    不过,这主要可以在 Python 中完成,从而避免使用 shell=True

    import subprocess
    print "' quotes are like in python '"
    for line in subprocess.check_output(['ls', '-l']).splitlines()[:9]:
        print line.split()[-1]
    

    和输出:

    ~$> ./argparse_help.py -s
    ' quotes are like in python '
    117984
    anvil_group.sh
    a.out
    argparse_help.py
    a.text
    awk
    bin
    Library
    confused_out
    

    希望你能再次出发。

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2018-10-05
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多