Last active
September 24, 2020 10:24
-
-
Save simomarsili/430850e507328181c64334df52a595fa to your computer and use it in GitHub Desktop.
Template script reading from input file
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
""" | |
Template script with input file. | |
Use the module docstring as the script description. | |
Example usage: script.py -a 100 < infile | |
""" | |
import sys | |
from argparse import ArgumentParser, FileType | |
def parse_args(): | |
"""Command line parser.""" | |
parser = ArgumentParser(description=__doc__) | |
parser.add_argument('-i', '--infile', nargs='?', type=FileType('r'), | |
default=sys.stdin) | |
parser.add_argument('-o', '--outfile', nargs='?', type=FileType('w'), | |
default=sys.stdout) | |
parser.add_argument('-a', | |
'--arg', | |
type=float, | |
help='Example argument', | |
default=None) | |
return parser.parse_args() | |
def main(): | |
"""Main script.""" | |
args = vars(parse_args()) # parsed args as a dict | |
infile = args.pop('infile') | |
outfile = args.pop('outfile') | |
print( | |
'Input file: %r\n' | |
'Output file: %r\n' | |
'Arguments: %r' % (infile, outfile, args) | |
) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment