Last active
February 7, 2017 13:52
-
-
Save farsil/50285bba6c4cf0fcf45e8530db1e01cd to your computer and use it in GitHub Desktop.
Template for command-line python script (file processing)
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 python3 | |
'Module docstring.' | |
import argparse as ap | |
import glob as gl | |
import sys | |
def run(paths): | |
'Runs the algorithm.' | |
for glob in paths: | |
for filename in gl.glob(glob): | |
# do something with filename | |
print(filename) | |
def main(): | |
'Parses arguments.' | |
parser = ap.ArgumentParser(argument_default=ap.SUPPRESS, \ | |
description=sys.modules[__name__].__doc__) | |
# add arguments with parser.add_argument() | |
parser.add_argument("paths", type=str, nargs=ap.REMAINDER, \ | |
help='argument help, supports unix-style globs') | |
if len(sys.argv) == 1: | |
parser.print_help() | |
sys.exit(1) | |
run(**vars(parser.parse_args())) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment