【问题标题】:Python Optparse module for no argument option implementation用于无参数选项实现的 Python Optparse 模块
【发布时间】:2014-10-22 08:39:03
【问题描述】:

我有一个脚本,我需要在没有任何选项的情况下限制运行该脚本 我已经尝试了以下程序,但我有点遗漏了一些帮助

#!/usr/bin/python
import os
import sys
import optparse

path = "/tmp/enable.txt"
usage = "This program will Help you to Enable and Disable Release Management"
parser = optparse.OptionParser(usage)

parser.add_option("-e", "--enable", dest="enable", action="store_true", help="This will     enable Release management")
parser.add_option("-d", "--disable", dest="disable", action="store_true", help="This will disable Release management")
(options, args) = parser.parse_args()

optionsdict = vars(选项)

 for k,v in optionsdict.items():
      if v is None:
      all_none = True


if all_none:
 parser.print_help()
 sys.exit()
if options.enable:
if os.path.exists(path):
   print "Its already enabled"
else:
   open(path, 'w').close()
   print "Enabled"

if options.disable:
 if not os.path.exists(path):
   print "Its already disabled"
else:
   os.remove(path)
   print "Disabled"

只有当我在没有选项的情况下运行我的脚本时,一切正常,我需要告诉我 --help 选项并告诉我输入一些选项

只有代码的中心部分没有工作,我错过了一些东西

提前致谢 萨蒂什库马尔

【问题讨论】:

    标签: python argparse optparse


    【解决方案1】:

    我终于找到了答案,而不是运行整个选项,这将解决问题

    if len(sys.argv[1:])== 0:
      print "Kindly enter some options"
    

    谢谢 萨蒂什

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      • 2013-01-09
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多