如何使用Python Argparse和Click来制作Git版本库的CLI。

问题描述 投票:0回答:1

我正在尝试制作一个 CLI 工具来执行版本库中的 python 脚本,我的一个朋友帮我做了,但考虑到这是我第一次使用 argparse 和 click 来制作 CLI,我在正确执行方面遇到了问题。我的一个朋友一直在帮助我,但考虑到这是我第一次使用 argparse 和 click 来制作 CLI,我在正确执行方面遇到了问题。我的目标是制作一个能与我的Python文件夹中的所有子目录兼容的CLI。

https:/github.comRichard-BarrettSalesforceCLItreemasterPython。

在这里,我有一个名为:sfdc.py的脚本,我想最终将其作为路径导入到 /usr/local/bin 在shell中执行。

实现这个功能的主脚本应该是 sfdc.py 我想这样称呼它

python sfdc.py <sub-directory> <optional_flag> 所以子目录是一个参数,可执行脚本是一个可选的标志。

这里是主要代码。https:/github.comRichard -BarrettSalesforceCLIblobmasterPythonsfdc.py。

实际代码。

#!/usr/bin/env python
import argparse

parser = argparse.ArgumentParser(
    description='SalesforceCLI will return Pandas Dataframes from Salesforce Cases within an Organizations SFDC. It will also allow you to interact with Salesforce Lighting Experience or Service console from within the CLI. You will even be able to make leads, create cases, and send off emails all from your CLI!',
    epilog="SalesforceCLI is here to help you automate reports and data within your Organizations SFDC"
)

# Poitional Arguments
parser.add_argument('accounts', help='Pandas Dataframe that shows all available accounts active within an organizational SFDC')
parser.add_argument('cases', help='cases dataframes related to defined case report, default is set to all cases')
parser.add_argument('contacts', help='return a list of contacts as a dataframe')
parser.add_argument('leads', help='leads dataframes related to all defined leads for user access, default is set to all concurrent leads within an organizational SFDC')
parser.add_argument('lightning', help='Work with Salesforce Lightning from the CLI')
parser.add_argument('service', help='Work with Salesforce Service Console from the CLI')
parser.add_argument('soql', help='SOQL custom query for users within an SFDC')
parser.add_argument('reports', help='reports dataframes related to defined reporst, default is set to list all available reports for use with SFDC access')

# Optional Arguments
parser.add_argument('-v','--version', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='Returns the version of SalesforceCLI'),
#printf("Optional Arguments for cases")
parser.add_argument('-s1','--sev1', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='Return Pandas Dataframe for all Severity Level 1 Cases')
parser.add_argument('-s2','--sev2', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='Return Pandas Dataframe for all Severity Level 2 Cases')
parser.add_argument('-s3','--sev3', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='Return Pandas Dataframe for all Severity Level 3 Cases')
parser.add_argument('-s4','--sev4', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='Return Pandas Dataframe for all Severity Level 4 Cases')

args = parser.parse_args()

if args.sev1:
   execfile('Cases/read_all_sev1_cases.py')

if args.sev2:
   execfile('Cases/read_all_sev2_cases.py')

如何用这个结构做一个CLI工具?帮助和文本输出几乎达到了我想要的效果。我甚至把它改成了这样的行数,但一直没有效果。

parser.add_argument('-s2','--sev2', action='store_true',
                    help='Return Pandas Dataframe for all Severity Level 2 Cases')

有谁有什么建议,如何用python写一个CLI?

我试图以一种方式运行代码

python sfdc.py cases --sev2

当我运行它的时候,我得到了以下的回溯。

 richardbarret@1152-MBP  ~/Git/SalesforceCLI/Python   master ● ⍟1  python sfdc.py cases -s1                                                             ✔  1128  20:19:53
usage: sfdc.py [-h] [-v] [-s1]
               accounts cases contacts leads lightning service soql reports
sfdc.py: error: too few arguments
python command-line-interface argparse python-click
1个回答
1
投票

所以,使用 Click既然在问题中打上了标签,我最终用以下的方法来实现这个目标。

import click


def sev1():
    os.system('python3 ~/Git/SalesforceCLI/Python/Cases/read_all_sev1_cases.py')

def sev2():
    os.system('python3 ~/Git/SalesforceCLI/Python/Cases/read_all_sev2_cases.py')

def sev3():
    os.system('python3 ~/Git/SalesforceCLI/Python/Cases/read_all_sev3_cases.py')

def sev4():
    os.system('python3 ~/Git/SalesforceCLI/Python/Cases/read_all_sev4_cases.py')

def handover():
    os.system('python3 ~/Git/SalesforceCLI/Python/Cases/read_all_handover_cases.py')

severities = [sev1, sev2, sev3, sev4]

@click.command('sfdc')
@click.argument('subdirectory', type=click.Path())
@click.version_option()
@click.option('-ho', '--handover', 'do_handover', is_flag=True)
@click.option('-s', '--severity', type=click.Choice(['1', '2', '3', '4']), required=False)
def sfdc(subdirectory, do_handover, severity):
    subdirectory = os.path.abspath(subdirectory)
    if severity:
        severity = int(severity) - 1
        severity_method = severities[severity]
        severity_method()
    if do_handover:
        handover()


if __name__ == '__main__':
    sfdc()

这似乎勾选了所有的要求 而且,在我看来,至少是,稍微更易读。这是否有助于解决你的问题?

一个执行的例子是。

python sfdc.py -ho -s 1
python sfdc.py -ho
python sfdc.py -s 3
© www.soinside.com 2019 - 2024. All rights reserved.