Last active
December 22, 2018 13:53
-
-
Save finswimmer/61b093b99920c5de4753ee9596ce3a1a to your computer and use it in GitHub Desktop.
boilerplate to create new scripts, that can read and write from stream and/or filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| from __future__ import print_function | |
| import sys | |
| from argparse import ArgumentParser | |
| def main(): | |
| args = get_args() | |
| def get_args(): | |
| parser = ArgumentParser(prog="progname.py", description="") | |
| parser.add_argument('--version', action='version', version='%(prog)s 0.1') | |
| parser.add_argument("input", help="input file, use - to read from stdin") | |
| parser.add_argument("-o", "--output", help="output file") | |
| if len(sys.argv) == 1: | |
| parser.print_help(sys.stderr) | |
| sys.exit(1) | |
| args = parser.parse_args() | |
| if args.output: | |
| sys.stdout = open(args.output, "w") | |
| if args.input != "-": | |
| sys.stdin = open(args.input, "r") | |
| return args | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment