2010-04-21 72 views
3

我目前正在學習如何使用Python optparse模塊。我正在嘗試下面的示例腳本,但是args變量是空的。我嘗試使用Python 2.5和2.6,但無濟於事。Python optparse不適合我

import optparse 

def main(): 
    p = optparse.OptionParser() 
    p.add_option('--person', '-p', action='store', dest='person', default='Me') 
    options, args = p.parse_args() 

    print '\n[Debug]: Print options:', options 
    print '\n[Debug]: Print args:', args 
    print 

    if len(args) != 1: 
    p.print_help() 
    else: 
    print 'Hello %s' % options.person 

if __name__ == '__main__': 
    main() 

輸出:

>C:\Scripts\example>hello.py -p Kelvin 

[Debug]: Print options: {'person': 'Kelvin'} 

[Debug]: Print args: [] 

Usage: hello.py [options] 

選項: -h,--help顯示此幫助信息並退出 -p PERSON,--person = PERSON

回答

6

args變量保存任何未被分配到選項的參數。通過將Kelvin分配給person選項變量,您的代碼確實可以正常工作。

如果你試圖運行hello.py -p Kelvin file1.txt,你會發現,person仍然被賦予的值"Kelvin",然後你args將包含"file1.txt"

the documentation on optparse參見:

parse_args()返回兩個值:

  • options,包含所有選項,e.g值的對象。如果--file接受一個字符串參數,然後options.file將是由用戶提供的文件名,或None如果用戶沒有提供該選項
  • args,位置參數列表解析選項
+0

退換哦,我明白了。我閱讀幫助文檔,但我想我必須仔細閱讀才能完全理解。謝謝!哇,第一次在這裏,我得到了一個快速的迴應。 – Nebu10z 2010-04-21 16:47:28

+0

你應該選擇一個答案,讓人們知道你是值得信賴的,下次你也會得到快速的迴應! – Personman 2010-04-22 01:10:09

0
後剩

注意:「選項」是一個包含您添加的選項的字典。 「參數」是一個包含未解析參數的列表。你不應該看長度「參數」。這裏是一個成績單來說明:

moshez-mb:profile administrator$ cat foo 
import optparse 

def main(): 
    p = optparse.OptionParser() 
    p.add_option('--person', '-p', action='store', dest='person', default='Me') 
    options, args = p.parse_args() 
    print '\n[Debug]: Print options:', options 
    print '\n[Debug]: Print args:', args 
    print 
    if len(args) != 1: 
     p.print_help() 
    else: 
     print 'Hello %s' % options.person 

if __name__ == '__main__': 
    main() 
moshez-mb:profile administrator$ python foo 

[Debug]: Print options: {'person': 'Me'} 

[Debug]: Print args: [] 

Usage: foo [options] 

Options: 
    -h, --help   show this help message and exit 
    -p PERSON, --person=PERSON 
moshez-mb:profile administrator$ python foo -p Moshe 

[Debug]: Print options: {'person': 'Moshe'} 

[Debug]: Print args: [] 

Usage: foo [options] 

Options: 
    -h, --help   show this help message and exit 
    -p PERSON, --person=PERSON 
moshez-mb:profile administrator$ python foo -p Moshe argument 

[Debug]: Print options: {'person': 'Moshe'} 

[Debug]: Print args: ['argument'] 

Hello Moshe 
moshez-mb:profile administrator$ 
+0

非常感謝。 – Nebu10z 2010-04-21 16:54:53

0

根據optparse幫助:

「在成功返回一個對(值,參數),其中‘價值’是價值實例(與你的所有選項值)和'args'是解析選項後左邊的的參數列表。「

嘗試hello.py -p Kelving abcd - '開' 將optparse解析, 'ABCD' 將在args可變土地parse_args

+0

我試過了,你說得對。 – Nebu10z 2010-04-21 16:52:50

0
import ast 

(options, args) = parser.parse_args() 
noargs = ast.literal_eval(options.__str__()).keys() 
if len(noargs) != 1: 
    parser.error("ERROR: INCORRECT NUMBER OF ARGUMENTS") 
    sys.exit(1) 
+0

我不確定ast模塊與args變量爲什麼沒有任何關係。參數的數量不正確,它們的分配方式不同。 – Mike 2014-10-31 12:20:29

+0

可否請讓我知道什麼是錯誤的code.btw這是一個工作...我不能讓len(args)使其工作....不知道我是否缺少一些東西。 – Rama 2014-10-31 12:29:03

+0

它沒有解決這個問題,海報問爲什麼'args'是空的。答案是參數**應該是空的,因爲'optparser'是如何工作的。您的代碼將添加「person''鍵並顯示它,但這不是在這裏應該發生的。 – Mike 2014-10-31 12:38:03