1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

argparseのファイル指定とパイプラインを切り替える

Last updated at Posted at 2017-02-06
$ python egg.py -i some-list.list
$ cat some-list.list | python egg.py
$ python egg.py < some-list.list

こういうのを自動で切り替えるやつ。

#!/usr/bin/env python3
# coding: utf-8

import argparse
import sys


parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-i', '--inline', type=argparse.FileType('r'), default=sys.stdin, help='some list')

if __name__ == '__main__':
    args = parser.parse_args(args=sys.argv[1:])
    print(args.inline.read())

両方あった場合は-iのが優先されるんじゃないかな

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?