0

を使用してoptparse、オプション リスト パラメータのリストを add_option() を呼び出す場所から分離したいと考えています。これが機能するように、ファイル A にパッケージ化してからファイル B にアンパックするにはどうすればよいですか? parser_options.append() 行は、書かれているとおりに機能しません...

ファイル A:

import file_b
parser_options = []
parser_options.append(('-b', '--bootcount', type='string', dest='bootcount', default='', help='Number of times to repeat booting and testing, if applicable'))
parser_options.append(('-d', '--duration', type='string', dest='duration', default='', help='Number of hours to run the test.  Decimals OK'))

my_object = file_b.B(parser_options)

ファイル B は parser_options を入力として受け取ります。

import optparse
class B:
    def __init__(self, parser_options):
        self.parser = optparse.OptionParser('MyTest Options')
        if parser_options:
            for option in parser_options: 
                self.parser.add_option(option)

*編集: オブジェクトを使用するように修正

4

3 に答える 3

0

オプションを何らかのデータ構造に押し込もうとするよりも、指定したパーサーにオプションを追加する関数をファイル A に定義する方が簡単ではないでしょうか?

ファイル A:

def addOptions(parser):
    parser.add_option('-b', '--bootcount', type='string', dest='bootcount', default='', help='Number of times to repeat booting and testing, if applicable')
    parser.add_option('-d', '--duration', type='string', dest='duration', default='', help='Number of hours to run the test.  Decimals OK')

ファイル B:

import optparse
def build_parser(parser_options):
    parser = optparse.OptionParser('MyTest Options')
    if parser_options:
        parser_options(parser)

他の場所:

import file_a
import file_b
file_b.build_parser(file_a.addOptions)
于 2013-01-09T00:36:10.337 に答える
0

問題は、タプルでキーワード引数を渡そうとしていることです。コード('-b', '--bootcount', type='string', dest='bootcount', default='', help='Number of times to repeat booting and testing, if applicable')は関数呼び出しでのみ有効であり、他の場所では有効ではありません。type='string'ビットはタプルでは正しくありません!

関数の引数を渡したい場合は、位置引数にリストまたはタプルを使用し、キーワード引数に辞書を使用する必要があります。argsタプルとkwargs辞書を含むタプルの単一のタプルを変更することで、これを行うことができる 1 つの方法を次に示します。

parser_options = []
parser_options.append((('-b', '--bootcount'),
                       dict(type='string', dest='bootcount', default='',
                            help='Number of times to repeat booting and testing, if applicable')))
parser_options.append((('-d', '--duration'),
                       dict(type='string', dest='duration', default='',
                            help='Number of hours to run the test.  Decimals OK')))

*他のファイルでは、 and演算子を使用してタプルと dict の内容を適切な関数に渡し、**引数をアンパックできます。

class B:
    def __init__(self, parser_options)
        self.parser = optparse.OptionParser('MyTest Options')
        if parser_options:
            for args, kwargs in parser_options: 
                self.parser.add_option(*args, **kwargs)
于 2013-01-09T04:14:44.557 に答える
0

構築時にパーサーをオブジェクトに渡すことになりました。呼び出しモジュールから名前を付けることができるので、これは素晴らしいことです。

import optparse
parser = optparse.OptionParser('My Diagnostics')
parser.add_option('-p', '--pbootcount', type='string', dest='pbootcount', default='testing1234', help=' blah blah')
c = myobject.MyObject(parser)
于 2013-01-09T15:15:41.077 に答える