没有提供参数时,python argparse设置行为

d.l*_*ous 7 python python-2.7 argparse

我是python的新手,在使用命令行参数时我仍然坚持如何构造我的简单脚本.

该脚本的目的是在我的工作中自动执行与排序和操作图像相关的一些日常任务.

我可以指定参数并让它们调用相关函数,但我也想在没有提供参数时设置默认操作.

这是我目前的结构.

parser = argparse.ArgumentParser()
parser.add_argument("-l", "--list", help="Create CSV of images", action="store_true")
parser.add_argument("-d", "--dimensions", help="Copy images with incorrect dimensions to new directory", action="store_true")
parser.add_argument("-i", "--interactive", help="Run script in interactive mode", action="store_true")
args = parser.parse_args()

if args.list:
    func1()
if args.interactive:
    func2()
if args.dimensions:
    func3()
Run Code Online (Sandbox Code Playgroud)

但是,当我没有提供任何论据时,将不会被称为.

Namespace(dimensions=False, interactive=False, list=False)
Run Code Online (Sandbox Code Playgroud)

如果没有提供争论,我想要的是一些默认行为

if args.list:
        func1()
    if args.interactive:
        func2()
    if args.dimensions:
        func3()
    if no args supplied:
        func1()
        func2()
        func3()
Run Code Online (Sandbox Code Playgroud)

这似乎应该相当容易实现,但我失去了如何检测所有参数是错误的逻辑,而不通过参数循环并测试是否所有都是假的.

更新

多个参数一起有效,这就是为什么我没有沿着elif路线走下去.

更新2

这是我更新的代码,考虑到@unutbu的答案

它看起来并不理想,因为所有内容都包含在if语句中,但在短期内我找不到更好的解决方案.我很高兴接受来自@unutbu的答案,所提供的任何其他改进都将受到赞赏.

lists = analyseImages()
    if lists:
        statusTable(lists)

        createCsvPartial = partial(createCsv, lists['file_list'])
        controlInputParital = partial(controlInput, lists)
        resizeImagePartial = partial(resizeImage, lists['resized'])
        optimiseImagePartial = partial(optimiseImage, lists['size_issues'])
        dimensionIssuesPartial = partial(dimensionIssues, lists['dim_issues'])

        parser = argparse.ArgumentParser()
        parser.add_argument(
        "-l", "--list", 
        dest='funcs', action="append_const", const=createCsvPartial,
        help="Create CSV of images",)
        parser.add_argument(
        "-c", "--convert", 
        dest='funcs', action="append_const", const=resizeImagePartial,
        help="Convert images from 1500 x 2000px to 900 x 1200px ",)
        parser.add_argument(
        "-o", "--optimise", 
        dest='funcs', action="append_const", const=optimiseImagePartial,    
        help="Optimise filesize for 900 x 1200px images",)
        parser.add_argument(
        "-d", "--dimensions", 
        dest='funcs', action="append_const", const=dimensionIssuesPartial,
        help="Copy images with incorrect dimensions to new directory",)
        parser.add_argument(
        "-i", "--interactive", 
        dest='funcs', action="append_const", const=controlInputParital,
        help="Run script in interactive mode",)
        args = parser.parse_args()

        if not args.funcs:
            args.funcs = [createCsvPartial, resizeImagePartial, optimiseImagePartial, dimensionIssuesPartial]

        for func in args.funcs:
            func()

    else:
        print 'No jpegs found'
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 7

您可以append_const将func设置为属性,args.funcs然后使用一个if语句提供默认行为(如果未设置任何选项):

if not args.funcs:
    args.funcs = [func1, func2, func3]
Run Code Online (Sandbox Code Playgroud)
import argparse

def func1(): pass
def func2(): pass
def func3(): pass

parser = argparse.ArgumentParser()
parser.add_argument(
    "-l", "--list",
    dest='funcs', action="append_const", const=func1,
    help="Create CSV of images", )
parser.add_argument(
    "-i", "--interactive",
    dest='funcs', action="append_const", const=func2,
    help="Run script in interactive mode",)
parser.add_argument(
    "-d", "--dimensions",
    dest='funcs', action='append_const', const=func3,
    help="Copy images with incorrect dimensions to new directory")
args = parser.parse_args()
if not args.funcs:
    args.funcs = [func1, func2, func3]

for func in args.funcs:
    print(func.func_name)
    func()
Run Code Online (Sandbox Code Playgroud)
% test.py
func1
func2
func3

% test.py -d
func3

% test.py -d -i
func3
func2
Run Code Online (Sandbox Code Playgroud)

请注意,与原始代码不同,这允许用户控制调用函数的顺序:

% test.py -i -d
func2
func3
Run Code Online (Sandbox Code Playgroud)

这可能是也可能不是可取的.


针对更新2:

你的代码工作得很好.但是,这是另一种组织方式:

例如:

import argparse
import sys

def parse_args(lists):
    funcs = {
        'createCsv': (createCsv, lists['file_list']),
        'resizeImage': (resizeImage, lists['resized']),
        'optimiseImage': (optimiseImage, lists['size_issues']),
        'dimensionIssues': (dimensionIssues, lists['dim_issues']),
        'controlInput': (controlInput, lists)
    }
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-l", "--list",
        dest='funcs', action="append_const", const=funcs['createCsv'],
        help="Create CSV of images",)
    parser.add_argument(
        "-c", "--convert",
        dest='funcs', action="append_const", const=funcs['resizeImage'],
        help="Convert images from 1500 x 2000px to 900 x 1200px ",)
    parser.add_argument(
        "-o", "--optimise",
        dest='funcs', action="append_const", const=funcs['optimiseImage'],
        help="Optimise filesize for 900 x 1200px images",)
    parser.add_argument(
        "-d", "--dimensions",
        dest='funcs', action="append_const", const=funcs['dimensionIssues'],
        help="Copy images with incorrect dimensions to new directory",)
    parser.add_argument(
        "-i", "--interactive",
        dest='funcs', action="append_const", const=funcs['controlInput'],
        help="Run script in interactive mode",)
    args = parser.parse_args()
    if not args.funcs:
        args.funcs = [funcs[task] for task in
                      ('createCsv', 'resizeImage', 'optimiseImage', 'dimensionIssues')]
    return args

if __name__ == '__main__':
    lists = analyseImages()

    if not lists:
        sys.exit('No jpegs found')

    args = parse_args(lists)   
    statusTable(lists)    
    for func, args in args.funcs:
        func(args)
Run Code Online (Sandbox Code Playgroud)


Aid*_*len 5

您可以通过检查 args 的数量是否等于 1 来处理此问题。这意味着只传递了您的 python 命令。

import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument("-l", "--list", help="Create CSV of images", action="store_true")
parser.add_argument("-d", "--dimensions", help="Copy images with incorrect dimensions to new directory", action="store_true")
parser.add_argument("-i", "--interactive", help="Run script in interactive mode", action="store_true")
args = parser.parse_args()

if len(sys.argv)==1:
    # display help message when no args are passed.
    parser.print_help()
    sys.exit(1)
Run Code Online (Sandbox Code Playgroud)