在python中使用命令行参数标志的正确方法

时间:2022-08-05 20:46:16

I have a set (4 flags) of flags which control the nature of the output that my python script generates and ones which control how much information the script prints to the terminal (the way a verbose flag generally does). These flags are used by multiple functions. The values of the flags are set by optional command line arguments which I parse using argparse.

我有一个控制,我的python脚本生成和输出的性质标志的一组(4个标志)的人,其控制多少信息的脚本打印到终端(详细标志通常的方式做)。这些标志由多个功能使用。标志的值由可选的命令行参数设置,我使用argparse进行解析。

At present I have these flags in global scope outside of any of the functions that use the flags rather than have the flags passed as arguments to the functions. Like so:

目前,我在全局范围内使用这些标志在任何使用标志的函数之外,而不是将标志作为参数传递给函数。像这样:

import argparse
flag1=True
flag2=True
flag3=True

def fun1():
    ...
    if flag1:
        ..something..
    else: 
        ..something else..
    #fun1 also uses the other flags in a similar way
    ...

def fun2():
    ...
    if flag2:
        ..something..
    else: 
        ..something else..
    #fun1 also uses the other flags in a similar way
    ...

def main()
    ...
    args=parser.parse_args()
    ...
    global flag1
    flag1=args.flag1
    # and so on for the other flags 
    fun1()
    fun2()

I have been lead to believe that it is bad practice to use global variables in python from many of the answers here (this, for example). Does that apply here too? I am a little reluctant to pass the flags as arguments since I'll be passing too many variables as arguments.

我一直认为,从python中的许多答案(例如,这个)中使用全局变量是不好的做法。这也适用于此吗?我有点不愿意将标志作为参数传递,因为我将传递太多变量作为参数。

What is the best way to handle flags set by command line arguments?

处理命令行参数设置的标志的最佳方法是什么?

2 个解决方案

#1


2  

The simplest solution would be to either pass all flags to all your functions (as a single args object) or pass relevant flags only depending on which function uses which flag.

最简单的解决方案是将所有标志传递给所有函数(作为单个args对象),或者仅根据哪个函数使用哪个标志传递相关标志。

Now this can indeed become quite of a burden, specially with nested function calls where you quickly end up having to pass the whole flags from function to function to function... The cure here is to use a class so you can share state between methods:

现在这确实会成为一个负担,特别是对于嵌套函数调用,你很快就必须将整个标志从函数传递给函数到函数......这里的解决方法是使用类,这样你就可以在方法之间共享状态:

class Command(object):
    def __init__(self, flags):
        self.flags = flags

    def fun1(self):
        ...
        if self.flags.flag1:
            ..something..
        else: 
            ..something else..

    def fun2(self):
        ...
        if self.flags.flag2:
            ..something..
        else: 
            ..something else..


    def run(self):
        self.fun1()
        self.fun2()     

def main()
    ...
    args=parser.parse_args()
    cmd = Command(args)
    cmd.run()

#2


0  

I'm still a beginner, but keyword arguments seems a pretty good solution to your problem for me. I use something like this:

我仍然是初学者,但关键字参数对我来说似乎是一个很好的解决方案。我使用这样的东西:

def foo(**kwargs):
    verbose = kwargs.get('verbose', 0)  # set default value for your options
    if verbose > 2:
       print >> sys.stderr, 'some debug message or sth like that'
    if verbose > 0:
       print 'verbose note'  # for sth like 'your_app -v'

if __name__=='__main__'
    args=parser.parse_args()
    verbose = args.flag1
    foo(verbose=verbose)
    foo()

#1


2  

The simplest solution would be to either pass all flags to all your functions (as a single args object) or pass relevant flags only depending on which function uses which flag.

最简单的解决方案是将所有标志传递给所有函数(作为单个args对象),或者仅根据哪个函数使用哪个标志传递相关标志。

Now this can indeed become quite of a burden, specially with nested function calls where you quickly end up having to pass the whole flags from function to function to function... The cure here is to use a class so you can share state between methods:

现在这确实会成为一个负担,特别是对于嵌套函数调用,你很快就必须将整个标志从函数传递给函数到函数......这里的解决方法是使用类,这样你就可以在方法之间共享状态:

class Command(object):
    def __init__(self, flags):
        self.flags = flags

    def fun1(self):
        ...
        if self.flags.flag1:
            ..something..
        else: 
            ..something else..

    def fun2(self):
        ...
        if self.flags.flag2:
            ..something..
        else: 
            ..something else..


    def run(self):
        self.fun1()
        self.fun2()     

def main()
    ...
    args=parser.parse_args()
    cmd = Command(args)
    cmd.run()

#2


0  

I'm still a beginner, but keyword arguments seems a pretty good solution to your problem for me. I use something like this:

我仍然是初学者,但关键字参数对我来说似乎是一个很好的解决方案。我使用这样的东西:

def foo(**kwargs):
    verbose = kwargs.get('verbose', 0)  # set default value for your options
    if verbose > 2:
       print >> sys.stderr, 'some debug message or sth like that'
    if verbose > 0:
       print 'verbose note'  # for sth like 'your_app -v'

if __name__=='__main__'
    args=parser.parse_args()
    verbose = args.flag1
    foo(verbose=verbose)
    foo()