python找不到自带的argparse_没有提供参数时,python argparse设置行为

您可以将funcs附加到一个属性args.funcs,然后使用一个if语句来提供默认行为,如果没有设置任何选项:

if not args.funcs:

args.funcs = [func1, func2, func3]

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()

% test.py

func1

func2

func3

% test.py -d

func3

% test.py -d -i

func3

func2

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

% test.py -i -d

func2

func3

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

针对更新2:

你的代码可以正常工作.但是,这是另一种组织方式:

>不是将主程序嵌套在if子句中,您可以

使用

if not lists:

sys.exit('No jpegs found')

# put main program here, unnested

sys.exit将打印没有找到stderr的jpegs并以退出代码1终止.

>虽然我最初建议使用functools.partial,但另一种 – 也许更简单 – 的方式现在浮现在脑海中:而不是

for func in args.funcs:

func()

我们可以说

for func, args in args.funcs:

func(args)

我们需要做的就是在args.func中存储一个元组(func,args)

而不是单独的功能.

例如:

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值