2013-05-27 30 views
7

我試圖在不同羣組之間建立一個互斥羣組: 我有參數-a,-b,-c和我想與-a和-b發生衝突,或-a和-c在一起。該幫助應顯示類似[-a |的內容([-b] [-c])]。使用羣組之間的互斥

下面的代碼似乎並沒有這樣做有相互排斥的選項:

import argparse 
parser = argparse.ArgumentParser(description='My desc') 
main_group = parser.add_mutually_exclusive_group() 
mysub_group = main_group.add_argument_group() 
main_group.add_argument("-a", dest='a', action='store_true', default=False, help='a help') 
mysub_group.add_argument("-b", dest='b', action='store_true',default=False,help='b help') 
mysub_group.add_argument("-c", dest='c', action='store_true',default=False,help='c help') 
parser.parse_args() 

解析不同的組合 - 全通:

> python myscript.py -h 
usage: myscript.py [-h] [-a] [-b] [-c] 

My desc 

optional arguments: 
    -h, --help show this help message and exit 
    -a   a help 
> python myscript.py -a -c 
> python myscript.py -a -b 
> python myscript.py -b -c 

我試圖改變mysub_groupadd_mutually_exclusive_group把一切都變成相互獨家:

> python myscript.py -h 
usage: myscript.py [-h] [-a | -b | -c] 

My desc 

optional arguments: 
    -h, --help show this help message and exit 
    -a   a help 
    -b   b help 
    -c   c help 

如何爲[-a |添加參數([-b] [-c])]?

回答

1

所以,這已被問了很多次。簡單的答案是「有了認同,你不能」。但是,這是一個簡單的答案。你可以利用subparsers的,所以:

import argparse 
parser = argparse.ArgumentParser(description='My desc') 
sub_parsers = parser.add_subparsers() 
parser_a = sub_parsers.add_parser("a", help='a help') 
parser_b = sub_parsers.add_parser("b", help='b help') 
parser_b.add_argument("-c", dest='c', action='store_true',default=False,help='c help') 
parser.parse_args() 

然後,您可以:

$ python parser -h 
usage: parser [-h] {a,b} ... 

My desc 

positional arguments: 
    {a,b} 
    a   a help 
    b   b help 

optional arguments: 
    -h, --help show this help message and exit 

和:

$ python parser b -h 
usage: parser b [-h] [-c] 

optional arguments: 
    -h, --help show this help message and exit 
    -c   c help 

如果你希望你的參數如問題所說,有一個看看docopt,它看起來很可愛,而且應該完全按照你的意願去做。

+0

沒有什麼我想要的,因爲它沒有給我選擇只有-c(沒有-b)。 – itzhaki

+0

這是真的,我的道歉。我不認爲你可以用argparse得到你想要的,但我仍然認爲docopt是一個不錯的選擇,你只需添加 用法:myscript.py [-h | -a | [-b -c]] 添加到您的文檔字符串中,它會爲您解決所有問題 –

0

argument_groups不影響解析。他們只是幫助格式化幫助。因此,在mutually_exclusive_group內定義一個組並不能解決這個問題。

在提議的補丁,http://bugs.python.org/issue10984'argparse add_mutually_exclusive_group should accept existing arguments to register conflicts',這將允許您定義兩個mutually_exclusive_groups,一個[-a | -b]和其他[-a | -c]。創建第二個包含已定義參數(-a)的組是此修補程序的一個小部分。生成一條有意義的使用線更加困難,並且需要重新編寫幾個HelpFormatter方法。

import argparse 
parser = argparse.ArgumentParser(description='My desc') 
group1 = parser.add_mutually_exclusive_group() 
action_a = group1.add_argument("-a", dest='a', action='store_true', default=False, help='a help') 
group1.add_argument("-b", dest='b', action='store_true',default=False,help='b help') 

group2 = parser.add_mutually_exclusive_group() 
group2.add_argument("-c", dest='c', action='store_true',default=False,help='c help') 
group2._group_actions.append(action_a) # THE KLUDGE 

print parser.format_usage() 
# usage: stack16769409.py [-h] [-a | -b] [-c] 

args = parser.parse_args() 

Usage沒有正確顯示2組。但它確實接受-b -c,同時反對-a -b-a -c。但是你可以寫一個自定義使用線。