6

Python の使用関数に問題があります。これは私の主な機能の一部です:

def main(argv):
    try:
            opts, args = getopt.getopt(argv, 'hi:o:tbpms:', ['help', 'input=', 'output='])
            if not opts:
                    print 'No options supplied'
                    usage()
    except getopt.GetoptError,e:
           print e
           usage()
           sys.exit(2)

    for opt, arg in opts:
            if opt in ('-h', '--help'):
                    usage()
                   sys.exit(2)
if __name__ =='__main__':
    main(sys.argv[1:])

使用関数も定義します

def usage():
    print "\nThis is the usage function\n"
    print 'Usage: '+sys.argv[0]+' -i <file1> [option]'

しかし、コードを./code.pyor ./code.py -h(実行可能) として実行すると、Python のヘルプしか表示されません。

4

1 に答える 1

8

以下は私のために働いた。「python code.py」で実行します。

import getopt, sys

def usage():
  print "\nThis is the usage function\n"
  print 'Usage: '+sys.argv[0]+' -i <file1> [option]'

def main(argv):
  try:
    opts, args = getopt.getopt(argv, 'hi:o:tbpms:', ['help', 'input=', 'output='])
    if not opts:
      print 'No options supplied'
      usage()
  except getopt.GetoptError,e:
    print e
    usage()
    sys.exit(2)

  for opt, arg in opts:
    if opt in ('-h', '--help'):
      usage()
      sys.exit(2)

if __name__ =='__main__':
    main(sys.argv[1:])
于 2011-09-26T14:33:29.237 に答える