小编Neb*_*10z的帖子

Python optparse不适合我

我目前正在学习如何使用Python optparse模块.我正在尝试以下示例脚本,但args变量为空.我尝试使用Python 2.5和2.6,但无济于事.

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() 
Run Code Online (Sandbox Code Playgroud)

输出:

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

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

[Debug]: Print args: []

Usage: hello.py [options]
Run Code Online (Sandbox Code Playgroud)

选项:-h, - help显示此帮助消息并退出-p PERSON, - person = PERSON

python optparse

4
推荐指数
1
解决办法
3482
查看次数

标签 统计

optparse ×1

python ×1