Python: How to make an option to be required in optparse?

前端 未结 9 2039
走了就别回头了
走了就别回头了 2021-02-05 00:57

I\'ve read this http://docs.python.org/release/2.6.2/library/optparse.html

But I\'m not so clear how to make an option to be required in optparse?

I\'ve tried to

9条回答
  •  情歌与酒
    2021-02-05 01:21

    You can implement a required option easily.

    parser = OptionParser(usage='usage: %prog [options] arguments')
    parser.add_option('-f', '--file', 
                            dest='filename',
                            help='foo help')
    (options, args) = parser.parse_args()
    if not options.filename:   # if filename is not given
        parser.error('Filename not given')
    

提交回复
热议问题