Last active
December 21, 2015 17:59
-
-
Save MikeDacre/6344630 to your computer and use it in GitHub Desktop.
Cumbersome python3 functions to give user the choice between either using STDIN, STDOUT, STDERR, or using -i, -o, and -l. The idea is that we want to use the protection of a `with"` statement if using the '-i' and '-o' flags for input and output files, while still allowing the use of STDIN and STDERR. It makes use of sys and argparse. It is cumb…
This file contains 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 python3 | |
# -*- coding: utf-8 -*- | |
# vim:fenc=utf-8 tabstop=4 expandtab shiftwidth=4 softtabstop=4 | |
""" | |
#==================================================================================== | |
# | |
# FILE: std-arg.py | |
# AUTHOR: Michael D Dacre, [email protected] | |
# LICENSE: Open Source | |
# CREATED: 2013-08-26 10:56 | |
# Last modified: 2013-08-26 11:19 | |
# | |
# DESCRIPTION: Cumbersome python3 functions to give user the choice between either | |
# using STDIN, STDOUT, STDERR, or using -i, -o, and -l. | |
# The idea is that we want to use the protection of a `with:` statement | |
# if using the '-i' and '-o' flags for input and output files, while still | |
# allowing the use of STDIN and STDERR. | |
# | |
# It makes use of sys and argparse. It is cumbersome because there are | |
# three functions and a 4-way if craziness to make it work. An advantage | |
# of this though, is that it allows any script to be either imported or | |
# executed directly. | |
# | |
# Essentially, this bit of cumbersomness will make any python3 script | |
# incredibly flexible. This is open source, use it as you wish | |
# | |
# USAGE: Use as you will, right now it is mostly useless, it is intended to between | |
# an example of how to accomplish this | |
# | |
#==================================================================================== | |
""" | |
def my_main(infile, outfile): | |
"""Actually operate on files""" | |
for line in infile: | |
outfile.write(line) | |
def _get_args(): | |
"""Command Line Argument Parsing""" | |
import argparse, sys | |
parser = argparse.ArgumentParser( | |
description=__doc__, | |
formatter_class=argparse.RawDescriptionHelpFormatter) | |
# Optional Arguments | |
parser.add_argument('-v', action='store_true', help="Verbose output") | |
# Required Files | |
#parser.add_argument('file', nargs=1, | |
# help="Required file") | |
# Optional Files | |
parser.add_argument('-i', '--infile', nargs='?', default=sys.stdin, | |
help="Input file, Default STDIN") | |
parser.add_argument('-o', '--outfile', nargs='?', default=sys.stdout, | |
help="Output file, Default STDOUT") | |
parser.add_argument('-l', '--logfile', nargs='?', default=sys.stderr, | |
type=argparse.FileType('a'), | |
help="Log File, Default STDERR (append mode)") | |
return parser | |
# Main function for direct running | |
if __name__ == '__main__': | |
# Get commandline arguments | |
parser = _get_args() | |
args = parser.parse_args() | |
# Test infile | |
if isinstance(args.infile, str): | |
infile_string = True | |
else: | |
infile_string = False | |
# Test outfile | |
if isinstance(args.outfile, str): | |
outfile_string = True | |
else: | |
outfile_string = False | |
# Do Stuff | |
if infile_string: | |
with open(args.infile, 'r') as infile: | |
if outfile_string: | |
with open(args.outfile, 'w') as outfile: | |
my_main(infile, outfile) | |
else: | |
my_main(infile, args.outfile) | |
else: | |
if outfile_string: | |
with open(args.outfile, 'w') as outfile: | |
my_main(args.infile, outfile) | |
else: | |
my_main(args.infile, args.outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment