如何使用 Python Argparse 和 Click 为 Git 存储库制作 CLI

How to Make a CLI for a Git Repository Using Python Argparse and Click

我正在尝试制作一个 CLI 工具来在存储库中执行 python 脚本。我的一个朋友一直在帮助我,但考虑到这是我第一次使用 argparse 并单击以制作 CLI,我在正确执行它时遇到了问题。我的目标是制作一个 CLI,它可以与我的 Python 文件夹中的所有子目录一起工作:

https://github.com/Richard-Barrett/SalesforceCLI/tree/master/Python

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

完成这个的主要脚本应该是sfdc.py,我想这样称呼它

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

这里是主要代码:https://github.com/Richard-Barrett/SalesforceCLI/blob/master/Python/sfdc.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

因此,使用 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