繁体   English   中英

如何使用 Python Argparse 为 Git 存储库创建 CLI 并单击

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

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

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 工具? 帮助和文本 output 几乎是我想要的。 我什至将其更改为包含这样的行,但它没有奏效。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM