Switch argparse file specification and pipeline

$ python egg.py -i some-list.list
$ cat some-list.list | python egg.py
$ python egg.py < some-list.list

The one that automatically switches between these things.

#!/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())

If there are both, I think -i will take precedence.

Recommended Posts

Switch argparse file specification and pipeline
datetime and file write and backup
Read and write csv file
Device and Linux file system
Read and write a file
Linux file and directory permissions
Write and read a file