2016-03-12 53 views
0

我寫與argparse選項的代碼爲:argparse多個選項組合

parser.add_argument("--nb", help="show number", action='store_true') 
parser.add_argument("--md", help="Create xyz file", action='store_true') 
parser.add_argument("--xsf", help="Create xsf file for md(default is xyz)" 
        , action='store_true') 

並正確調用。

但我想,比如說--xsf可以使用--md選項。如果我使用

./mycode.py --nb --xsf 

它應該給一個錯誤/警告--xsf--nb工作,並與--md只有

+0

能' md'和'nb'走在一起? – hpaulj

+0

沒有。實際上,正如我在zondo的帖子中發現的那樣,'nb'和'md'是互斥組。我只想'-xsf'作爲'md'的子選項 – BaRud

+0

你會如何解釋你的用戶的替代方案?什麼樣的「使用」線會清楚? – hpaulj

回答

1

您可以添加一個互斥組:

parser.add_argument("--md", help="Create xyz file", action='store_true') 

group = parser.add_mutually_exclusive_group() 

group.add_argument("--nb", help="show number", action='store_true') 
group.add_argument("--xsf", help="Create xsf file for md(default is xyz)" 
        , action='store_true')