Specifying default filenames with argparse, but not opening them on --help?

后端 未结 3 1439
日久生厌
日久生厌 2021-02-07 17:05

Let\'s say I have a script that does some work on a file. It takes this file\'s name on the command line, but if it\'s not provided, it defaults to a known filename (conte

3条回答
  •  情话喂你
    2021-02-07 17:44

    Use stdin as default:

    parser.add_argument('file', default='-', nargs='?', type=argparse.FileType('r'))
    

提交回复
热议问题